#!/bin/bash

# Philippe Teuwen <phil_a_teuwen_point_org>
# @doegox

REALPATH=$0
SCRIPT_BASENAME="${REALPATH##*/}"
REALDIR="${REALPATH%$SCRIPT_BASENAME}"
if [ "$REALDIR" ]; then
    cd "$REALDIR"
fi

DICT_PATH="link-grammar"
sentencez=$(cat)
secret=$1
secret=${secret:-0}
DEBUG=false

# Prepare common words dict
###########################

TMPDICT=/tmp/4.0.dict.short
# we eliminate the list "a<>an" and "such a<>such an" 
# as there should have been a stricter rule
[ -e $TMPDICT ] ||\
    cat $DICT_PATH/en/4.0.dict |\
	tr '\n;' ' \n'|\
	sed 's/^ \+//;s/  / /g'|\
	grep '^[a-z].* [a-z].*:'|\
	sed 's/:.*$//'|\
	grep -v "^\(a an\|such_a such_an\)$" \
	> $TMPDICT

# Deciphering:
##############

# loosy way to diffuse the secret
$DEBUG && echo -n "key: $secret"
secret=$(echo "$secret * 1234567 % 32768"|bc)
$DEBUG && echo "=>$secret"
oldnx=0

sentencex=""
for wordz in $sentencez; do
    if [[ "$wordz" =~ ":" ]]; then
	# This is a ciphered word
	if [[ "$wordz" =~ "/" ]]; then
	    eval $(echo $wordz | sed 's/\(.*\):\(.*\)\/\(.*\)/file=\1;nx=\2;t=\3/')
	else
	    eval $(echo $wordz | sed 's/\(.*\):\(.*\)/file=\1;nx=\2/')
	    if [[ "$file" =~ "@" ]]; then
		l=${file##@}
		t=$(cat $TMPDICT |\
		    awk "FNR==$l{print NF}")
	    else
	    	t=$(cat $DICT_PATH/en/words/words.$file | wc -w)
	    fi
	fi
	n=$(echo "($nx - 1 - $secret - $oldnx + ($t * ((($secret + $oldnx) / $t)+1))) % $t + 1"|bc)
	[ $secret -ne 0 ] && oldnx=$nx
	wordx="$file:$n"
    else
	wordx=$wordz
    fi
    [ "$sentencex" != "" ] && sentencex="$sentencex "
    sentencex="$sentencex$wordx"
done

$DEBUG && echo -n "Dciphering:" 1>&2
$DEBUG && echo $sentencex 1>&2

# Decoding:
###########

was_a=false
for wordx in $sentencex; do
    if [[ "$wordx" =~ ":" ]]; then
	# This is an encoded word
	eval $(echo $wordx | sed 's/\(.*\):\(.*\)/file=\1;n=\2/')
	if [[ "$file" =~ "@" ]]; then
	    l=${file##@}
	    word=$(cat $TMPDICT |\
		awk "FNR==$l{print \$$n}")
	    word=${word//_/ }
	else
	    word=$(cat $DICT_PATH/en/words/words.$file |\
		sed 's/ $//;s/ /\n/g;'|\
		awk "FNR==$n")
	    # Schtroumpf mode
	    # Example: ./decode n smurf
	    [ "$3" != "" ] && [[ "$word" =~ "\.$2" ]] && word=$3
	fi
	# Clean word extension
	word=${word/.*}
	is_a=false
    else
	word=${wordx}
	[ "$word" == "a" ] && is_a=true || is_a=false
    fi
    # as the grammar cannot make the diff between "a" and "an"
    # we ll try to do smart guess
    if $was_a; then
	a="a"
	[[ "$word" =~ "^[aeio]" ]] && a="an"
	word="$a $word"
    fi
    was_a=$is_a
    if ! $is_a; then
	[ "$sentence" != "" ] && sentence="$sentence "
	sentence="$sentence$word"
    fi
done

$DEBUG && echo -n "Decoding:  " 1>&2
$DEBUG && echo $sentence 1>&2
echo $sentence
