#!/bin/bash
#
# showtimes - cgi-bin script to check wspell_info.inc and wspell_str.inc files
#
# Copyright (C) 2006  Nathan (Acorn) Pooley
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
# 
# 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 (gpl.txt) for more details. 
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# 
# You can contact the author, Nathan (Acorn) Pooley, by writing
# to Nathan (Acorn) Pooley, 949 Buckeye Drive, Sunnyvale, CA  94086, USA
# or through the form at http://www.rawbw.com/~acorn/wand/feedback.html
# 

echo "Content-type: text/html"
echo ""

echo "<html>"
echo "<body>"


echo "HELLO"
echo "</body>"
echo "</html>"
exit 0

fn_t=../../hog/wspell_tmp.upload
fn_s=../../hog/wspell_str.inc.upload
fn_i=../../hog/wspell_info.inc.upload

echo "FOOBA" > $fn_t
cat >> $fn_t


str="$fn_s"
info="$fn_i"

tmp=tmp_showtimes_$$
str2=${tmp}_str2
info2=${tmp}_info2
err=${tmp}_err

\rm -f $err

_err() {
	echo "ERROR: $*" >&2
	echo "ERROR: $*" >> $err
}

_get_str() {
	typeset line
	typeset label=""
	typeset text=""
	while read line
	do
		if [[ $line = @A* ]] ; then
			if [[ -n $label ]] ; then
				_err "label with no text: '$label'"
			fi
			label="${line#@A}"
		elif [[ $line = @B* ]] ; then
			text="${line#@B}"
			if [[ -z $label ]] ; then
				_err "text with no label: '$text'"
			fi
			echo "$label$text"
			label=""
			text=""
		else
			_err "Bad line in $str: '$line'"
		fi
	done
	if [[ -n $label ]] ; then
		_err "label with no text: '$label'"
	fi
}

cat $str | \
	sed \
		-e 's|[^!-~]| |g' \
		-e 's|[ 	][ 	]*| |g' \
		-e 's|^w_str.*:|@A&|' \
		-e 's| db "\([^"]*\).0"|@B&|' | \
	_get_str > $str2


_get_info() {
	
	typeset cur=""
	typeset t0=""
	typeset t
	typeset h
	typeset m
	typeset a
	typeset b
	typeset c
	typeset d
	echo "<ul>"
	while read a b c d
	do
		if [[ $a = @C ]] ; then
			if [[ -n $cur ]] ; then
				_err "Spell $cur is not terminated!!"
			fi
			echo "</ul>"
			echo "<h2>Spell $b</h2>"
			echo "<ul>"
			cur="$b"
		elif [[ $a = @D ]] ; then
			:
		elif [[ $a = @E ]] ; then
			if [[ -z $cur ]] ; then
				_err "Info is not in a spell: $a $b $c $d"
			fi
			if (( c & 0x80 )) ; then
				cur=""
			fi
			(( h = b + 0 ))
			(( m = c & 0x3f ))
			if (( m < 10 )) ; then
				m="0$m"
			fi
			if (( h < 24+6 )) ; then
				t="BEFORE GAME BEGINS"
			elif (( h < 24+12 )) ; then
				(( h = h - 24 ))
				t="$h:$m am Saturday"
			elif (( h < 24+13 )) ; then
				(( h = h - 24 ))
				t="$h:$m noon Saturday"
			elif (( h < 24+24 )) ; then
				(( h = h - 24 - 12 ))
				t="$h:$m pm Saturday"
			elif (( h < 24+25 )) ; then
				(( h = h - 24 - 12 ))
				t="$h:$m minight Saturday/Sunday"
			elif (( h < 24+24+12 )) ; then
				(( h = h - 24 - 24 ))
				t="$h:$m am Sunday"
			elif (( h < 24+24+13 )) ; then
				(( h = h - 24 - 24 ))
				t="$h:$m noon Sunday"
			elif (( h < 24+24+24 )) ; then
				(( h = h - 24 - 24 - 12 ))
				t="$h:$m pm Sunday"
			elif (( h == 255 )) ; then
				t=""
			else 
				t=""
			fi

			if [[   -z $t0 && -z $t ]] ; then
				echo "<li>ALWAYS:"
			elif [[ -n $t0 && -z $t ]] ; then
				echo "<li>After $t0:"
			elif [[ -z $t0 && -n $t ]] ; then
				echo "<li>Before $t:"
			else
				echo "<li>After  $t0<br>"
				echo "Before $t:"
			fi

			echo -n "<pre>                "
			grep "^$d:" $str2
			echo -n "</pre>"
		else
			_err "Bad line in $info: '$a $b $c $d'"
		fi
	done
	echo "</ul>"
}

cat $info | \
	sed \
		-e 's|[^!-~]| |g' \
		-e 's|[ 	][ 	]*| |g' \
		-e 's|^w_spellinfo_\([^:]*\)1:|@C \1|' \
		-e 's|^w_spellinfo.*equ.*|@D|' \
		-e 's| *;.*|@D|' \
		-e 's|^ db \(0x[0-9a-fA-F]*\) *, *\(0x[0-9a-fA-F]*\) *, *HIGH \(w_str_[^ ,]*\) *,.*|@E \1 \2 \3|' | \
	_get_info > $info2



len=`wc -l $fn_t`
if (( len > 10 )) ; then
	fn="xxxx"
	echo "Uploaded $len line file $fn"
	echo "<pre>"
	head -10 $fn_t
	echo "</pre>"
fi



echo "<h1>Upload new wspell_info.inc file</h1>"


    echo "<form method=\"post\" enctype=\"multipart/form-data\" action=showtimes>"
    echo "File:<input type=file id=\"upfile\" name=\"upfile\">"
    echo "<input type=submit>"
    echo "</form>"
    echo "</p>"
    echo "<p>NOTE: After clicking 'Submit' please wait until you see the 'Success' screen and confirm the file length.  "
    echo "(Do NOT repeatedly click the 'Submit' button.)</p>"


echo "<h1>Upload new wspell_str.inc file</h1>"

    echo "<form method=\"post\" enctype=\"multipart/form-data\" action=showtimes>"
    echo "File:<input type=file id=\"upfile\" name=\"upfile\">"
    echo "<input type=submit>"
    echo "</form>"
    echo "</p>"
    echo "<p>NOTE: After clicking 'Submit' please wait until you see the 'Success' screen and confirm the file length.  "
    echo "(Do NOT repeatedly click the 'Submit' button.)</p>"








if [[ -s $err ]] ; then
	echo "<h1>Errors Occurred</h1>"
	cat $err
else
	echo "<p>(No errors)</p>"
fi
echo "<h1>Results</h1>"
cat $info2

echo "</body>"
echo "</html>"

\rm -f $str2 $info2
