+
+unset CDPATH
+
+TEMP=`getopt -o 'gI:o:' --long 'help,pkgdir:' -n goaljobs -- "$@"`
+if [ $? -ne 0 ]; then
+ echo "goaljobs: could not parse command line arguments"
+ exit 1
+fi
+eval set -- "$TEMP"
+
+set -e
+
+usage ()
+{
+ echo "Usage: goaljobs [-o output] source.ml"
+ echo "See goaljobs(1) man page for more information."
+}
+
+declare -a passthru
+i=0
+
+while true; do
+ case "$1" in
+ -g)
+ passthru[i++]="$1"
+ shift;;
+ -I)
+ passthru[i++]="$2"
+ shift 2;;
+ -o)
+ output="$2"
+ shift 2;;
+ --pkgdir)
+ pkgdir="$2"
+ shift 2;;
+
+ --help)
+ usage
+ exit 0;;
+ --)
+ shift
+ break;;
+ *)
+ echo "Internal error!"
+ exit 1;;
+ esac
+done
+
+if [ $# -lt 1 ]; then
+ usage
+ exit 1
+fi
+
+# Bytecode or native code?
+if ocamlopt --help >/dev/null 2>&1; then
+ best=opt
+ libext=cmxa
+else
+ best=c
+ libext=cma
+fi
+
+# Get name of final source file.
+for final; do :; done
+
+# Get module names of all source files, and check them.
+declare -a modules
+i=0
+for src in "$@"; do
+ # Module name of source file.
+ module=`basename "$src" .ml`
+ module="$(tr '[:lower:]' '[:upper:]' <<< ${module:0:1})${module:1}"
+
+ # Check module name is a valid OCaml name.
+ if [[ ! ( "$module" =~ ^[A-Z][a-zA-Z0-9_]*$ ) ]]; then
+ echo "$0: module name '$module' is not a valid OCaml module name."
+ echo "You have to use a file that contains only letters, numbers and"
+ echo "underscore, and starts with a letter."
+ exit 1
+ fi
+
+ modules[i++]="$module"
+done
+
+# Choose an output filename if the user didn't select one.
+if [ "$output" = "" ]; then
+ output=`basename "$final" .ml`
+fi
+
+# Either use installed package or if user selected --pkgdir then
+# use goaljobs from that directory.
+declare -a pkg
+if [ "$pkgdir" = "" ]; then
+ pkg[0]="-package"
+ pkg[1]="goaljobs,goaljobs.syntax"
+else
+ pkgdir="$(cd $pkgdir; pwd)"
+ pkg[0]="-I"
+ pkg[1]="$pkgdir"
+ pkg[2]="unix.$libext"
+ pkg[3]="goaljobs.$libext"
+ pkg[4]="-pp"
+ pkg[5]="camlp4o $pkgdir/pa_goal.cmo"
+fi
+
+# Compile the input file(s).
+echo \
+ocamlfind $best "${passthru[@]}" "${pkg[@]}" "$@" -o "$output"
+ocamlfind $best "${passthru[@]}" "${pkg[@]}" "$@" -o "$output"