#! /bin/sh

# Basic sanity checks: Inform the curious user what is this script for

if [ $# -eq 0 ]
	then 
		echo "\nERROR: should have 0 or 1 command-line parameters";
		# ERROR - no tool specified
		JSTART_EXITCODE=13;
		echo "FATAL: No tool specified for launch";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

if [ $1 = "-?" ]
	then
		# ERROR - the script parameters are totaly wrong
		JSTART_EXITCODE=10;
		echo "\n$0 : This helper script is not designed to be run directly";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

if [ $1 = "--?" ]
	then
		# ERROR - the script parameters are totaly wrong
		JSTART_EXITCODE=10;
		echo "\n$0 : This helper script is not designed to be run directly";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

if [ $1 = "-h" ]
	then
		# ERROR - the script parameters are totaly wrong
		JSTART_EXITCODE=10;
		echo "\n$0 : This helper script is not designed to be run directly";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

if [ $1 = "--h" ]
	then
		# ERROR - the script parameters are totaly wrong
		JSTART_EXITCODE=10;
		echo "\n$0 : This helper script is not designed to be run directly";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

if [ $1 = "-help" ]
	then
		# ERROR - the script parameters are totaly wrong
		JSTART_EXITCODE=10;
		echo "\n$0 : This helper script is not designed to be run directly";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

if [ $1 = "--help" ]
	then
		# ERROR - the script parameters are totaly wrong
		JSTART_EXITCODE=10;
		echo "\n$0 : This helper script is not designed to be run directly";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

# Get the location of the script and return to the base directory
CURRENT_DIR=`pwd`
RELATIVE_DIR=`dirname $0`
cd $RELATIVE_DIR
SCRIPT_LOCATION=`pwd`
cd $CURRENT_DIR

# Check the input parameters
NODE_NAME=$1

if [ ".$NODE_NAME" = "." ]
	then
		echo "\nERROR: should have 0 or 1 command-line parameters";
		# ERROR - no tool specified
		JSTART_EXITCODE=13;
		echo "FATAL: No tool specified for launch";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

STARTUP_FILE=$2

if [ ".$STARTUP_FILE" = "." ]
	then
		STARTUP_FILE="$SCRIPT_LOCATION/../../../../exe/startup.properties";
	else
		PARAMETERS=$3;
fi

if [ ! -r $STARTUP_FILE ]
	then
		# ERROR - startup file not found
		JSTART_EXITCODE=11;
		echo "\nFATAL: Cannot read startup file: [$STARTUP_FILE]";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

# Old Working Logic
#INSTANCE_NUMBER=`echo $SCRIPT_LOCATION | sed -e 's^.*\([0-9][0-9]\)/j2ee/cluster/bootstrap/scripts^\1^'`
#INSTANCE_NAME=`echo $SCRIPT_LOCATION | sed -e 's^.*/\([A-Z]*[0-9][0-9]\)/j2ee/cluster/bootstrap/scripts^\1^'`


# New working logic
INSTANCE_NUMBER=`expr match "$SCRIPT_LOCATION" '.*\([0-9][0-9]\)/j2ee/cluster/bootstrap/scripts'`
INSTANCE_NAME=`expr match "$SCRIPT_LOCATION" '.*/\([A-Z]*[0-9][0-9]\)/j2ee/cluster/bootstrap/scripts'`

# Get the profile path
PROFILE=`$SCRIPT_LOCATION/../../../../exe/sapcontrol -nr $INSTANCE_NUMBER -prot NI_HTTP -function ParameterValue SAPPROFILE_IN_EFFECT`

# Get the profile status
PROFILE_STATUS=`expr match "$PROFILE" '.*\(OK\).*'`

if [ "$PROFILE_STATUS" != "OK" ]
	then
		# ERROR - the sapstartsrv is not started
		JSTART_EXITCODE=16;
		echo "\nFATAL: SAPSTART service for instance [$INSTANCE_NAME] is not started";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

# Get the full path to the profile
PROFILE=`expr match "$PROFILE" '.*\(/usr/sap/.../SYS/profile/..._[A-Z]*[0-9][0-9]_.*\)'`

if [ ! -r $PROFILE ]
	then
		# ERROR - the profile does not exist
		JSTART_EXITCODE=15;
		echo "\nFATAL: Cannot read profile: [$PROFILE]";
		exit $JSTART_EXITCODE;
		# This newline must remain
fi

# Launch the tool via JSF
cd $SCRIPT_LOCATION
$SCRIPT_LOCATION/../../../../exe/jstart -launch -nodename=$NODE_NAME -file=$STARTUP_FILE pf=$PROFILE $PARAMETERS
JSTART_EXITCODE=$?
cd $CURRENT_DIR

#Display the output
OUTPUT_FILE=$SCRIPT_LOCATION/../../../../work/jvm_${NODE_NAME}.out

if [ -r $OUTPUT_FILE ]
	then
		cat $OUTPUT_FILE;
		echo "\nNOTE: This output is taken from log file [$OUTPUT_FILE]";
fi

if [ "$JSTART_EXITCODE" != "0" ]
	then
		echo "\nERROR: JStart exit code [$JSTART_EXITCODE], see details in log file [${SCRIPT_LOCATION}/dev_$NODE_NAME]";
fi

# End of the script
exit $JSTART_EXITCODE
# This newline must remain