#! /bin/ksh

#  (c) 1998-1999  Sun Microsystems, Inc.
#
#  @(#)sdtpdasync.ksh	1.19 00/05/31 1.19 Sun Microsystems, Inc.

PORT=

# Process arguments
while [[ $1 = -* ]]; do

        case $1 in
                -display )
                        shift
                        print "Setting display to: $1"
                        DISPLAY=$1
                        ;;

                -port )
			shift
			PORT="-port $1"
			;;

		-debug )
			shift
			DEBUG="-debug $1"
			;;

		-res_cmd )
			shift
			RESTART="-res_cmd $1"
			;;

                * )
                        print "Unexpected argument: $1"
        esac
        shift
done

# Save the current directory.
current_dir=`pwd`

version_error()
{
	echo "You need the Java 2 Runtime Environment installed in order to run
sdtpdasync."
	echo "Either install SUNWj2rt package or set JAVA_HOME environment varia
ble"
	echo "to the location of your Java 2 Runtime Environment Production Rele
ase"
	echo "installation directory."
	echo ""
	echo "Ex:  setenv JAVA_HOME /usr/Solaris_1.2.1_04  (csh)"
	echo "     export JAVA_HOME=/usr/Solaris_1.2.1_04  (ksh)"
	exit 1
}

# Check if java exists in the user's environment
: ${JAVA_HOME:=/usr/java1.2/jre}
if [ ! -a $JAVA_HOME ]; then
	version_error
fi

# Change directory to location of packages.
cd /usr/dt/appconfig/sdtpdasync

# Set the LD_LIBRARY_PATH
cd lib
LD_LIBRARY_PATH=`pwd`:/usr/dt/lib:${LD_LIBRARY_PATH}
cd ..

# Set the class path to include each and every jar file of the application
CLASSPATH=`pwd`:${CLASSPATH}
cd classes
CLASSPATH=`pwd`:${CLASSPATH}
jarfiles=`/bin/ls *.jar`
for i in ${jarfiles}; do
	CLASSPATH=`pwd`/${i}:${CLASSPATH}
done

# Set xservices (xsession) and java help jars.
	CLASSPATH=/usr/dt/classes/xservices.jar:${CLASSPATH}
	CLASSPATH=/usr/dt/classes/jhall.jar:${CLASSPATH}
	CLASSPATH=/usr/dt/classes/jsearch.jar:${CLASSPATH}
cd ..

# Set the class path to include each and every conduit jar file.
cd conduits
jarfiles=`/bin/ls *.jar`
for i in ${jarfiles}; do
	CLASSPATH=`pwd`/${i}:${CLASSPATH}
done
# LD_LIBRARY_PATH=`pwd`:${LD_LIBRARY_PATH}
cd ..

export LD_LIBRARY_PATH
export CLASSPATH

# Get the java version.
${JAVA_HOME}/bin/java -version 2>/tmp/sdtpdasync.version_test

java_ver=`awk -F\" '{ print $2 }' /tmp/sdtpdasync.version_test`

# Get the major.minor.subminor java version numbers.
major=`awk -F\" '{ print $2 }' /tmp/sdtpdasync.version_test | awk -F\. '{ print $1 }' `
minor=`awk -F\" '{ print $2 }' /tmp/sdtpdasync.version_test | awk -F\. '{ print $2 }' `
subminor=`awk -F\" '{ print $2 }' /tmp/sdtpdasync.version_test | awk -F\. '{ print $3 }' `

rm /tmp/sdtpdasync.version_test

# Start the pdasync application, based on the java version.
cd $current_dir
if (( ${minor} >= 2 )); then

	# JDK 1.2 or higher
	(cd /usr/dt/appconfig/sdtpdasync; ${JAVA_HOME}/bin/java -classpath ${CLASSPATH} com.sun.pdasync.PDASync.PDASync ${RESTART} ${DEBUG} ${PORT})

else

	# Less than JDK 1.2
	version_error

fi
exit 0
