#!/bin/sh

#
# This script provides a wrapper around checksum.jar to make starting
# this java program as easy as starting a compiled command-line
# program.  This script should work on *nix or cygwin.  After you set
# this scripts executable bit, you can run it as follows:
#
#     checksum.sh <file>
#
# You can also pass in more command-line options like:
#
#     checksum.sh -sha1 <file1> <file2>
#
# Run "checksum.sh -h" for a list of all command-line options.
#

# You MUST change the following path if this is not where you
# installed checksum.jar
checksum_jar="$HOME/bin/checksum.jar"


########################################################################
# You should not need to edit below here.
########################################################################

verify_exec() {
    type "$1" 1>/dev/null 2>&1
    if [ $? -ne 0 ] ; then
        echo "***" 1>&2
        echo "*** $progname: Error: Unable to find \"$1\" in your path." 1>&2
        echo "***" 1>&2
    fi
}

#
# Script starts here.
#

progname="checksum.sh"
verify_exec "basename"
progname=`basename "$0"`

verify_exec "java"

# See if we need to use cygpath to convert from cygwin-style paths to
# windows-style paths because the java interpreter on windows only
# accepts windows-style paths.
has_cygpath="false"
type "cygpath" 1>/dev/null 2>&1
if [ $? -eq 0 ] ; then
    has_cygpath="true"
fi

# Set path to checksum.jar.
if [ $has_cygpath = "true" ] ; then
    checksum_jar=`cygpath -w "$checksum_jar"`
fi

if [ $has_cygpath = "true" ] ; then
    # Create the full command converting all convertable file paths
    # from cygwin to windows.
    cmd="java -jar \"$checksum_jar\""
    while [ $# -gt 0 ] ; do
        if [ -e "$1" ] ; then
            arg=`cygpath -w "$1"`
        else
            arg="$1"
        fi
        cmd="$cmd \"$arg\""
        shift
    done
    # Exit point if this is cygwin.
    eval exec "$cmd"
fi

# Only reached if this is not cygwin.
exec java -jar "$checksum_jar" ${1+"$@"}

