#!/usr/bin/ksh
#
# Name: fvt-server-2
#
# Purpose:
#    Tests the main functions for a CMVC server, such
#    as vcPath, resetAge, age, audit/log, chf*, cmvcd, notifyd,
#    stopCMVC, etc.
#    Performs Release and Level extracts with nomount option
#    to avoid complications due to NFS configuration/setup.
#
# Notes:
#
# 1) This script should be run after: fvt-server-1
#
# 2) This script can only be issued from the CMVC family user id.
#
# 3) To invoke this shell script and redirect standard output and
#    standard error to a file (such as fvt-server-2.out) do the
#    following (the -s flag is "silent mode" to avoid prompts to the
#    user):
#
#    ./fvt-server-2  -s  2>&1  | tee fvt-server-2.out
#
# Return codes:                                                   
#  0 = All commands were successful                               
#  1 = At least one command failed, see the output file and search
#      for the keyword "ERROR".                                   
#
#############################################################.#########

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

usage()
{
 print "USAGE: fvt-server-2 [-h] [-s]"
 print "WHERE: -h = help       -s = silent (no prompts)"
 print "PREREQUISITES:"
 print " - The CMVC family to be used should be only for FVT purposes."
 print " - "fvt-server-1" should have been used to populate this CMVC family "
 print " - The workstation login of the CMVC family must have"
 print "   client access to the family as well as superuser privilege."
 print " - You must be logged in to the workstation login of the CMVC family."
 print " - Run fvt-server-2 from the fvt directory as the CMVC family administrator."
 print " - The CMVC_FAMILY environment variable must be set. "
 print " - The CMVC_HOME environment variable must be set. (such as /usr/lpp/cmvc )"
 print " - The DATABASE environment variable must be set to ORACLE, INFORMIX,"
 print "   SYBASE or DB2."
 print " - The directory, fvt, must exist relative to the CMVC family "
 print "   account's home directory (such as /home/cmfvt/fvt)"
 print "fvt-server-2: exiting now with rc=1."
 exit 1
}

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

terminate()
{
 print "The handling of CMVC server actions for the CMVC family was not successful."
 print "Recreating the family may be necessary before running fvt-server-2 again,"
 print "that is, you must use 'rmdb', 'rmfamily', 'mkfamily' and 'mkdb -d'. "
 print "fvt-server-2 terminated, exiting now with rc=1."
 dateTest=`date`
 print "End of testing at: $dateTest"
 print ""
 exit 1
}

# ---------------------------------------------------------------------------
# CMVC commands are called in a subroutine so that return code can be
# checked for possible errors.
# ---------------------------------------------------------------------------

Ager()
{
if [ "$DATABASE" = "ORACLE" ]
then
   $CMVC_HOME/bin/age $CMVC_SUPERUSER $ORACLE_PASS &
   sleep 2
   $CMVC_HOME/bin/age $CMVC_SUPERUSER $ORACLE_PASS &
fi
if [ "$DATABASE" = "SYBASE" ]
then
   $CMVC_HOME/bin/age $CMVC_SUPERUSER $SYBASE_PASS &
   sleep 2
   $CMVC_HOME/bin/age $CMVC_SUPERUSER $SYBASE_PASS &
fi
if [ "$DATABASE" = "INFORMIX" ]
then
   $CMVC_HOME/bin/age $CMVC_SUPERUSER &
   sleep 2
   $CMVC_HOME/bin/age $CMVC_SUPERUSER &
fi
if [ "$DATABASE" = "DB2" ]
then
   $CMVC_HOME/bin/age $CMVC_SUPERUSER &
   sleep 2
   $CMVC_HOME/bin/age $CMVC_SUPERUSER &
fi
}

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

# --------------------------------------------
# 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 --

# ------------------------------------------------
# The following CMVC environment variables must be set
# ------------------------------------------------

[ -z "$CMVC_FAMILY" ] && { print "The environment variable CMVC_FAMILY is not set."; usage; }
[ -z "$CMVC_HOME" ] && { print "The environment variable CMVC_HOME is not set."; usage; }

# ------------------------------------------------
# The DATABASE environment variable must be set
# ------------------------------------------------

[ -z "$DATABASE" ] && { print "The environment variable DATABASE is not set."; usage; }
case "$DATABASE" in
   ORACLE) continue ;;
   INFORMIX) continue ;;
   SYBASE) continue ;;
   DB2) continue ;;
   *) print "The environment variable DATABASE must be set to one of ORACLE, INFORMIX, DB2 or SYBASE."; usage;
      ;;
esac

#---------------------------------------
# What is the system login?
#---------------------------------------

THISUSERID=$LOGNAME

#---------------------------------------
# Setup the CMVC user ID
#---------------------------------------

CMVC_BECOME=$THISUSERID
export CMVC_BECOME

# ---------------------------------------------------
# Make sure that this user can access the CMVC family
# ---------------------------------------------------

Report -testServer > /dev/null
if [ $? -ne 0 ]
then
 print "Cannot access the CMVC family $CMVC_FAMILY"
 usage
fi

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

if [ "$SILENT" = "yes" ]
then
 RESPONSE="y"
else
 print "Family $CMVC_FAMILY will be populated with FVT objects."
 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 testing, Server, Part 2"
dateTest=`date`
print "Begin testing at: $dateTest"
print ""
print "Database: $DATABASE"
print "Family:   $CMVC_FAMILY"
print "Testcase: fvt-server-2"
print ""
# --------------------------------------------------

# --------------------------------------------------
print ""
print "Performing a release extract, current, to $HOME/fvt/extract/release/current"
# --------------------------------------------------
mkdir -p $HOME/fvt/extract/release/current
Release -extract track-level-1.2 -node nomount -root $HOME/fvt/extract/release/current \
        -verbose
if [ $? -ne 0 ]
then
 print "ERROR found in Release -extract track-level-1.2, current"
 let "errorCounter = errorCounter + 1"
else
 print "The extracted files are:"
 ls -al $HOME/fvt/extract/release/current/*
fi

# --------------------------------------------------
print ""
print "Performing a release extract, committed, to $HOME/fvt/extract/release/committed"
# --------------------------------------------------
mkdir -p $HOME/fvt/extract/release/committed
Release -extract track-level-1.2 -node nomount -root $HOME/fvt/extract/release/committed \
        -committed -verbose
if [ $? -ne 0 ]
then
 print "ERROR found in Release -extract track-level-1.2, committed"
 let "errorCounter = errorCounter + 1"
else
 print "The extracted files are:"
 ls -al $HOME/fvt/extract/release/committed/*
fi

# --------------------------------------------------
print ""
print "Performing a release extract, by date, to $HOME/fvt/extract/release/bydate"
print "The date is: 1999/01/01"
# --------------------------------------------------
mkdir -p $HOME/fvt/extract/release/bydate
Release -extract track-level-1.2 -node nomount -root $HOME/fvt/extract/release/bydate \
        -date 1999/01/01 -verbose
if [ $? -ne 0 ]
then
 print "ERROR found in Release -extract track-level-1.2, by date"
 let "errorCounter = errorCounter + 1"
else
 print "The extracted files are:"
 ls -al $HOME/fvt/extract/release/bydate/*
fi

# --------------------------------------------------
print ""
print "Performing a level extract, delta, to $HOME/fvt/extract/level/delta"
# --------------------------------------------------
mkdir -p $HOME/fvt/extract/level/delta
Level -extract 02  -release track-level-1.2 -node nomount \
      -root $HOME/fvt/extract/level/delta
if [ $? -ne 0 ]
then
 print "ERROR found in Level -extract 02 for release track-level-1.2, delta"
 let "errorCounter = errorCounter + 1"
else
 print "The extracted files are:"
 ls -al $HOME/fvt/extract/level/delta/*
fi

# --------------------------------------------------
print ""
print "Performing a level extract, full, to $HOME/fvt/extract/level/full"
# --------------------------------------------------
mkdir -p $HOME/fvt/extract/level/full
Level -extract 03  -release track-level-1.2 -node nomount -full \
      -root $HOME/fvt/extract/level/full
if [ $? -ne 0 ]
then
 print "ERROR found in Level -extract 03 for release track-level-1.2, full"
 let "errorCounter = errorCounter + 1"
else
 print "The extracted files are:"
 ls -al $HOME/fvt/extract/level/full/*
fi


# --------------------------------------------------
print ""
print "Aging defects and features ..."
# --------------------------------------------------

Ager 
sleep 2
Ager

# --------------------------------------------------
print ""
print "============================================================================="
print ""
# --------------------------------------------------

# --------------------------------------------------
print ""
print "Listing those defects that can be aged (open state)."
# --------------------------------------------------
Report -view DefectView -where "state in ('open')"
if [ $? -ne 0 ]
then
 print "ERROR found in Repor -view DefectView"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Executing the 'age' tool"
# --------------------------------------------------
# Invoking the Ager() function, which calls 'age'
Ager

# --------------------------------------------------
print ""
print "Listing those defects that were aged (open state)."
# --------------------------------------------------
Report -view DefectView -where "state in ('open')"
if [ $? -ne 0 ]
then
 print "ERROR found in Repor -view DefectView"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Executing resetAge for workweek"
# --------------------------------------------------
resetAge workweek
if [ $? -ne 0 ]
then
 print "ERROR found in resetAge workweek"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Listing those defects whose age was reseted (open state)."
# --------------------------------------------------
Report -view DefectView -where "state in ('open')"
if [ $? -ne 0 ]
then
 print "ERROR found in Repor -view DefectView"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Executing vcPath for file softtar.c, in release track-level-1.1"
# --------------------------------------------------
vcPath softtar.c track-level-1.1
if [ $? -ne 0 ]
then
 print "ERROR found in vcPath softtar.c track-level-1.1"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Showing the last 10 entries in the audit/log"
# --------------------------------------------------
tail -10 $HOME/audit/log

# --------------------------------------------------
print ""
print "Using the cmvchost command"
# --------------------------------------------------
cmvchost $SITE
if [ $? -ne 0 ]
then
 print "ERROR found in cmvchost"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Stopping the CMVC family"
# --------------------------------------------------
stopCMVC

# --------------------------------------------------
print ""
print "Reloading the *.ld files: chauth"
# --------------------------------------------------
chauth -s
if [ $? -ne 0 ]
then
 print "ERROR found in chauth"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Reloading the *.ld files: chintr"
# --------------------------------------------------
chintr -s
if [ $? -ne 0 ]
then
 print "ERROR found in chintr"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Reloading the *.ld files: chcfg"
# --------------------------------------------------
chcfg -s
if [ $? -ne 0 ]
then
 print "ERROR found in chcfg"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Reloading the *.ld files: chcomproc"
# --------------------------------------------------
chcomproc -s
if [ $? -ne 0 ]
then
 print "ERROR found in chcomproc"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Reloading the *.ld files: chrelproc"
# --------------------------------------------------
chrelproc -s
if [ $? -ne 0 ]
then
 print "ERROR found in chrelproc"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Restarting the family: cmvcd"
# --------------------------------------------------
cmvcd $LOGNAME 2
if [ $? -ne 0 ]
then
 print "ERROR found in cmvcd"
 let "errorCounter = errorCounter + 1"
fi

# --------------------------------------------------
print ""
print "Restarting the family: notifyd"
# --------------------------------------------------
notifyd
if [ $? -ne 0 ]
then
 print "ERROR found in notifyd"
 let "errorCounter = errorCounter + 1"
fi

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

print ""
print "It is suggested to execute manually the following commands:"
print "  monitor 0"
print "  chfield -object Defect"

print ""
print "fvt-server-2 complete."
dateTest=`date`
print "End of testing at: $dateTest"
print ""

exit 0

# end of file
