#!/bin/ksh
#
# Copyright (c) NetManSys 1996
# All rights reserved. 


# Default installation directory
#
INSTALL_DIR=${CAT_INSTALL_DIR:=/opt/SUNWconn/AgtTester}
export INSTALL_DIR
TCLTK_DIR=/opt/SUNWtcl/7.5

FBPATH=$INSTALL_DIR/etc:$INSTALL_DIR/mib:$INSTALL_DIR/bin:$FBPATH:.
export FBPATH
LD_LIBRARY_PATH=${LD_LIBRARY_PATH:=/usr/lib:/usr/openwin/lib}
LD_LIBRARY_PATH=$INSTALL_DIR/shared:$TCLTK_DIR/sun4/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

# Usage
#
usage () {
echo $@
echo "Usage: testgen [-v] [-i] [-c] -p <project dir> [-t <CT file>] [-s <sample values file>] [-d <masterfile>]" >&2
}


# Check that given directory has correct mode according to current process
# The directory must be readable and searchable (i.e. rx).
# If the second parameter 'w' is given , this means that we also check
# that the directory is writable.
# If problem, exit with status 1.
#
# Call: chkDirMode <dir> [w]
#
chkDirMode () {
    if [ ! -d $1 ]
    then
	# Directory does not exist: nothing to say
	return 0
    fi
    if ( test ! -r $1 ) || ( test ! -x $1 ) || ( ( test $# -eq 2 ) && ( test "$2" = "w" ) && ( test ! -w $1 ) )
    then
	echo "[ERROR] Directory $1 cannot be accessed. Please chmod it." >&2
	exit 1
    fi
}


# Retrieve parameters
#
VERB_OPT=
INFO_OPT=
COMPIL_OPT=

while [ $# -gt 0 ]
do
	case $1 in
	-v)	VERB_OPT="-v"
		shift;;
	-i)	INFO_OPT="-i"
		shift;;
	-c)	COMPIL_OPT="-c"
		shift;;
	-p)	PROJ_DIR=$2
		shift 2;;
	-t)	CT_FILE_NAME=$2
		shift 2;;
	-s)	SAMPLE_VAL_FILE_NAME=$2
		shift 2;;
	-d)	MASTER_FILE_NAME=$2
		shift 2;;
	-*)	usage Illegal option $1
		exit 1;;
	*)	break;;
	esac
done


# Check project directory (use value of environment variable if required and
# possible)
#
if [ -z "$PROJ_DIR" ]
then
	if [ ! -z "$CAT_PROJECT_DIR" ]
	then
		PROJ_DIR="$CAT_PROJECT_DIR"
	else
		usage No project directory and no environment variable CAT_PROJECT_DIR
		exit 1
	fi
fi


# Set absolute file names for project (sub)directories
#
PROJ_MD_DIR=$PROJ_DIR/Metadata
PROJ_TEST_DIR=$PROJ_DIR/Tests
PROJ_RQ_DIR=$PROJ_TEST_DIR/Requests
PROJ_SC_DIR=$PROJ_TEST_DIR/Scenarios
PROJ_RF_DIR=$PROJ_TEST_DIR/References


# Add path to project metadata dir in FBPATH
#
FBPATH=.:$PROJ_MD_DIR:$FBPATH
export FBPATH


# Set values for parameters accoring to command-line
#
OUT_DIR=$PROJ_DIR
if [ ! -z "$CT_FILE_NAME" ]
then
	CT_FILE="$PROJ_MD_DIR/$CT_FILE_NAME"
fi
if [ ! -z "$SAMPLE_VAL_FILE_NAME" ]
then
	SAMPLE_VAL_FILE="$PROJ_MD_DIR/$SAMPLE_VAL_FILE_NAME"
fi
if [ ! -z "$MASTER_FILE_NAME" ]
then
	MASTER_FILE="$PROJ_MD_DIR/$MASTER_FILE_NAME"
fi


# Check existence of given parameters
#
if [ ! -d $PROJ_DIR ]
then
	usage Project directory does not exist
	exit 1
elif [ ! -d $PROJ_MD_DIR ]
then
	usage Project metadata directory does not exist
	exit 1
elif [ ! -z "$CT_FILE" ] && [ ! -a $CT_FILE ] && [ -z "$COMPIL_OPT" ]
then
	usage CT file does not exist
	exit 1
elif [ ! -z "$SAMPLE_VAL_FILE" ] && [ ! -a $SAMPLE_VAL_FILE ] && \
     [ -z "$COMPIL_OPT" ]
then
	usage Sample value file does not exist
	exit 1
elif [ ! -z "$MASTER_FILE" ] && [ ! -a $MASTER_FILE ]
then
	usage Masterfile does not exist
	exit 1
fi


# Provide default values for parameters if required and possible
#
if [ -z "$CT_FILE" ] && [ -z "$COMPIL_OPT" ]
then
	if [ -a $PROJ_MD_DIR/tct.tree ]
	then
		CT_FILE="$PROJ_MD_DIR/tct.tree"
	else
		usage No CT file and no default tct.tree file
		exit 1
	fi
fi
if [ -z "$MASTER_FILE" ]
then
	if [ -a $PROJ_MD_DIR/masterfile ]
	then
		MASTER_FILE="$PROJ_MD_DIR/masterfile"
	else
		usage No masterfile and no default masterfile file
		exit 1
	fi
fi
if [ -z "$SAMPLE_VAL_FILE" ] && [ -a $PROJ_MD_DIR/asnValues ] && \
   [ -z "$COMPIL_OPT" ]
then
	SAMPLE_VAL_FILE="$PROJ_MD_DIR/asnValues"
fi


# Check process rights for project subdirectories
#
if [ -z "$COMPIL_OPT" ]
then
	OUT_DIR_OPT="-o $OUT_DIR"
	CT_FILE_OPT="-t $CT_FILE"
	if [ ! -z "$SAMPLE_VAL_FILE" ]
	then
		SAMPLE_VAL_FILE_OPT="-s $SAMPLE_VAL_FILE"
	else
		echo [INFO] No sample values input file
	fi
fi
MASTER_FILE_OPT="-d $MASTER_FILE"

#########################################
# save old test files if exist
if [ -d $PROJ_DIR/Tests_old ]
then
	/usr/bin/rm -rf $PROJ_DIR/Tests_old
fi

cp -r $PROJ_DIR/Tests $PROJ_DIR/Tests_old

# delete old Scenarios, Requests Replies and References
/usr/bin/rm -f $PROJ_DIR/Tests/Scenarios/*.sc
/usr/bin/rm -f $PROJ_DIR/Tests/Requests/*.rq
/usr/bin/rm -f $PROJ_DIR/Tests/References/*.rf
/usr/bin/rm -f $PROJ_DIR/Tests/Replies/*.rp

# save requests created by user and their related files
/usr/bin/cp $PROJ_DIR/Tests_old/Scenarios/usr_*.sc $PROJ_DIR/Tests/Scenarios/ 2>/dev/null
/usr/bin/cp $PROJ_DIR/Tests_old/Requests/usr_*.rq $PROJ_DIR/Tests/Requests 2>/dev/null
/usr/bin/cp $PROJ_DIR/Tests_old/References/usr_*.rf $PROJ_DIR/Tests/References 2>/dev/null
/usr/bin/cp $PROJ_DIR/Tests_old/Replies/usr_*.rp $PROJ_DIR/Tests/Replies 2>/dev/null


# Execute call to fbtestgen for request/scenario/reference generation
#
$INSTALL_DIR/bin/fbtestgen $VERB_OPT $INFO_OPT $COMPIL_OPT $OUT_DIR_OPT $CT_FILE_OPT $SAMPLE_VAL_FILE_OPT $MASTER_FILE_OPT
$CMD_LINE

if [ $? != 0 ]
then
	/usr/bin/rm -rf $PROJ_DIR/Tests
	/usr/bin/cp -r $PROJ_DIR/Tests_old $PROJ_DIR/Tests
	/usr/bin/rm -rf $PROJ_DIR/Tests_old
	exit 1
fi

echo

if [ -z "$COMPIL_OPT" ]
then
	# Execute call to mktoc for TOC generation
	#
	$INSTALL_DIR/bin/mktoc -rs -proj $PROJ_DIR
fi

mktoc_result=$?

if [ $mktoc_result != 0 ]
then
	/usr/bin/rm -rf $PROJ_DIR/Tests 
	/usr/bin/cp -r $PROJ_DIR/Tests_old $PROJ_DIR/Tests 2>/dev/null
	/usr/bin/rm -rf $PROJ_DIR/Tests_old
fi

exit $mktoc_result
