#!/bin/sh
export PATH || exec /bin/sh $0 $argv:q

#
# DESCRIPTION
# 
# Bourne Shell Script
#	Make Java Run-Time Stub shell script for a standalone java 
#	class file.
#
# Author:
#	Peter Pilgrim 
#	Thu Jan 22 10:17:29 GMT 1998
# 
# RCS HEADER ``make-jrt-stub.sh''
# 
# $Author: pilgpe $
# $Date: 1999/06/28 12:07:09 $
# $Source: /export/devel/cvs/OTC_DEV/CvtKondor/src/make-jrt-stub.sh,v $
# $Revision: 1.1 $    $State: Exp $    $Locker:  $
#

# ********************************************************************************
PrintUsage()
# ********************************************************************************
{
    cat << EOF
USAGE: $myname	
		[ --java-home (-j) <JAVA_ROOT_DIR> ]
		[ --property  (-D) <JAVA_PROPERTY> ]
		[ --zipfile   (-Z) <ZIPFILE>    ]...
		[ --jarfile   (-Z) <JARFILE>    ]...
		[ --srcdir    (-S) <SOURCEDIR>  ]...
		[ --output    (-o) <SCRIPTNAME> ]
		[ --classpath (-cp) <CLASSPATH> ]
		[ --dryrun (-dr) ] [ --force ]
		[ --verbose (-v) | --noverbose (+v) ] 
		[ --help (-h) | --usage (-u) ] 
		JAVACLASSFILE...

OPTIONS:
    '--output' '-o'	  specifies the name of the output stub.
    '--zipfile' '-Z'	  specifies a zip file or jar file that should be also
    '--jarfile'		  prepended to the generated classpath.
    '--srcdir' '-S'	  specifies a development source directory to add the
			  generated classpath.
    '--property' '-D'	  specifies a Java system property to pass to the JVM.
    '--java-home' '-J'	  specifies where java is installed.
    '--classpath' '-cp'	  specifies the pathname for java classes and archives.
    '--dryrun'		  dry run and test the configuration, do not configure the
    '-dr'		  the run-time stub file.
    '--verbose'		  generates verbose output also.
    '-v'
    '--help'		  produces this brief text and exits gracefully.
    '-h' '-u'

DESCRIPTION:

Generates a run-time shell-script stub file for a java class file. 
Assumption is that the supplied java class has a valid public 
exported application class with a public method of the form:

   'public static void main( String args[] )'.

In other words it must be a Java Application not an applet.

You can pass system properties to the java virtual machine by using the
property cli parameter e.g.

   $myname -Djava.rmi.codebase='http://myserver/foo'

NB:The entire environment is passed through by default as a system property 
with the argument -Denv="\`env\`"

By default the output stub is the last component of the class name. If the
fully qualified class name is 'greek.myth.minotour.Crusade', then 
the stubname will be 'Crusade'. Use the '-o' flag to override the default

EXAMPLES:
   pegusus > $myname  MyApplication.class
   pegusus > $myname  xenon/alpha/beta/DemoApp.java


ENVIRONMENTAL VARIABLES:

    'JAVA_HOME' where is java installed on your system
    'CLASSPATH' the path java uses to look up specific classnames.

Peter Pilgrim Thu Jan 22 10:19:43 GMT 1998
EOF

    echo '$RCSfile: make-jrt-stub.sh,v $ $Revision: 1.1 $ $Author: pilgpe $ $Date: 1999/06/28 12:07:09 $'
    exit 0
}

# ********************************************************************************
BackupFile ()
# ********************************************************************************
{
    # Backup a file by renaming it.
    ThisFile=$1
    if [ -f ${ThisFile} ]; then
	if [ -f ${ThisFile}.bak ]; then
	    (set $VerboseOpt; ${PrefixCmd} /bin/rm -f ${ThisFile}.bak )
	fi
	(set $VerboseOpt; ${PrefixCmd} mv ${ThisFile} ${ThisFile}.bak )
    fi
}

# ********************************************************************************
SysInfo() 
# ********************************************************************************
{
    # Log an informational message string to the standard out and do NOT exit
    echo "$myname: *INFO* : $1"
}

# ********************************************************************************
SysWarn() 
# ********************************************************************************
{
    # Log an message string to the standard out and do NOT exit
    echo "$myname: *WARNING* : $1" 1>&2
}

# ********************************************************************************
SysError() 
# ********************************************************************************
{
    # Log an message string to the standard error and exit
    echo "$myname: *ERROR* : $1" 1>&2
    exit 1
}

# ********************************************************************************
SignalCatcher() 
# ********************************************************************************
{
    # A generic signal handler for the shell script.
    echo "$myname: Got Signal $1"
    WriteLog "$myname: Got Signal $1"
    exit 3
}

# ********************************************************************************
CleanUp () 
# ********************************************************************************
{
    # if [ "$1" != "" ]; then
    #	echo "$myname: $1" 1>&2
    # fi
    # remove temporary files etcetera
    # echo "$myname:Fini ( total:$total, errors:$errors, completed:$cmpltd)"
    /bin/rm -f DUMMY_FILE $TempFile1 $TempFile2
}

# ********************************************************************************
MakeJavaRunTimeStub ()
# ********************************************************************************
{
    # From a java class file, generate a basic html applet stubfile for
    # a HTML browser to use.

    JavaClass="$1"
    JavaBaseClass=`basename "$1"`
    RunTimeStubFile="$2"

    if [ ! -f "$JavaClass" ]; then
	if [ "x${force_flag}" = "x" ];then
	    SysWarn "Can't find java class file:\`$JavaClass'"
	    return 1
	fi
    fi

    RootFilename=`echo "$JavaClass" | sed 's/\.class$//' `
    if [ "$RootFilename" = "$JavaClass" ]; then
	SysWarn "Sorry, input filename \`$JavaClass' does not have \`.class' suffix."
	return 1
    fi
    if [ "x${RunTimeStubFile}" = "x" ]; then
        # If no runtime stub file is set
	RunTimeStubFile=`basename $RootFilename`
    fi

    if [ -r "$RunTimeStubFile" ] ; then
	${PrefixCmd} /bin/rm -f DUMMYFILE "$RunTimeStubFile"
    fi
    Title="\`${JavaBaseClass}'"

    if [ $DryRunFlag -eq 0 ]; then
	OutputFile=`basename ${RunTimeStubFile}`
    else
	OutputFile=`tty`
    fi

    RootPackageFilename=`echo $RootFilename | sed -e 's!/!.!g'`
    if test -n "$verbose" ; then
	echo "OutputFile=\`$OutputFile'"
	echo "Java main class pathname:      \`$RootFilename'"
	echo "Java main class packaged name: \`$RootPackageFilename'"
	echo "------------------------------------------------------------"
    fi

    cat <<-XEOF > $OutputFile
	#!/bin/sh
	export PATH || exec /bin/sh \$0 \$argv:q
	
	# **** WARNING: DO NOT EDIT THIS FILE ****
	# This file was *automagically* generated by '$myname', which is
	# bash shell script written by Peter Pilgrim [c] 1998
	#
	# DESCRIPTION
	#	Shell script - Run-Time Java stub file '$OutputFile' to execute
	#	a standalone java application.
	# 
	# Author
	#	${USER}
	#	`date`
	#
	# RCS HEADER \`\`$OutputFile''
	# 
	# \$Author\$
	# \$Date\$
	# \$Source\$
	# \$Revision\$    \$State\$    \$Locker:  $
	# 

	myname=\`basename \$0\`
	debug=
	verbose=
	PrefixCmd=

	XEOF
    status=$?
    if [ $status -ne 0 ]; then
	SysWarn "could not java run-time stub file:\`$RunTimeStubFile' (status:$status)"
    fi

    # Write the JAVA_HOME
    if [ "x${jrt_java_home}" != "x" ]; then
	cat <<-XEOF >> $OutputFile
	# Where can I find a default Java home if required
	: \${JAVA_HOME:=${jrt_java_home}}
	export JAVA_HOME
	XEOF
    fi

    # Application Classpath
    # Development source directories take precedence
    # over installed public directories
    app_classpath=""
    if [ "x${srcdir_list}" != "x" ] ; then 
	# Add the source directory list if it was specified
	if [ "x${app_classpath}" != "x" ] ; then
	    app_classpath="${app_classpath}:"
	fi
	app_classpath="${app_classpath}${srcdir_list}"
    fi
    if [ "x${jarfile_list}" != "x" ]; then
	# Add the installed jar file list
	if [ "x${app_classpath}" != "x" ] ; then
	    app_classpath="${app_classpath}:"
	fi
	# Add in the jar file list if it was specified
	app_classpath=${app_classpath}${jarfile_list}
    fi

    if [ "x${app_classpath}" = "x" ] ; then 
	# If no application classpath was defined, then use the 
	# development directory as a default.
	app_classpath=`pwd`
    fi
    
    # Write the CLASSPATH if required
    if [ "x${jrt_classpath}" != "x" ]; then
	cat <<-XEOF >> $OutputFile
	# What is the Java archive/zip file(s) classpath if required
	: \${CLASSPATH:=${jrt_classpath}}
	export CLASSPATH
	XEOF
    fi

    cat <<-XEOF >> $OutputFile
	# Append the application classpath to the user's classpath.
	PROG_CLASSPATH=${app_classpath}

	if [ "x\$CLASSPATH" != "x" ]; then
	    PROG_CLASSPATH=\${PROG_CLASSPATH}:\${CLASSPATH}
	fi

	# Define the environment variable 'JRTSTUB_DEBUG' to 'yes'
	# To get debuggable output from this script.
	case \${JRTSTUB_DEBUG} in
	    [Yy][Ee][Ss] | [Tt][Rr][Uu][Ee] )
	    echo ""
	    echo "${myname}: Java runtime stub *debugging* output:"
	    echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
	    echo "          JAVA_HOME=\${JAVA_HOME}"
	    echo "         SWING_HOME=\${SWING_HOME}"
	    echo "          CLASSPATH=\${CLASSPATH}"
	    echo "     PROG_CLASSPATH=\${PROG_CLASSPATH}"
	    echo " JAVAFILESEARCHPATH=\${JAVAFILESEARCHPATH}"
	    echo "         IMAGES_DIR=\${IMAGES_DIR}"
	    echo "          AUDIO_DIR=\${AUDIO_DIR}"
	    echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
	    echo ""
	    ;;

	    * ) ;;
	esac

	#
	# Run the java interpreter on the Java application class.
	#
	## WARNING 
	## ====================================================================
	## Please the note environment variables are passed to a Java program
	## using a system property called \`env'. If this property does 
	## exist then you get a runtime exception
	java -Denv="\`env\`" ${system_properties_list} -classpath \${PROG_CLASSPATH} "${RootPackageFilename}" "\$@"; retval=\$?
	exit \$retval
	# fini
	XEOF

    ${PrefixCmd} chmod 755 "$RunTimeStubFile"
    test -n "$verbose" && echo "${myname}: created file:\`$RunTimeStubFile'"

    return 0
}

# ********************************************************************************
# MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN 
# ********************************************************************************

myname=`basename $0`

debug=
verbose=
silent=
VerboseOpt="+x"
PrefixCmd=""
DryRunFlag=0
cf_prev_arg=
cf_optval=
force_flag=

cmpltd=0
errors=0
total=0

jrt_java_home="${JAVA_HOME}"
## jrt_classpath="$CLASSPATH"
jrt_classpath=""

jarfile_list=""
srcdir_list=""

system_property_list=""
StubOutputFile=""

#
# Interpret the command line arguments (the GNU way!)
#
while [ $# -gt 0 ]
do
    #
    # Interpret cli argument
    #
    case $1 in

	-output | --output | --outpu| --outp | --out | --ou | --o | -o )
	StubOutputFile="$2"
	shift
	;;

	-zipfile | --zipfile | --zipfil | --zipfi | --zipf | --zip | -zip | \
	-jarfile | --jarfile | --jarfil | --jarfi | --jarf | --jar | -jar | -Z )
	# The name of a zip-file or java archive file to use.
	if [ "x$2" = "x" ]; then
	    SysError "empty jar or zip file supplied!"
	fi
	if [ "x${jarfile_list}" = "x" ]; then
	    jarfile_list="$2"
	else
	    jarfile_list="${jarfile_list}:$2"
	fi
	shift
	;;

	-srcdir | --srcdir | --srcdi | --srcd | --src | -src | --S | -S )
	# Source directory
	if [ "x$2" = "x" ]; then
	    SysError "empty source directory supplied!"
	fi
	if [ "x${srcdir_list}" = "x" ]; then
	    srcdir_list="$2"
	else
	    srcdir_list="${srcdir_list}:$2"
	fi
	shift
	;;

	-D | -property | --property | --prop | -prop )
	# Source directory
	if [ "x$2" = "x" ]; then
	    SysError "empty JVM system property supplied!"
	fi
	if [ "x${system_property_list}" = "x" ]; then
	    system_property_list="-D$2"
	else
	    system_property_list="${system_property_list} -D$2"
	fi
	shift
	;;

	-force | --force | --f | -f )
	# Force flag
	force_flag=yes
	;;

	-classpath | --classpath | --classpat | --classpa | --classp | \
	--cp | -cp )
	# Java run time stub class path
	jrt_classpath="$2" ; shift
	;;

	-java-home | --java-home | -jdk-home | --jdk-home | \
	-javahome | --javahome | --java | --java | -java | --jdk | -jdk | -j )
	# where does the JDK live.
	jrt_java_home="$2" ; shift
	;;

	# **** The standard CLI options begin here ****
	-silent | -quiet | \
	--silence | --silenc | --silenc | --silen | --sile | --sil | \
	--quiet | --quie | --qui | --qu | --q | -q )
	silent=yes
	verbose=
	VerboseOpt="+x"
	;;
 
	-verbose | --verbose | --verbos | --verbo | --verb | \
	--ver | --ve | --v | -v ) 
	verbose=yes
	VerboseOpt="-x"
	;;

	--debug | --debu | --deb | -debug | -debu | -deb ) 
	debug=yes
        ;;

	--dryrun | --dryru | --dryr | --dry | --dr | -dr | \
	-dryrun | -dryru | -dryr | -dry | -dr | -dr )
	PrefixCmd="echo =>"
	DryRunFlag=1
	;;

	--help | --hel | --he | --h | -help | -hel | -he | -h | \
	--usage | --usag | --usa | --us | --u | \
	-usage | -usag | -usa | -us |  -u )
	PrintUsage
	exit 0
	;;

	--* | -*)
	SysError "unknown cli option: '$1'. Try '--help' for more info"
	break;;

	*) break;;

    esac
    shift
done

#
# Trap any signals
#
trap 'CleanUp "Cleaning"' 0
trap 'SignalCatcher "(SIGHUP)"'  1
trap 'SignalCatcher "(SIGINT)"'  2
trap 'SignalCatcher "(SIGQUIT)"' 3
trap 'SignalCatcher "(SIGTERM)"' 15


# Check we have a default JVM
if [ "x${jrt_java_home}" = "x" ]; then
    SysError "script cannot generate a default location for a Java Virtual Machine. 
Please set up the environment variable JAVA_HOME to point your JVM
or pass it through using the cli option \`--java-home'."
fi

## if [ "x${jrt_classpath}" = "x" ]; then
##     # This does not work at all on Solaris, because the JVM is using direct tty comms
##     # thereby avoid any shell i/o stream at all.
##     JVMVersion=`java -version `
##     case "$JVMVersion" in
## 	   1.0.* | 1.1.* )
## 	   # Java Platform 1
## 	   jrt_classpath=".:${jrt_java_home}/lib/classes.zip"
## 	   ;;
## 
## 	   *)
## 	   # Java Platform 2
## 	   jrt_classpath="."
## 	   ;;
##    esac
##     
## fi


#
# Do whatever
#
if [ $# -ne 1 ]; then
    SysError "need an applet class name."
fi
arg="$1"
MakeJavaRunTimeStub "$arg" "$StubOutputFile"; retval=$?
exit $retval

#fini - `make-jrt-stub' -	$RCSfile: make-jrt-stub.sh,v $ $Revision: 1.1 $ $Date: 1999/06/28 12:07:09 $
