#!/bin/sh

translate_boot() {
  "$image" <<EOF
$optimizations
(in-package "BOOTTRAN")
(boottoclc "$1" "$output")
(quit)
EOF
}

compile_lisp() {
  "$image" <<EOF
$optimizations
(in-package "FRICAS-LISP")
(unless (fricas_compile_file "$1" "$output")
  (exit-with-status 1))
(quit)
EOF
}

make_program () {
  "$image" <<EOF
$optimizations
(in-package "FRICAS-LISP")
(unless (make-program "$output" (quote ($*)))
  (exit-with-status 1))
(quit)
EOF
}

action=
image=
output=
debug=
optimizations=

while test $# -gt 0 ; do
    arg=$1

    case $arg in
    --)
        break
        ;;
    --output=*)
        output=`echo $arg | sed -e 's/^--output=//'`
        shift;
        ;;
    --use=*)
        image=`echo $arg | sed -e 's/^--use=//'`
        shift
        ;;
    --make_program|--translate_boot|--compile_lisp)
        action=$1
        shift
        ;;
    --debug=*)
        debug=`echo $arg | sed -e 's/^--debug=//'`
        shift
        ;;
    *)
        break
        ;;
    esac
done

if [ x$action = x ] ; then
    echo "$0: no operation specified"
    exit 1
fi

if [ x"$image" = x ] ; then
    echo "$0: no lisp command specified"
    exit 1
fi

if [ x"$output" = x ] ; then
    echo "$0: no output specified"
    exit 1
fi

if [ x"$debug" = "xyes" ] ; then
    optimizations="(declaim (optimize (debug 2)))"
fi

case $action in
--make_program)
    make_program "$1"
    ;;
--translate_boot)
    translate_boot "$1"
    ;;
--compile_lisp)
    compile_lisp "$@"
    ;;
esac
