#! /bin/sh  

########################################################################
#
# /u/sonmi/bin/jssqa
#
# this script is supposed to automatically run - now a sanity test, later QA for 
# JSS on all required Unix and Windows (NT and 2000) platforms
#
# parameters
# ----------
#   jssversion  (supported: 31, tip)
#   builddate   (default - today)
#
# options
# -------
#   -y answer all questions with y - use at your own risk...ignores warnings
#   -s silent (only usefull with -y)
#   -h, -? - you guessed right - displays this text
#   -d debug
#   -f <filename> - write the (error)output to filename
#   -cron equivalient to -y -s -d -f $RESULTDIR/$HOST.nssqa
#
########################################################################

O_OPTIONS=ON            # accept options (see above for listing)
WIN_WAIT_FOREVER=OFF    # don't wait for the NSS testdir
PRODUCT_TO_TEST="JSS"
JSS_NSPR_DIR="/share/builds/components/nspr20/v4.1.2"
JSS_NSS_DIR="/share/builds/components/nss/NSS_3_3_1_RTM"
JSS_NSS_UX_SRC_DIR="nss331/builds/20010928.2.331-RTM/booboo_Solaris8"
JSS_NSS_NT_SRC_DIR="nss331/builds/20010928.2.331-RTM/blowfish_NT4.0_Win95"
JSS_NSS_SRC_DIR=$JSS_NSS_UX_SRC_DIR
NATIVE_FLAG=""

. `dirname $0`/header   # utilities, shellfunctions etc, global to NSS and JSS QA

if [ -z "$O_TBX" -o "$O_TBX" != "ON" ] ; then
    is_running ${TMP}/jssqa
                        # checks if the file exists, if yes Exits, if not
                        # creates to implement a primitive locking mechanism
fi

INTERNAL_TOKEN="NSS Certificate DB"
SIGTEST_INTERNAL_TOKEN="Internal Key Storage Token"

################################ jss_init #########################
# 
# Most of the procedure is setting up the test environment.
# set all necessary dir and file variables, set all paths, copy the shared libs 
# Put all the shared libraries into a lib directory, <libdir>.
# including the libjss3.so that was built by the build process.
# set LD_LIBRARY PATH and CLASSPATH
# The xpclass.jar produced by the JSS build needs to be in the classpath.
# The classpath must also include the current directory so we can run our test
# programs.
################################################################################

jss_init()
{
  Debug "Jss init"
  #correct all directories that the header has set...
  NTDIST=`echo $NTDIST | sed -e 's/nss/jss/g'`
  UXDIST=`echo $UXDIST | sed -e 's/nss/jss/g'`
  RESULTDIR=`echo $RESULTDIR | sed -e 's/nss/jss/g'`
  mkdir -p ${RESULTDIR} 2>/dev/null
  JSS_LOGFILE=${RESULTDIR}/${HOST}.txt
  FILENAME=$JSS_LOGFILE
  O_FILE=ON

  MOZILLA_ROOT=`echo $MOZILLA_ROOT | sed -e 's/nss/jss/g'`

  JSS_SAMPLES="$MOZILLA_ROOT/security/jss/samples"
  JSS_CLASSPATH=`echo $MOZILLA_ROOT | 
      sed -e "s/jss$NSSVER.builds/jss$NSSVER\/ships/g" -e "s/mozilla/jss\/${QAYEAR}${BUILDDATE}/"`
  Debug "JSS_CLASSPATH=$JSS_CLASSPATH"
  Debug "JSS_SAMPLES=$JSS_SAMPLES"

  if [ ! -d $JSS_SAMPLES ] ; then
      if [ "$O_WIN" = "ON" -a "$WIN_WAIT_FOREVER" = "ON" ]
      then
          WaitForever $JSS_SAMPLES/TestKeyGen.java 1
      else
          Exit "Test directory $JSS_SAMPLES does not exist"
      fi
  fi

  PWFILE="$JSS_SAMPLES/passwd"
  EMPTYFILE="$JSS_SAMPLES/emptyfile"
  rm $PWFILE $EMPTYFILE 2>/dev/null
  echo "jss" >$PWFILE
  echo "" >$EMPTYFILE
  echo "" >>$EMPTYFILE
  echo "" >>$EMPTYFILE

  INIT_PATH=$PATH
  INIT_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
}


jss_mode_init()
{
  OBJDIR=`cd ${TESTSCRIPTDIR}/common; gmake objdir_name`

  LOCALDIST_BIN=`echo $LOCALDIST_BIN | sed -e 's/nss/jss/g'`
  LOCALDIST_LIB=$LOCALDIST_BIN/../lib
  debug_dirs

  #make testdir/libdir

  JSS_LIBDIR=${RESULTDIR}/${HOST}.libdir/${OBJDIR}
  mkdir -p ${JSS_LIBDIR} 2>/dev/null
  Debug "JSS_LIBDIR=$JSS_LIBDIR"

  #Put all the shared libraries into a lib directory
  Debug "copy all needed libs to ${JSS_LIBDIR}"
  cp $JSS_NSPR_DIR/${OBJDIR}/lib/* ${JSS_LIBDIR}
  cp $JSS_NSS_DIR/${OBJDIR}/lib/* ${JSS_LIBDIR}
  cp $LOCALDIST_LIB/libjss3.*  ${JSS_LIBDIR}
  #FIXME uncomment above

  if [ $O_DEBUG = ON ] ; then
      Debug "ls $JSS_LIBDIR"
      ls $JSS_LIBDIR
  fi

  #LD_LIBRARY_PATH=$INIT_LD_LIBRARY_PATH:${JSS_LIBDIR}
  LD_LIBRARY_PATH=${JSS_LIBDIR} #remove to avoid HP coredump
  CLASSPATH="$JSS_CLASSPATH/xpclass.jar:."

  SHLIB_PATH=${LD_LIBRARY_PATH}
  LIBPATH=${LD_LIBRARY_PATH}

  PATH=$JSS_NSPR_DIR/${OBJDIR}/bin:$JSS_NSS_DIR/${OBJDIR}//bin:$INIT_PATH
  Debug "PATH $PATH"
  Debug "LD_LIBRARY_PATH $LD_LIBRARY_PATH"
  Debug "CLASSPATH=$CLASSPATH"

  export CLASSPATH LD_LIBRARY_PATH SHLIB_PATH LIBPATH
  export TESTSCRIPTDIR COMMON
  export_dirs
}

  
################################ jss_test #########################
# 
# go into the build tree. cd to mozilla/security/jss/samples.
# Create NSS directories in this directory with modutil and set the password
#
#6. Create an alias for the "java" and "javac" commands. You'll need to set
#it to whatever version of the JDK you used to build on this platform. For
#example, 
      #alias java /share/builds/components/cms_jdk/AIX/1.3.0/jre/bin/java
        #alias javac /share/builds/components/cms_jdk/AIX/1.3.0/bin/javac
# instead $JAVA and $JAVAC
# 7. Compile the tests.
#####################################################################
jss_test()
{
  O_FILE=OFF
  Debug "JSS main test"
  #set -x
  cd $JSS_SAMPLES

  Debug "Cleaning  $JSS_SAMPLES"
  rm cert7.db key3.db 2>/dev/null

  Debug "echo | modutil -dbdir . -create -force"
  echo | modutil -dbdir . -create -force
  Debug "modutil returned $?"

  modutil -dbdir . -list

  Debug "echo | modutil -dbdir . -changepw \"$INTERNAL_TOKEN\" -newpwfile $PWFILE -force"
  modutil -dbdir . -changepw "$INTERNAL_TOKEN" -newpwfile $PWFILE  -force <$EMPTYFILE
  #modutil -dbdir . -changepw "$INTERNAL_TOKEN" -pwfile $PWFILE -newpwfile $PWFILE <$EMPTYFILE
  Debug "modutil returned $?"

  Debug "$JAVAC TestKeyGen.java"
  $JAVAC TestKeyGen.java
  Debug "$JAVAC TestKeyGen.java returned $?"

  Debug "$JAVAC SigTest.java"
  $JAVAC SigTest.java
  Debug "$JAVAC SigTest.java returned $?"
          
  echo "Starting new jss test on $HOST"  
  date

  # Run the actual tests

  Debug "$JAVA $NATIVE_FLAG TestKeyGen ." 
  $JAVA $NATIVE_FLAG TestKeyGen . 
  Debug "$JAVA TestKeyGen returned $?"

  Debug "$JAVA $NATIVE_FLAG SigTest . \"$SIGTEST_INTERNAL_TOKEN\""
  $JAVA $NATIVE_FLAG SigTest . "$SIGTEST_INTERNAL_TOKEN"
  Debug "$JAVA SigTest returned $?"

  O_FILE=ON
}

jss_init
jss_mode_init

if [ "$O_CRON" = "ON" -o "$O_WIN" = "ON" ]
then
    jss_test >>$JSS_LOGFILE  2>&1
else
    jss_test 2>&1 | tee -a $JSS_LOGFILE
fi
BUILD_OPT=1; export BUILD_OPT; Debug "BUILD_OPT $BUILD_OPT"
jss_mode_init
if [ "$O_CRON" = "ON" -o "$O_WIN" = "ON" ]
then
    jss_test >>$JSS_LOGFILE  2>&1
else
    jss_test 2>&1 | tee -a $JSS_LOGFILE
fi
Exit "jssqa completed. Done `uname -n` $QA_OS_STRING"
