#!/bin/bash

# INSTALLATION:
# 1 - put this file in the directory with your text novel/text.
# 2 - command line, make this file executable.
# chmod +x check.bash
# 3 - edit the configuration below to choose what to test.
# 4 - run command.

# USAGE:  check.bash [Input File Name]
# Best make the file a straight text file.  .txt

# Example:

# ./check.bash novel.txt > results.txt

# DISCLAIMER:
# This is a hack job.  Give fixes to T@Cattail.Nu
# It's been tested on straight text files. Anything else you throw
# at it is likely to generate ugly results.


##################################################
#
# EDIT HERE / CONFIGURATION
#
# Change 0's to 1's to perform test.
#
##################################################

duplicate_word=1
usa_punctuation=0
british_punctuation=0
a_an_check=1
oxford_commas=0
no_oxford_commas=0
capital_after_stop=0
missing_brackets=0
unmatched_quotes=1

##################################################
#
# DO NOT EDIT BELOW
#
##################################################


if [ -f tlfcheck.txt ]; then
  rm tlfcheck.txt
fi

if [ -f tlffinal.txt ]; then
  rm tlffinal.txt
fi

if [ $# -ne 1 ] ; then
  echo "You must provide a file to check." > /dev/stderr
  echo "USAGE check.bash [File Name]" > /dev/stderr
  exit
fi

if [ ! -f "$1" ]; then
  echo "Input file doesn't exist." > /dev/stderr
  exit
fi

echo "~~ STARTING TESTS ~~" >> tlfcheck.txt

if [ $unmatched_quotes -eq 1 ] ; then
  echo " " >> tlfcheck.txt
  echo "~~ Unmatched quotes check: ~~" >> tlfcheck.txt
  cp "$1" tlftest.txt
  awk -F[\"\“\”] '{print NF-1 "\t" $0}' tlftest.txt | grep -E '^[13579]' >> tlfcheck.txt
  rm tlftest.txt
fi

if [ $missing_brackets -eq 1 ] ; then
  echo " " >> tlfcheck.txt
  echo "~~ No Oxford comma check: ~~" >> tlfcheck.txt
  grep -E '\([^)]*$' $1 >> tlfcheck.txt
fi

if [ $capital_after_stop -eq 1 ] ; then
  echo " " >> tlfcheck.txt
  echo "~~ Capital letter after .?! check: ~~" >> tlfcheck.txt
  grep -E '[^.][^A-Z]\. [a-z]' $1 >> tlfcheck.txt
  grep -E '[^.][^A-Z]\? [a-z]' $1 >> tlfcheck.txt
  grep -E '[^.][^A-Z]\! [a-z]' $1 >> tlfcheck.txt
fi

if [ $no_oxford_commas -eq 1 ] ; then
  echo " " >> tlfcheck.txt
  echo "~~ No Oxford comma check: ~~" >> tlfcheck.txt
  grep -E '\w+, \w+, and ' $1 >> tlfcheck.txt
fi

if [ $oxford_commas -eq 1 ] ; then
  echo " " >> tlfcheck.txt
  echo "~~ Oxford comma check: ~~" >> tlfcheck.txt
  grep -E '\w+, \w+ and ' $1 >> tlfcheck.txt
fi

if [ $duplicate_word -eq 1 ] ; then
  echo " " >> tlfcheck.txt
  echo "~~ Duplicate word check: ~~" >> tlfcheck.txt
  grep -E '(\b\S+\b)\s+\b\1\b' $1 >> tlfcheck.txt
fi

if [ $a_an_check -eq 1 ] ; then
  echo " " >> tlfcheck.txt
  echo "~~ A vs. An Check: ~~" >> tlfcheck.txt
  grep -E '\s{1}[Aa]{1}\ [aeiouAEIOU]' $1 >> tlfcheck.txt
fi

if [ $usa_punctuation -eq 1 ] ; then
  echo " " >> tlfcheck.txt
  echo "~~ US English comma errors: ~~" >> tlfcheck.txt
  grep -E '"[.,]' $1 >> tlfcheck.txt
  grep -E '“[.,]' $1 >> tlfcheck.txt
fi

if [ $british_punctuation -eq 1 ] ; then
  echo " " >> tlfcheck.txt
  echo "~~ British English comma errors: ~~" >> tlfcheck.txt
  grep -E '[.,]\"' $1 >> tlfcheck.txt
  grep -E '[.,]“' $1 >> tlfcheck.txt
fi

echo "~~ END OF TESTS ~~" >> tlfcheck.txt

awk '{print $0 "\n"}' tlfcheck.txt > tlffinal.txt

cat tlffinal.txt

rm tlfcheck.txt
rm tlffinal.txt

#EOF

