#!/bin/ksh
#
# fixbug
#

if [ "`whoami`" != "axlbugs" ] ; then
	echo Please log as axlbugs@axiomxl to fix a bug
	exit -1 
fi

if [ "`hostname | sed -e 's/\..*//'`" != "axiomxl" ] ; then
	echo Please log as axlbugs@axiomxl to fix a bug
	exit -1 
fi


fn=""

while [ "$fn" = "" ]
do
        echon "Name of bug file: "
        read fn
        if [ ! -w "$fn" ]; then
                fnaxl=$fn'.as'
                if [ ! -w "$fnaxl" ]
                then
                        echon "\tNeither $fn nor $fnaxl is a writable file."
			echo
                        fn=""
                else
                        fn=$fnaxl
                fi
                continue
        fi
        fnbase=`basename $fn`
        typeset -L3 fnpref=$fnbase
        if [ ! "$fnpref" = "bug" ]; then
                echon "\tName of bug file must begin with \"bug\"."
		echo
                fn=""
        fi
done

fn0=`basename $fn .as`

echo "Description of $fn: "
egrep "^$fn0" Log
echo " "

# a check for directory existence

fnbase=`basename $fn`
fndir=`dirname $fn`
fixbase=`echo $fnbase | sed -e 's/^bug/fix/'`
fixdir="$fndir/fixes"

if [ ! -d "$fixdir" ]; then
        echon "\tRequired directory $fixdir does not exist. Terminating."
	echo
        exit 1
fi

fix=$fixdir'/'$fixbase

initials=""
while [ "$initials" = "" ]
do
        echon "Your initials: "
        read initials
        if [ "$initials" = "" ]; then
                echon "\tYour initials are required."
		echo
        else
                # Now for that personal touch

                typeset -u initials=$initials

                echon "\tThanks, "

                if   [ "$initials" = "RSS" ]; then
                        echo "Bob"
                elif [ "$initials" = "SMW" ]; then
                        echo "Stephen"
                elif [ "$initials" = "SSD" ]; then
                        echo "Sam"
                elif [ "$initials" = "SCM" ]; then
                        echo "Scott"
                elif [ "$initials" = "JMS" ]; then
                        echo "Jon"
                elif [ "$initials" = "BMT" ]; then
                        echo "Barry"
                elif [ "$initials" = "PAB" ]; then
                        echo "Peter"
                elif [ "$initials" = "PI"  ]; then
			echo "Pietro"
			initials = "PI "
                else
                        echo "most mysterious $initials"
                fi
        fi
done


descript=""
while [ "$descript" = "" ]
do
        echo "Description of fix (double escape special characters): "
        read descript
        if [ "$descript" = "" ]; then
                echon "\tA description is required."
		echo
        fi
done

docfn=""
while [ "$docfn" = "" ]
do
        echon "Name of file with detailed description (default none): "
        read docfn
        if [ "$docfn" = "" ]
        then
                docfn=/dev/null
        elif [ ! -r "$docfn" ]
        then
                echon "\t$docfn is not a readable file."
		echo
                docfn=""
        fi
done


testfile=""
while [ "$testfile" = "" ]
do
        echon "Name of new or existing file in test directory: "
        read testfile
        if [ "$testfile" = "" ]; then
                echon "\tA testfile name is required."
		echo
        fi
done


datetime=`date`

echon "\tFix by $initials at $datetime"
echo
echon "\tCreating fix file $fix"
echo

awkcmds='/tmp/fixbug.awk'
rm -f $awkcmds

# Generate awk command file

echo "# file generated by fixbug" > $awkcmds

echo " " >> $awkcmds
echo "/^--@ Fixed  by:/ {" >> $awkcmds
echo "    print \"--@ Fixed  by: " $initials " " $datetime "\"" >> $awkcmds
echo "    next" >> $awkcmds
echo "}" >> $awkcmds

echo " " >> $awkcmds
echo "/^--@ Tested by:/ {" >> $awkcmds
echo "    print \"--@ Tested by: " $testfile "\"" >> $awkcmds
echo "    next" >> $awkcmds
echo "}" >> $awkcmds

echo " " >> $awkcmds
echo "/^--@ Summary:/ {" >> $awkcmds
echo "    print \"--@ Summary:   " $descript "\"" >> $awkcmds
echo "    next" >> $awkcmds
echo "}" >> $awkcmds

echo " " >> $awkcmds
echo "                  {" >> $awkcmds
echo "    print" >> $awkcmds
echo "    next" >> $awkcmds
echo "}" >> $awkcmds

awk -f $awkcmds < $fn > $fix

if [ $? -ne 0 ]; then
        echon "\tCould not generate fix file for some reason. Terminating."
	echo
        exit 1
fi

echo ' ' >> $fix
sed -e 's/^/--+ /' < $docfn >> $fix

rm -f $awkcmds
echon "\tRemoving $fn."
echo
rm $fn

if [ $? -ne 0 ]; then
        echon "\tCould not remove $fn for some reason. Terminating."
	echo
        exit 1
fi

echon "\tUpdating  Log  file."
echo
fixed="$fn0  Fixed $initials     :"
cp -p Log Log0
cat Log0 | sed -e "s/^$fn0[^:]*:/$fixed/g" > Log
rm -f Log0

# notify bug reporter of fix

fixreply $fix
exit 0
