#!/bin/sh
#
# fixbug
# ToDo: Fix echo handling...
#

AXBUGS="ax-bugs@nag.co.uk"

#
# Collect name of bug to fix.
#

ARG=""
NUM=""
BUG=""

if [ $# -gt 0 ] ; then
	ARG="$1"
fi

while [ "${BUG}" = "" ]
do
	if [ "${ARG}" = "" ] ; then
		echon "Name of bug file:  "
		read ARG
	fi

	NUM="`echo ${ARG} | sed -e 's/^bug//' -e 's/\.as$//'`"
	case "${NUM}" in
	[0-9][0-9][0-9])	BUG="bug${NUM}.as"	;;
	[0-9][0-9][0-9][0-9])	BUG="bug${NUM}.as"	;;
	*)			BUG="" ; echon "\tIgnoring '${ARG}'."; echo ;;
	esac

	if [ ! -r "${BUG}" ] ; then
		echon "\t${BUG} is not a readable file." ; echo ; BUG=""
	fi
	ARG=""
done

#
# Echo the description if we have a Log file around.
#

if [ -r Log ] ; then
	echo "Description of ${BUG}:  "
	egrep "^bug${NUM}" Log
	echo
fi

#
# Collect the information for the fix report.
#

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

                echon "\tThanks, "

                if   [ "${INITIALS}" = "RSS" -o "${INITIALS}" = "rss" ]; then
                        echo "Bob"
                elif [ "${INITIALS}" = "SMW" -o "${INITIALS}" = "smw" ]; then
                        echo "Stephen"
                elif [ "${INITIALS}" = "SSD" -o "${INITIALS}" = "ssd" ]; then
                        echo "Sam"
                elif [ "${INITIALS}" = "SCM" -o "${INITIALS}" = "scm" ]; then
                        echo "Scott"
                elif [ "${INITIALS}" = "JMS" -o "${INITIALS}" = "jms" ]; then
                        echo "Jon"
                elif [ "${INITIALS}" = "BMT" -o "${INITIALS}" = "bmt" ]; then
                        echo "Barry"
                elif [ "${INITIALS}" = "PAB" -o "${INITIALS}" = "pab" ]; then
                        echo "Peter"
                elif [ "${INITIALS}" = "TTT" -o "${INITIALS}" = "ttt" ]; then
                        echo "Themos"
                elif [ "${INITIALS}" = "MND" -o "${INITIALS}" = "mnd" ]; then
                        echo "Martin"
                elif [ "${INITIALS}" = "PI"  -o "${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`

#
# Create the bug fix file.
#

FIX="/tmp/fix${NUM}.as"
AWK="/tmp/fix${NUM}.awk"
rm -f ${FIX} ${AWK}

echon "\tFix by ${INITIALS} at ${DATETIME}"
echo
echon "\tCreating fix file ${FIX}"
echo

touch ${AWK}
if [ ! -w ${AWK} ] ; then
	echon "\tCould not generate fix file."
	echo
	exit 0
fi

echo " " >> ${AWK}
echo "/^--@ Fixed  by:/ {" >> ${AWK}
echo "    print \"--@ Bug Number: " $BUG "\"" >> ${AWK}
echo "    print \"--@ Fixed  by: " ${INITIALS} " " $datetime "\"" >> ${AWK}
echo "    next" >> ${AWK}
echo "}" >> ${AWK}

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

echo " " >> ${AWK}
echo "/^--@ Summary:/ {" >> ${AWK}
echo "    print \"--@ Summary:   " ${DESCRIPT} "\"" >> ${AWK}
echo "    next" >> ${AWK}
echo "}" >> ${AWK}

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

echo "Subject: fixbug" > ${FIX}
echo "By: ${INITIALS}" >> ${FIX}
echo "Fixed: ${BUG}" >> ${FIX}

awk -f ${AWK} < ${BUG} >> ${FIX}

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

echo >> ${FIX}
sed -e 's/^/--+ /' < ${DOCFN} >> ${FIX}

#
# Send fixbug request to AXBUGS.
#
echo "Emailing ${AXBUGS} ${FIX} ..."
mail ${AXBUGS} < ${FIX}
echo "   ... done"

#
# Update the fix directory
#
echo "Copying fix into fixes"
cp ${FIX} fixes
rm -f ${FIX} ${AWK}
#
