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

#### WARNING: This shell script contains embedded tabs.
#
# DESCRIPTION
# 
# Merges N number of jar files into one big jar file using a temporary directory
#
# Peter Pilgrim
# Tue Mar 23 20:30:17 GMT 1999
# 
# RCS HEADER ``make-jar.sh''
# 
# $Author$
# $Date$
# $Source$
# $Revision$    $State$    $Locker$
#

# ********************************************************************************
PrintUsage() 
# ********************************************************************************
{
    cat << XEOF
USAGE: 
    $myname  
	   [ --help (-u) | --usage (-u) ]
	   [ --dryrun (-dr) ] [ --silent (-s) ]
	   [ --verbose (-v) | --noverbose (+v) ]
	   SRCJAR...  DSTJAR

OPTIONS:-

    '--help'		produces this message text.
    '--usage'		ditto.

    '--verbose'		produces verbose output.
    '--noverbose'	no extra info is printed, quiet.
    '--silent'		run the script silently.
    '--dryrun'		dry run and test the configuration, do not configure the
    '-dr'		makefile.


DESCRIPTION:-
    '${myname}' merges N number of jar files into one.

Peter Pilgrim Sun Sep 06 17:37:42 BST 1998
Version 1.0
XEOF

    echo '$RCSfile: make-jar.sh,v $ $Revision: 1.3 $ $Date: 1996/05/23 14:08:39 $ $Author: peterp $'
    exit 0
}

# ********************************************************************************
SysWarn ()
# ********************************************************************************
{
    # Print warning exception and allow the script to continue.
    echo "$myname: *WARNING* : $1" 1>&2
}

# ********************************************************************************
SysError ()
# ********************************************************************************
{
    # Print fatal error exception and terminate the script with error.
    echo "$myname: *ERROR* : $1" 1>&2
    exit 1
}

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

# ********************************************************************************
CleanUp () 
# ********************************************************************************
{
    # clean up at exit remove temporary files etcetera
    echo "$myname: finito (total:$total,errors:$errors,cmpltd:$cmpltd)"
    /bin/rm -f DUMMY_FILE $TempFile1 $TempFile2
    /bin/rm -rf DUMMY_DIR $JarMergeDir
}


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

myname=`basename $0`
PrefixCmd=""
DryRunFlag=0
VerboseOpt="+x"
silent=
verbose=
debug=

# Save the current working directory now
SaveDir=`pwd`

# Where should temporaries go.
: ${TMPDIR:=/tmp}

# Java Archive Command
: ${JAR_CMD:=jar}

# Java Archive Command
: ${UNZIP_CMD:=unzip}

JarMergeDir=${SaveDir}/jarmerge

#
# Command Line Arguments interpretation
#
with_opt=NONE
first_arg=dummy
prev_arg=0
while [ $# -gt 0 ]
do
    #
    # Interpret current command line argument
    #
    case $1 in

	-store | --store | --stor | --sto | --st | --0 | -0 )
	# Do NOT compress JAR with ZLIB algorithm
	JarStoreOnlyFlag='0'
	;;

	-nostore | --nostore | --nostor | --nosto | --nost | ++0 | +0 )
	# compress JAR into one large file with ZLIB algorithm
	JarStoreOnlyFlag=""
	;;

	-normal | -norm | --normal | --norma | \
	--norm | --nor | --no | --n | -n )
	   NormalFlag=1 ;;
 
	-force | --force | --forc | --for | --fo | --f | -f )
	   ForceFlag=1;;

	# **** 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"
	JarVerboseFlag="v"
	;;

	--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

#
# Reset the accumulation
#
SavedCli="$@"
total=0
errors=0
cmpltd=0
status=0

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

if [ $# -lt 2 ]; then
    SysError "no destination jar was specified."
fi

if [ -d ${JarMergeDir} ]; then
    /bin/rm -rf ${JarMergeDir}
fi
mkdir ${JarMergeDir}

#
# For all the Java Archives unpack the files.
#
for JarFile
do
    total=`expr $total + 1`
    if [ $# -eq 1 ]; then
	break;
    fi
    if [ -d ${JarFile} ]; then
	errors=`expr $errors + 1`
	SysError "cannot unzip a directory: ${JarFile}"
    fi

    echo "    [$total] Unzipping file:\`${JarFile}' ..."
    if [ $DryRunFlag -ne 0 ]; then
	echo "=> ${UNZIP_CMD} -o ${JarFile}  -d ${JarMergeDir}"
    else 
	# Find any MANIFEST file and delete them straight away
	# find ${JarMergeDir} -name "MANIFEST.MF" -print
	(set -x; /bin/rm -rf ${JarMergeDir}/META-INF )
	find ${JarMergeDir} -name "MANIFEST.MF" -exec /bin/rm -f {} \;
	# The `-o' means command line arguments mean unzip and overwrite
	# any existing file so order of Jar/Zip file is important! *PP*
	${UNZIP_CMD} -o ${JarFile}  -d ${JarMergeDir}; status=$?
    fi
    if [ $status -ne 0 ]; then
	SysError "failed to unzip file: ${JarFile} (status:$status)"
	errors=`expr $errors + 1`
    else
	cmpltd=`expr $cmpltd + 1`
    fi
    echo "---------------"
    shift
done

#
# Pay special attention to if the destination file is a absolute
# or relative filename.
#
DestJarFile="$1"
echo "Creating the merged JAR file:\`${DestJarFile}'"

case $DestJarFile in
     /*)
	ThisJarFile=$DestJarFile
	;;
     -)
        ThisJarFile=$DestJarFile
	PipeCmd=1
	errors=`expr $errors + 1`
	SysError "cannot use a bar as a target jar file!"
	;;
     *)
	ThisJarFile=$SaveDir/$DestJarFile
	;;
esac

if [ -d ${ThisJarFile} ]; then
    errors=`expr $errors + 1`
    SysError "destination file:\`${DestJarFile}' cannot be a directory."
fi
if [ -f ${ThisJarFile} ]; then
    # Delete the old jar file
    /bin/rm -f ${ThisJarFile}
fi

#
# Create the new merge Jar file by changing to the directory
# and creating a Java archive.
#
find ${JarMergeDir} -name "MANIFEST.MF" -exec /bin/rm -f {} \;

( cd ${JarMergeDir} ; 
  ${PrefixCmd} \
  ${JAR_CMD} ${JarStoreOnlyFlag}c${JarVerboseFlag}f ${ThisJarFile} . ); 
status=$?

if [ $status -ne 0 ]; then
    SysError "Failed to create a merged file:\`${ThisJarFile}' (status:$status)"
    cmpltd=`expr $cmpltd + 1`
else
    errors=`expr $errors + 1`
fi

exit 0
# fini
