#!/bin/sh
#
SYSPATH=`pwd`
nArgs=$#
while test $nArgs -ge 1
do
    case $1 in
        --prefix=*) 
                # strip out leading "--prefix="
                SYSPATH=`echo $1 | perl -pe 's{^--prefix=(.*?)\$}{\$1}'`
                shift;;
        --help) 
            echo "./configure [--prefix=<install path>]";;
        *) 
            echo "Unrecognized argument: $1"
            exit;;
    esac
    nArgs=`expr $nArgs - 1`
done
SYSPATH="'$SYSPATH/sys'"

touch make.inc
make clean
OS=`uname`
MACHINE=`hostname`
rm -fr make.inc
if test $OS = "AIX"
then
    if test   $MACHINE = "rattler" 
    then
        ln -s Makefile.ibm   make.inc
    elif test $MACHINE = "caviar"  
    then
        ln -s Makefile.ibm1   make.inc
    else
        #ln -s Makefile.ibm_gcc   make.inc
        ln -s Makefile.ibm   make.inc
    fi
elif test $OS = "HP-UX" 
then
    ln -s Makefile.hp    make.inc
elif test $OS = "Linux" 
then
    ln -s Makefile.linux make.inc
elif test $OS = "SunOS" 
then
    ln -s Makefile.sun   make.inc
else
    echo "No rules for $OS"
    exit
fi

# do in-place edit to replace placeholder with path to boot.v
echo "Tops will look for boot.v in $SYSPATH"
perl -pi -e "\$a=1 if /TOPS_SYSPATH_DEFAULT/; \
              if (\$a and /IF\s+'.*?'\s+THEN/) { \
                 \$a = 0; s{IF\s+'.*?'\s+THEN}{IF $SYSPATH THEN};} " src/sys.c
echo
echo "Ready to run 'make' [$MACHINE running $OS]"
echo
