#! /bin/sh
# jcomp -- compile Java code using BOCK
# usage: jcomp <startclass> <programfile> <sourcefile>...

set -e

if test $# = 0; then
  echo 1>&2 'usage: jcomp [-O] [-o programfile] [-s startclass] sourcefile...'
  echo 1>&2 '(Any options given must be in exactly the order shown above)'
  echo 1>&2 ' -O        optimise'
  echo 1>&2 ' -E        write C to standard output; don'\''t generate binary'
  echo 1>&2 ' -o name   write program to name (j.out if not specified)'
  echo 1>&2 ' -s class  start executing at class.main() (taken from name of first'
  echo 1>&2 '                                        source file if not specified'
# echo 1>&2 'If no source files, use <programfile>*.java'
# echo 1>&2 'If no startclass, use Test'
# echo 1>&2 'If no programfile, use j.out'
  exit 1
fi

if test "$1" = -O; then
  opt=-O2
  shift
else
  opt=
fi

if test "$1" = -E; then
  output=true
  shift
else
  output=false
fi

if test "$1" = -o; then
  programfile="$2"
  shift
  shift
else
  programfile=j.out
fi

if test "$1" = -s; then
  startclass="$2"
  shift
  shift
else
  startclass=`basename "$1" .java`
  echo 1>&2 "Startclass not specified; using $startclass"
fi

imfile=jcomp.$$.c
trap 'rm -f $imfile' EXIT

#if test "$*" = ""; then set ${programfile}*.java; fi

prefix=/usr
exec_prefix=${prefix}

set +e

if $output; then
  ${BOCK:-bock} -s $startclass "$@" ${BOCKLIB:-${prefix}/share/bock/lib}/*
else
  ${BOCK:-bock} -o $imfile -s $startclass "$@" \
                ${BOCKLIB:-${prefix}/share/bock/lib}/* \
  && ${CC:-cc} $opt $imfile -o $programfile -lgc 
fi

exit $?
