#!/usr/bin/ksh
#
# Name: fvt-cleanup
#
# Purpose:
#    To cleanup the $HOME/fvt directory of the files generated
#    by the other shell scripts.
#    It also cleans up the $HOME/queue subdirectories.
#
# Notes:
#
# 1) This script can be executed before repeating the testing cycle.
#
# 2) To invoke this shell script and redirect standard output and
#    standard error to a file (such as fvt-cleanup) do the
#    following (the -s flag is "silent mode" to avoid prompts to the
#    user):
#
#    fvt-cleanup -s   2>&1  | tee fvt-cleanup.out
#
######################################################################.#########

# ----------------------------
# Subroutine to print the usage
# ----------------------------

usage()
{
 print "USAGE: fvt-cleanup [-h] [-s]"
 print "WHERE: -h = help       -s = silent (no prompts)"
 print "PREREQUISITES:"
 print " - The directory, fvt, must exist relative to the CMVC family "
 print "   account's home directory (such as /home/cmfvt/fvt)"
 print " - Run fvt-cleanup from the fvt directory as the CMVC family administrator."
 print "fvt-cleanup terminated with rc=1."
 exit 1
}

# ----------------------------------
# Subroutine to terminate abnormally
# ----------------------------------

terminate()
{
 print "The cleanup action for the CMVC family directory was not successful."
 print "fvt-cleanup terminated, exiting now with rc=1."
 exit 1
}

################################################################################

# --------------------------------------------
# Main routine for populating the CMVC family
# --------------------------------------------

CALLER=`basename $0`                    # The Caller name
SILENT="no"                             # User wants prompts
let "errorCounter = 0"

# ----------------------------------
# Handle keyword parameters (flags).
# ----------------------------------

set -- `getopt hs $* 2>/dev/null`       # -h = help & -s = silent

if [ $? != 0 ]
then
 print "Unknown flag(s)"
 usage
fi

while [ "$1" != -- ]                    # While $1 is not the termination char
 do
  case "$1" in
   -h) usage "HELP";            shift;; # Help requested
   -s) SILENT="yes";            shift;; # Prompt is not needed
  esac
 done

shift                                   # Shift pass --

# --------------------------------------------------
# Everything seems to be OK, prompt for comfirmation
# --------------------------------------------------

if [ "$SILENT" = "yes" ]
then
 RESPONSE="y"
else
 print "The $HOME/fvt directory will be cleaned up."
 print "Do you wish to proceed [y or n]? "
 read RESPONSE                         # Wait for response
 [ -z "$RESPONSE" ] && RESPONSE="n"
fi

case "$RESPONSE" in
 [yY]|[yY][eE]|[yY][eE][sS])
 ;;
 *)
  print "$CALLER terminated with rc=1."
  exit 1
 ;;
esac

# --------------------------------------------------
print "Subject: CMVC 2.3.1, FVT cleanup"
dateTest=`date`
print "Begin testing at: $dateTest"
print ""
print "Family: $CMVC_FAMILY"
print "Testcase: fvt-cleanup"
print ""
# --------------------------------------------------

# --------------------------------------------------
print ""
print "Cleaning the fvt directory"
print ""
# --------------------------------------------------
cd $HOME/fvt
rm -f copyright.h
rm -f demoprog.mk 
rm -f main.c 
rm -f notes.ProjectA 
rm -f .notes.ProjectA 
rm -f softtar.c 
rm -f .softtar.c 
rm -f softtar.mk 
rm -f .softtar.mk 
rm -f softtar.tar 
rm -f .softtar.tar 
rm -f sqri.c 
rm -f report.exe 
rm -f "File with SCCS keywords.txt"
rm -f ".File with SCCS keywords.txt"
rm -fr superHello
rm -fr extract

# --------------------------------------------------
print ""
print "Cleaning the queue and messages directories"
print ""
# --------------------------------------------------
cd $HOME/queue
rm i*
cd $HOME/queue/messages
rm *

# --------------
# Exit
# --------------
if [ $errorCounter -ne 0 ]
then
 print ""
 print "*** $errorCounter ERRORS found during the execution of this test case."
 terminate
fi

print ""
print "fvt-cleanup complete."

exit 0

# end of file
