#!/bin/sh

set -e
fail=0
[ -n "$1" ] && dir="$1" || dir="usr/share/petname"
[ -n "$2" ] && chars="$2" || chars=12
[ -n "$3" ] && syllables="$3" || syllables
adjectives=$(cat $dir/adjectives.txt)
adverbs=$(cat $dir/adverbs.txt)
names=$(cat $dir/names.txt)

succeed() {
	echo "⤷ Success"
}

fail() {
	echo "⤷ Failure"
	exit 1
}

echo "TEST [01/16]: Ensure that we have no blacklisted words or derivatives [$dir]..."
grep -xi -Ff debian/blacklist.txt $dir/*txt && fail || succeed

echo "TEST [02/16]: Ensure that we only have a-z in our words; no funny characters; no upper case [$dir]..."
grep "[^abcdefghijklmnopqrstuvwxyz]" $dir/*txt && fail || succeed

echo "TEST [03/16]: Ensure that we have no adjectives more than $chars characters [$dir]..."
grep "[a-z]\{$((chars+1))\}" $dir/adjectives.txt && fail || succeed

echo "TEST [04/16]: Ensure that we have no names more than $chars characters [$dir]..."
grep "[a-z]\{$((chars+1))\}" $dir/names.txt && fail || succeed

echo "TEST [05/16]: Ensure that we have no adverbs more than $((chars+2)) characters [$dir]..."
grep "[a-z]\{$((chars+3))\}" $dir/names.txt && fail || succeed

# Done with checks against our large/complex wordlist
[ $chars -gt 40 ] && exit 0
[ $syllables -gt 20 ] && exit 0

echo "TEST [06/16]: Ensure that we have no repeated words [$dir]..."
grep -xi -Ff $dir/adjectives.txt $dir/adverbs.txt && fail || succeed
grep -xi -Ff $dir/adjectives.txt $dir/names.txt && fail || succeed
grep -xi -Ff $dir/names.txt $dir/adverbs.txt && fail || succeed

echo "TEST [07/16]: Ensure that all adverbs end in -ly [$dir]..."
grep -v "ly$" $dir/adverbs.txt && fail || succeed

echo "TEST [08/16]: Ensure that we have no adjectives more than $syllables syllables [$dir]..."
output=$(cat $dir/adjectives.txt | perl -e "use strict; use Lingua::EN::Syllable; while(<>) { chomp; print \$_ . '\n' if syllable(\$_) > $syllables; };")
if [ -n "$output" ]; then
	echo "$output"
	fail
else
	succeed
fi

echo "TEST [09/16]: Ensure that we have no names more than $syllables syllables [$dir]..."
output=$(cat $dir/names.txt | perl -e "use strict; use Lingua::EN::Syllable; while(<>) { chomp; print \$_ . '\n' if syllable(\$_) > $syllables; };")
if [ -n "$output" ]; then
	echo "$output"
	fail
else
	succeed
fi

echo "TEST [10/16]: Ensure that we have no adverbs more than $((syllables+1)) syllables [$dir]..."
output=$(cat $dir/adverbs.txt | perl -e "use strict; use Lingua::EN::Syllable; while(<>) { chomp; print \$_ . '\n' if syllable(\$_) > $((syllables+1)); };")
if [ -n "$output" ]; then
	echo "$output"
	fail
else
	succeed
fi

echo "TEST [11/16]: Ensure all adverbs and adjectives are in the ispell(1) dictionary [$dir]..."
for word in $adverbs $adjectives; do
	if (echo "$word" | ispell | grep -qs "^word: ok"); then
		continue
	else
		echo $word
		fail=1
	fi
done
[ "$fail" = "1" ] && fail || succeed

echo "TEST [12/16]: Ensure all adverbs are common in the SCOWL database [$dir]..."
for word in $adverbs; do
	if grep -qsri "^$word$" /usr/share/dict/scowl/english-words.[0-5]?; then
		continue
	else
		echo $word
		fail=1
	fi
done
[ "$fail" = "1" ] && fail || succeed

echo "TEST [13/16]: Ensure all adjectives are common in the SCOWL database [$dir]..."
for word in $adjectives; do
	if grep -qsri "^$word$" /usr/share/dict/scowl/english-words.[0-5]?; then
		continue
	else
		echo $word
		fail=1
	fi
done
[ "$fail" = "1" ] && fail || succeed

if ! pgrep dictd >/dev/null 2>&1; then
	echo "Skipping dict(1) tests..."
	exit 0
fi

echo "TEST [14/16]: Ensure all adverbs are actually adverbs in the dict(1) dictionary [$dir]..."
for word in $adverbs; do
	if dict -h localhost "$word" 2>&1 | egrep -qs "^      adv | adv\."; then
		continue
	else
		echo $word
		fail=1
	fi
done
[ "$fail" = "1" ] && fail || succeed

echo "TEST [15/16]: Ensure all adjectives are actually adjectives in the dict(1) dictionary [$dir]..."
for word in $adjectives; do
	if dict -h localhost "$word" 2>&1 | egrep -qs "^      adj | adj\."; then
		continue
	elif echo "$word" | egrep -qs "ing$|ed$"; then
		continue
	else
		echo $word
		fail=1
	fi
done
[ "$fail" = "1" ] && fail || succeed

echo "TEST [16/16]: Ensure that all names are common nouns in the dictionary [$dir]..."
for word in $names; do
	score=0
	if grep -qs "^$word$" debian/whitelist.txt; then
		continue
	fi
	if ! (echo "$word" | ispell | grep -qs "^word: ok"); then
		score=$((score+1))
	fi
	if ! (dict -h localhost "$word" 2>&1 | egrep -qs "^      n | n\.|\\n\.") ; then
		score=$((score+1))
	fi
	if ! grep -qsri "^$word$" /usr/share/dict/scowl/english-words.[0-7]?; then
		score=$((score+1))
	fi
	if [ $score -gt 1 ]; then
		echo $word
		fail=1
	fi
done
if [ "$fail" = "1" ]; then
	fail
else
	succeed
fi
