#!/bin/csh -f
#
# $Header: /u/04c/mnt/integ/tools/src/RCS/bugs,v 1.9 89/08/29 16:05:27 serpent Exp $
#
# Serpent Problem Reporting Script
#
# Usage:
#   report_bug [ -f | -b ]
#
#   -b   Report a bug.
#   -f   Report a bug fix.
#
#   The options are mutually exclusive.  If neither is specified, you
#   will be prompted for one.
#
#   You will be placed in your favorite text editor with a copy of the
#   appropriate form, which you should edit.  When you exit the editor, 
#   the script will ask you what to do with the edited form.  "Your 
#   favorite editor" is determining by examining the VISUAL environment
#   variable.  If VISUAL isn't set, the contents of the EDITOR environment
#   variable are used.  If EDITOR isn't set, Emacs is used.
# -------------------------------------

# Configuration constants

set TITLE            = "Serpent Problem Report Generator"
set REPORT_TEMPLATE  = $SERPENT_BASE_PATH/tools/src/SPR
set FIX_TEMPLATE     = $SERPENT_BASE_PATH/tools/src/SPC
set BUG_ACCOUNT      = "serpent-bugs@sei.cmu.edu"
set TEMP_FILE        = /tmp/$user.$$
set SAVE_FILE        = $home/serpent_bug.$$
set DATE_PATTERN     = "^Date submitted"

# Command-line constants

set OPT_BUG_REPORT   = "b"
set OPT_BUG_FIX      = "f"
set PROGRAM   = $0 ; set PROGRAM = $PROGRAM:t  # cheap basename(1)
set USAGE            = "Usage: $PROGRAM [-$OPT_BUG_REPORT | -$OPT_BUG_FIX]"


# Initialize

onintr abort
set editor = emacs  # the default editor

if ( $?VISUAL ) then
   if ("$VISUAL" != "") set editor = $VISUAL

else if ($?EDITOR) then
   if ("$EDITOR" != "") set editor = $EDITOR

else
   set editor = emacs
endif

# Process options

unset do_bug_report do_bug_fix formtype 
set more_options

while ($?more_options)
   switch ("$1")
      case -"$OPT_BUG_REPORT":
         set do_bug_report
         shift
         breaksw

      case -"$OPT_BUG_FIX":
         set do_bug_fix
         shift
         breaksw

      case -*:
         echo "$USAGE"
         exit 1
         breaksw

      default:
         # No more options
         unset more_options
         breaksw
   endsw
end

if ($#argv != 0) then
   echo "$USAGE"
   exit 1
endif

# Check option settings

if ( $?do_bug_fix && $?do_bug_report ) then
   echo "-$OPT_BUG_REPORT and -$OPT_BUG_FIX are mutually exclusive."
   unset do_bug_fix do_bug_report
   # Don't exit.  Prompt instead.  (See below.)
endif

if ( (! $?do_bug_fix) && (! $?do_bug_report) ) then
   # Prompt for it.
   unset okay
   echo ""
   echo "$TITLE"
   echo ""
   while (! $?okay)
      echo "${OPT_BUG_REPORT}: Report a bug."
      echo "${OPT_BUG_FIX}: Report a bug fix."
      echo ""
      echo "To abort, press the interrupt key followed by RETURN."
      echo -n "? "
      switch ("$<")
         case "$OPT_BUG_REPORT":
            set do_bug_report
            set okay
            breaksw

         case "$OPT_BUG_FIX":
            set do_bug_fix
            set okay
            breaksw
      endsw
   end # while
endif

# Now that all this setup stuff is out of the way, start the actual
# submission process.

if ($?do_bug_report) then
   set template = $REPORT_TEMPLATE
   set formtype = "Serpent bug report"
else
   set template = $FIX_TEMPLATE
   set formtype = "Serpent bug fix"
endif

set today = "`date '+%d %h %y'`"

# Weird quoting is necessary to get past stupid C-shell.

sed "/$DATE_PATTERN/s/"\$"/ $today/" <$template >$TEMP_FILE
if ($status != 0) then
   echo "Can't make a copy of template file $template."
   rm -f $TEMP_FILE
   exit 1

else if ( ! { chmod 600 $TEMP_FILE } ) then
   echo "Can't change mode on $TEMP_FILE"
   rm -f $TEMP_FILE
   exit 1
endif

echo "Preparing to edit a $formtype form."
echo ""
echo "Please fill in the template file.  When you are finished, write"
echo "out the file and exit the editor."
sleep 1

onintr -
$editor $TEMP_FILE
onintr abort

# Now, what does the user want to do?
unset done
while (! $?done)
   echo ""
   echo -n "S)ubmit report, E)dit report again, Q)uit and save, A)bort "
   switch("$<")
      case [Ee]:
         $editor $TEMP_FILE
         breaksw

      case [Ss]:
         echo "Mailing form to $BUG_ACCOUNT and $user"

         ( \
         echo "To: $BUG_ACCOUNT, $user" ;\
         echo "From: $user" ;\
         echo "Subject: $formtype" ;\
         echo "" ;\
         cat $TEMP_FILE ;\
         ) | /usr/lib/sendmail -t -om

         if ($status != 0) then
            echo "Can't submit report.  Saving it to file $SAVE_FILE"
            if ( ! { mv $TEMP_FILE $SAVE_FILE } ) then
               echo "Save failed.  File $TEMP_FILE contains the form"
               exit 1
            endif
         else
            echo "Submitted $formtype."
         endif
         set done
         breaksw

      case [Qq]:
         echo -n "Save form in what file? [$SAVE_FILE] "
         set save_file="$<"
         if ("$save_file" == "") set save_file=$SAVE_FILE
         if ( -f $save_file ) then
            echo "Appending timecard to $save_file"
            cat $TEMP_FILE >>$save_file
         else
            echo "Creating $save_file"
            # Use >> anyway.  It's safer.
            cat $TEMP_FILE >>$save_file
         endif
         set done
         breaksw

      case [Aa]:
         echo -n "Are you sure? [n] "
         if ("$<" =~ [Yy]*) then
            echo "Aborted.  Form was not saved."
            set done
         endif
         breaksw
   endsw
end # while

rm -f $TEMP_FILE
exit 0

abort:
echo "Aborting."
rm -f $TEMP_FILE
exit 1
