#!/bin/msh
# word.sh - word game
# Licence: GNU GPL v3 or later at your option
# Author:  Florian Bruhin (The Compiler) <florianbruh@gmail.com>
version="1.0"
# Copyright 2009 Florian Bruhin

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#=============================================================#
#=============================================================#

echo "$*" | grep -q trace && set -x
echo "$*" | grep -q echo && set -v
echo "$*" | grep -q "debug" && debug=1
echo "$*" | grep "noexec" && set -n

OLDIFS="$IFS"

dir="$HOME/.word"
file="$dir/words"
savefile="$dir/sav"
license="$dir/copying"

[ ! -e "$dir" ] && mkdir "$dir"
[ ! -f "$file" ] && >"$file"
[ ! -s "$license" ] && echo "License not found. See <http://www.gnu.org/licenses/>." > "$license"

echo "$*" | grep -q clear && >"$file" && echo "Cleared." && exit 0
clear

echo "
This program is distributed in the hope that it will be useful, \
but WITHOUT ANY WARRANTY; without even the implied warranty of \
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more \
informations, type 'license' after choosing a word.

The rules of the game are quite simple: One chooses a (mostly \
4 letter long) word, then the two players may create new words \
by changing a letter, appending a letter or move a letter to \
the end of the word. The player which can't tell any new word \
loses.
"

if [ -s "$savefile" ]; then # save exists
	echo "Save file found. Load it? (y/n)"
	read ans
	case "$ans" in
		[yYjJ]*)
			old=`cat "$savefile"`
			rm "$savefile"
			save=1
			;;
		*)
			echo "Delete savefile? (y/n)" && read ans
			echo "Delete wordfile? (y/n)" && read ans2
			case "$ans" in [yYjJ]*) rm "$savefile" ;; esac
			case "$ans2" in [yYjJ]*) >"$file" ;; esac
	esac
fi
[ "$save" != 1 ] && echo "Word?" && read old && echo "$old" >> "$file"

clear
while true; do
	echo 'Instructions:
	c/change x y - replace x with y
	a/append x - append x to the word
	m/move x - delete x and append it to the word
	clear - clear file
	end/exit/quit - Quit
	license - show license
	list - list words
	'
	echo "Old: $old"

	echo "Command?"
	read chg
	IFS=" "
	set -- $chg
	IFS="$OLDIFS"
	case "$chg" in
		license)
			echo "Press q to quit the license, X and Y to scroll"
			echo "Press A now to show it."
			read lol
			less "$license"
			no=1
			;;
		end|exit|quit)
			echo "Thanks for playing. Clear file? (y/n)"
			read ans
			case "$ans" in
				y*|Y*|j*|J*) >"$file" && echo "Cleared." ;;
				*)
					echo "Not clearing file."
					echo "Save last word? (y/n)"
					read ans
					case "$ans" in
						y|Y)
							echo "Saving..."
							echo "$old" > "$savefile"
							;;
						*) echo "Not saving." ;;
					esac
					;;
			esac
			echo "Goodbye."
			exit 0
			;;
		clear) >"$file" && echo "Cleared." && no=1 ;;
		list) echo "Press q to quit." && sleep 1 && less "$file" ;;
		[cC]*)
			case "$old" in
				*$2*$2*) # contains the character at least twice
					echo "Which $2 in '$old' (number) should get replaced? g for all."
					read n
					cmd="s/$2/$3/$n"
					;;
				*$2*) cmd="s/$2/$3/" ;; # contains the character once
				*) # doesn't contain the character
					echo "Error: That doesn't contain $2!"
					sleep 1
					no=1
					;;
			esac
			;;
		[aA]*) cmd="s/\$/$2/" ;;
		[mM]*)
			case "$old" in
				*$2*$2*)
					echo "Which $2 in '$old' should get moved to the end? g for all."
					read n
					cmd="s/$2//$n;s/\$/$2/"
					;;
				*$2*) cmd="s/$2//;s/\$/$2/" ;;
				*)
					echo "Error: That doesn't contain $2!"
					sleep 1
					no=1
					;;
			esac
			;;
		*) echo "unknown command" && no=1 ;;
	esac
	new=`eval echo \""$old"\" \| sed \'"$cmd"\'`
	clear
	echo
	if [ "$no" = 1 ]; then
		no=
	elif grep -qi $new $file; then
		echo "*** Already said. ***"
	else
		echo "Ok."
		grep -qi $new $file || echo $new >> $file
		old="$new"
	fi
done

