#!/bin/sh
# $Id: genobj.in,v 1.4 2004/07/19 23:03:30 archiecobbs Exp $

# Constants
JAVA="/usr/bin/java"
prefix="/usr/local"
JC_HOME="${prefix}/jc"
JCCPATH="${JC_HOME}/share/jc/jc.zip"
CPCPATH="${JC_HOME}/share/classpath/glibj.zip"
SOOTDIR="${JC_HOME}/share/soot"
SOOTCPATH="${SOOTDIR}/sootclasses-2.1.0.jar:${SOOTDIR}/jasminclasses-sable-1.2.jar:${SOOTDIR}/polyglot-1.1.0.jar"

JVCPATH="${JCCPATH}:${SOOTCPATH}"
JCCPATH="${JCCPATH}:${CPCPATH}:${SOOTCPATH}"
GENCLASS="org.dellroad.jc.BootstrapObjectGenerator"

# Defaults
USERCPATH="."
BASE_SRCPATH="${JC_HOME}/src"
OBJDIR="${JC_HOME}/obj"
INCDIR="${JC_HOME}/include"
VMFLAGS="-classpath ${JVCPATH}"
VERBOSE="true"
DEBUG="false"

#
# Output usage message
#
usage()
{
    echo 'Usage: genobj [options] pattern ...' >&2
    echo ''
    echo 'Options:' >&2
    echo '    -classpath path  Specify search path for class files.' >&2
    echo '    -srcpath path    Prepend path to the search path for generated C source' >&2
    echo '                     files. The first component of the resulting path is also'
    echo '                     the destination for newly generated sources. This option' >&2
    echo '                     is cumulative and may be repeated multiple times.' >&2
    echo '    -newsrcpath path Same as -srcpath but replaces the current search path' >&2
    echo '                     instead of prepending to it.' >&2
    echo '    -objdir dir      Specify destination for generated ELF objects.' >&2
    echo '    -incdir dir      Specify include directory for JC headers.' >&2
    echo '    -vmopt opt       Pass opt through to the Java VM command line.' >&2
    echo '    -quiet           Quiet mode (omit verbose output).' >&2
    echo '    -g               Include support for Java line numbers.' >&2
    echo ''
    echo 'Example patterns:' >&2
    echo '    jan.foo.Bar      Class jan.foo.Bar' >&2
    echo '    jan.foo.*        All classes in the jan.foo package' >&2
    echo '    jan.foo.%        Same' >&2
    echo '    jan.foo.**       All classes in jan.foo and all its subpackages' >&2
    echo '    jan.foo.%%       Same' >&2
    echo ''
    echo "The default -srcpath is ${BASE_SRCPATH}."
    echo "The default -objdir is ${OBJDIR}."
    echo "The default -incdir is ${INCDIR}."
    echo ''
    exit 1
}

#
# Parse command line
#
while :
    do case "$1" in
	-srcpath)
	    USER_SRCPATH="${2}:${USER_SRCPATH}"
	    shift
	    shift
	    ;;
	-newsrcpath)
	    BASE_SRCPATH=""
	    USER_SRCPATH="${2}"
	    shift
	    shift
	    ;;
	-objdir)
	    OBJDIR="${2}"
	    shift
	    shift
	    ;;
	-classpath)
	    USERCPATH="$2"
	    shift
	    shift
	    ;;
	-incdir)
	    INCDIR="$2"
	    shift
	    shift
	    ;;
	-vmopt)
	    VMFLAGS="$2 ${VMFLAGS}"
	    shift
	    shift
	    ;;
	-quiet)
	    VERBOSE="false"
	    shift
	    ;;
	-g)
	    DEBUG="true"
	    shift
	    ;;
	--)
	    shift
	    break
	    ;;
	-*)
	    usage
	    ;;
    	*)
	    break
	    ;;
    esac
done

case $# in
    0) 
	usage
	;;
    *)
	;;
esac

JAVACMD="${JAVA} ${VMFLAGS}"

SRCPATH="${USER_SRCPATH}${BASE_SRCPATH}"

if [ ${VERBOSE} = "true" ]; then
	echo ${JAVACMD} -Djc.verbose.gen=${VERBOSE} \
	    -Djc.gen.inline.verbose=${VERBOSE} \
	    -Djc.include.line.numbers=${DEBUG} \
	    "${GENCLASS}" -classpath "${JCCPATH}:${USERCPATH}" -srcpath \
	    "${SRCPATH}" -objdir "${OBJDIR}" -incdir "${INCDIR}" "${@}"
fi
${JAVACMD} -Djc.verbose.gen=${VERBOSE} \
    -Djc.gen.inline.verbose=${VERBOSE} \
    -Djc.include.line.numbers=${DEBUG} \
    "${GENCLASS}" -classpath "${JCCPATH}:${USERCPATH}" -srcpath \
    "${SRCPATH}" -objdir "${OBJDIR}" -incdir "${INCDIR}" "${@}"
exit $?

