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

#
# DESCRIPTION
# 
# Bourne Shell Script
#	Make HTML wrapper for an Java Applet class file.
#
# Author:
#	Peter Pilgrim 
#	Wed Jan 21 10:40:18 GMT 1998
# 
# RCS HEADER ``make-applet.sh''
# 
# $Author$
# $Date$
# $Source$
# $Revision$    $State$    $Locker$
#

# ********************************************************************************
PrintUsage()
# ********************************************************************************
{
    cat << EOF
USAGE: $myname	
		[ --width <APPLET_WIDTH> ] [ --height <APPLET_HEIGHT> ]
		[ --dryrun (-dr) ] 
		[ --verbose (-v) | --noverbose (+v) ] 
		[ --help (-h) | --usage (-u) ] 
		JAVACLASSFILE...

OPTIONS:
    '--width'		specify the initial width of the applet.
    '--height'		specify the initial height of the applet.
    '--dryrun'		dry run and test the configuration, do not configure the
    '-dr'		embedded applet HTML file.
    '--verbose'		generates verbose output also.
    '-v'
    '--help'		produces this brief text and exits gracefully.
    '-h' '-u'

DESCRIPTION:

Generates an embedded java applet HTML file for a java class file. 
Assumption that the java class is a valid applet. That is that it is
a subclass of the exported 'java.app.Applets' class.

EXAMPLES:
	> $myname  MyApplet.class

ENVIRONMENTAL VARIABLES:
Not at this time.

Peter Pilgrim Wed Jan 21 11:01:59 GMT 1998
EOF

    echo '$RCSfile$ $Revision$ $Author$ $Date$'
    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
}

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

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

    if [ ! -f "$JavaClass" ]; then
	SysWarn "Can't find java class file:\`$JavaClass'"
	return 0
    fi

    RootFilename=`echo "$JavaClass" | sed 's/\.class$//' `
    if [ "$RootFilename" = "$JavaClass" ]; then
	SysWarn "Sorry, input filename \`$JavaClass' does not have \`.class' suffix."
	return 1
    fi
    AppletHTMLFile=${RootFilename}.html


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

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

    WidthFmt="WIDTH=$AppletWidth"
    HeightFmt="HEIGHT=$AppletHeight"

    cat <<-XEOF > $OutputFile
	<HTML>
	<HEAD> 
	<TITLE>HTML Java Applet $Title</TITLE>
	</HEAD>
	<BODY>
	<P>HTML automatically generated by the "$myname"
	<BR><APPLET CODE=${JavaClass} ${WidthFmt} ${HeightFmt}>
	Your HTML browser software is unfortunately <I>not</I> Java aware,
	or is momentarily disabled.
	</APPLET>
	</BODY>
	</HTML>
	XEOF
    status=$?
    if [ $status -ne 0 ]; then
	SysWarn "could not create applet html file:\`$AppletHTMLFile' (status:$status)"
    else
	${PrefixCmd} chmod 644 "$AppletHTMLFile"
	test -n "$verbose" && echo "	created file:\`$AppletHTMLFile'"
    fi

    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=

cmpltd=0
errors=0
total=0

AppletWidth=600
AppletHeight=400

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

	-width | --width | -w )
	AppletWidth=$2 ; shift
	;;

	-height | --height | -h )
	AppletHeight=$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

#
# Do whatever
#
if [ $# -ne 1 ]; then
    SysError "need an applet class name."
fi

cmpltd=0
errors=0
total=0
for arg 
do
    total=`expr $total + 1`
    MakeHTMLApplet "$arg"; status=$?
    if [ $status -eq 0 ]; then
	cmpltd=`expr $cmpltd + 1`
    else
	errors=`expr $errors + 1`
    fi
done

if [ $errors -gt 0 ]; then
    retval=1;			# Errors occurred
else
    retval=0;			# No errors
fi
exit $retval

#fini - `make-applet' -	$RCSfile$ $Revision$ $Date$
