#!/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 
#	Tue Jan 27 10:00:57 GMT 1998
# 
# RCS HEADER ``clean-java-class.sh''
# 
# $Author$
# $Date$
# $Source$
# $Revision$    $State$    $Locker$
#

# ********************************************************************************
PrintUsage()
# ********************************************************************************
{
    cat << EOF
USAGE: $myname	
		[ --interactive (-i) ] [ --force (-f) ]
		[ --dryrun (-dr) ]
		[ --verbose (-v) | --noverbose (+v) ] 
		[ --help (-h) | --usage (-u) ] 
		JAVACLASSFILE...

OPTIONS:
    '--interactive'	ask for confirmation before removing files.
    '--force'		ignore any errors whilst removing files.
    '--dryrun'		dry run and test the removal process.
    '-dr'		embedded applet HTML file.
    '--verbose'		generates verbose output also.
    '-v'
    '--help'		produces this brief text and exits gracefully.
    '-h' '-u'

DESCRIPTION:

A special shell script to clean java class files. Intelligent enough
to handle inner classes generated by the Java Compiler 'javac(1)'.

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
}

#   
# OS independent dynamic 'echo -n'
#   
# ********************************************************************************
SetupDynamicPrompt ()
# ********************************************************************************
{
    if [ "`echo -n`" = "-n" ]; then
	_N_PROMPT=
	_C_PROMPT='\c'
    else
	_N_PROMPT='-n'
	_C_PROMPT=
    fi
    _SETUP_DYNAMIC_PROMPT=yes
}

# ********************************************************************************
SysPrompt ()
# ********************************************************************************
{
    if [ "${_SETUP_DYNAMIC_PROMPT}" = "" ]; then
	SetupDynamicPrompt
    fi
    echo ${_N_PROMPT} "$@${_C_PROMPT}"
}

# ********************************************************************************
PromptAskYesNo () 
# ********************************************************************************
{
    # Ask operator a question, and get a yes or no answer !
    Message=$1
    Default=$2
    
    if [ $Default -eq 0 ]; then
	DefaultAnswerStr="[n] "
	DefaultAnswer=0
    else
	DefaultAnswerStr="[y] "
	DefaultAnswer=1
    fi

    Done=0
    Answer=$DefaultAnswer
    until [ $Done -ne 0 ]
    do
	SysPrompt "$Message : $DefaultAnswerStr ? "
        read Response	
	case $Response in
	    "" )
	      Answer=$DefaultAnswer; Done=1 ;;	       
            Yes | YES | yes | Y | y )
	      Answer=1; Done=1;;
            NO | No | no | N | n )
	      Answer=0; Done=1;;
            *)
	       echo "Please answer the question with [YyNn]." ;;	       
	esac
    done

    return $Answer
}

# ********************************************************************************
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() 
# *******************************0*************************************************
{
    # A generic signal handler for the shell script.
    echo "$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
}

# ********************************************************************************
RemoveJavaClassFiles ()
# ********************************************************************************
{
    # Removes java classes safely. (Including inner classes JDK 1.1)
    # set -vx

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

    test -n "$debug" && echo "*DEBUG* RemoveJavaClassFile \`$1'"

    if [ ! -f "$JavaClass" ]; then
	if [ -z $force_flag ]; then
	    SysWarn "Can't find java class file:\`$JavaClass'"
	fi
	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
    FileList=`ls ${RootFilename}*.class 2>/dev/null`;  status=$?
    if [ $status -ne 0 ]; then
	SysWarn "no such files \`${RootFilename}*.class'" 
	return 1
    fi

    retval=0
    for File in $FileList
    do
	total=`expr $total + 1`
	if [ ! -r "$File" ]; then
	    test -z "$force_flag" && retval=1
	    errors=`expr $errors + 1`
	    SysWarn "no such file or directory \`$File' (not readable)."
	    continue
	fi
	if [ ! -f "$File" ]; then
	    test -z "$force_flag" && retval=1
	    errors=`expr $errors + 1`
	    SysWarn "files is _NOT_ a regular file \`$File'".
	    continue
	fi
	if [  ! -w "$File" ]; then
	    if test -z "$force_flag" ; then
		retval=1
		errors=`expr $errors + 1`
		SysWarn "no write permissions \`$File' (permissions)."
		continue
	    fi
	fi
	if test -n "$interactive"; then
	      PromptAskYesNo "Remove this file \`$File'" 0; the_answer=$?
	     if [ $the_answer -ne 1 ]; then
		 continue
	     fi
	fi
	( set $VerboseOpt; ${PrefixCmd} /bin/rm "$File" ); status=$?
	if [ $status -ne 0 ]; then
	    test -z "$force_flag" && retval=1
	    errors=`expr $errors + 1`
	    SysWarn "could not remove \`$File' (status:$status )"

	else
	    cmpltd=`expr $cmpltd + 1`
	fi
    done

    return $retval
}

# ********************************************************************************
# 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=

##DryRunFlag=1
##PrefixCmd="echo =>"

cmpltd=0
errors=0
total=0
interactive=
force_flag=

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

	-interactive | --interactive | --interactiv | --interacti | \
	--interact | --interac | --intera | --inter | --inte | \
	--int | --in | --i | -i )
	interactive=yes
	;;

	-force | --force | --forc | --for | --fo | --f | -f )
	force_flag=yes
	;;

	# **** 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 [ $# -lt 1 ]; then
    SysError "need at least one java class filename.  ($@) "
fi

cmpltd=0
errors=0
total=0
retval=0
for arg 
do
    RemoveJavaClassFiles "$arg"; status=$?
done

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

#fini - `clean-java-class' -	$RCSfile$ $Revision$ $Date$
