#! /bin/sh

set -e

dir=`dirname "$0"`

pb_dir="$dir/ChezScheme/boot/pb"

use_cs=maybe
use_bc=maybe
default_vm=cs
supplied_racket=no
supplied_scheme=no
enable_boothelp=
CC_FOR_BUILD='$(CC) -O2'

# We don't have to detect conflicts like `--enable-csdefault --enable-bcdefault`,
# since the `configure` scripts will do that, but  we have to figure out which
# configure scripts to run

for arg in $*; do 
    case "$arg" in
        --enable-cs)
            use_cs=yes
            ;;
        --enable-csdefault)
            use_cs=yes
            default_vm=cs
            ;;
        --enable-bc)
            use_bc=yes
            ;;
        --enable-bcdefault | --enable-cgcdefault)
            use_bc=yes
            default_vm=bc
            ;;
        --enable-csonly)
            use_cs=yes
            use_bc=no
            default_vm=cs
            ;;
        --enable-bconly)
            use_cs=no
            use_bc=yes
            default_vm=bc
            ;;
        --enable-racket=*)
            supplied_racket=yes
            ;;
        --enable-scheme=*)
            supplied_scheme=yes
            ;;
        --help | -h)
            echo $0:
            echo see --help-bc or --help-cs, since the Racket CS build and the
            echo Racket BC build support different options. If you use options
            echo that build both CS and BC, then you can mix options that apply
            echo to both or either kind of build.
            exit 0
            ;;
        --help-bc)
            exec "$dir/bc/configure" --help
            ;;
        --help-cs)
            exec "$dir/cs/c/configure" --help
            ;;
        CC_FOR_BUILD=*)
            CC_FOR_BUILD=`echo $1 | sed -e 's/^CC_FOR_BUILD=//'`
            ;;
    esac
done

# Select default build if none specified:
if test "$use_bc" = maybe ; then
    if test "$use_cs" = maybe ; then
        use_cs=yes
        use_bc=no
        default_vm=cs
    elif test "$use_cs" = no ; then
        use_bc=yes
    elif test -d "$pb_dir" -o $supplied_racket = yes ; then
        use_bc=no
    else
        echo No "$pb_dir", so enabling BC build
        use_bc=yes
        enable_boothelp=--enable-boothelp
    fi
elif test "$use_cs" = "maybe" ; then
    use_cs=no
    default_vm=bc
fi

if test "$use_cs" = "yes" ; then
    if test $use_bc = no  -a $supplied_racket = no  -a $supplied_scheme = no  -a ! -d "$pb_dir" ; then
        echo $0: must have $pb_dir, --enable-racket=... or --enable-scheme=... for --enable-csonly
        exit 1
    fi

    echo "=== Racket CS enabled"

    mkdir -p cs/c
    case "$dir" in
        /*)
            (cd cs/c && "$dir/cs/c/configure" ${1+"$@"})
            ;;
        *)
            (cd cs/c && "../../$dir/cs/c/configure" ${1+"$@"})
            ;;
    esac
fi

if test "$use_bc" = "yes" ; then
    echo "=== Racket BC enabled"

    mkdir -p bc
    case "$dir" in
        /*)
            (cd bc && "$dir/bc/configure" ${1+"$@"})
            ;;
        *)
            (cd bc && "../$dir/bc/configure" ${1+"$@"})
            ;;
    esac
fi

case "$dir" in
    /*)
        upsrcdir="$dir"
        ;;
    *)
        upsrcdir="../$dir"
        ;;
esac

echo "srcdir=$dir" > Makefile
echo "upsrcdir=$upsrcdir" >> Makefile
echo "default_vm=${default_vm}" >> Makefile
echo "use_cs=${use_cs}" >> Makefile
echo "use_bc=${use_bc}" >> Makefile
echo "CC_FOR_BUILD=${CC_FOR_BUILD}" >> Makefile
cat "$dir/Makefile.in" >> Makefile

cp "$dir/buildmain.zuo" main.zuo
