#! /bin/sh
# Configure/Build script for JSPICE3
# (C) Stephen R. Whiteley 1994

version=2.5;
makeprog=$MAKE
if [  z$makeprog = "z" ]; then
    makeprog=make;
fi

stop_at_binmake=0
if [ $# -gt 0 ]; then
    if [ $1 = "make" ]; then
        echo Creating main makefile only
        stop_at_binmake=1
    fi
fi

if [ -d conf/unixconf ]; then
    if [ -f conf/unixconf/mkheader.0 ]; then
        rm -f conf/mkheader.in
        echo '#######################################################################'> conf/mkheader.in
        echo '####### Makefile generated by build script ############################'>> conf/mkheader.in
        echo VERSION = $version>> conf/mkheader.in
        echo DATE = `/bin/date`>> conf/mkheader.in
        echo '#######################################################################'>> conf/mkheader.in
        cat conf/unixconf/mkheader.0 >> conf/mkheader.in
    else
        echo Error: conf/unixconf/mkheader.0 not found
        exit;
    fi
    if [ -f conf/unixconf/config_h.0 ]; then
        rm -f conf/config.h.in
        cp conf/unixconf/config_h.0 conf/config.h.in
    else
        echo Error: conf/unixconf/config_h.0 not found
        exit;
    fi
else
    echo Error: conf/unixconf directory not found
    exit;
fi

if [ -f conf/util/configure ]; then
    cd conf

# Hackery for MacPorts X11 on Apple OS X, which puts X in /opt/X11
# which is not found automatically by the configure script.  There may
# be other targets where X is not found, these can probably be treated
# similarly.
    if [ `uname -s` = Darwin -a -d /opt/X11 ]; then
        util/configure --x-includes=/opt/X11/include --x-libraries=/opt/X11/lib -srcdir .
    else
        util/configure -srcdir .
    fi

    if  [ ! -f ../src/include/config.h ] || \
            [ "`diff config.h ../src/include/config.h |head`" ]; then
        echo Installing config.h
        rm -f ../src/include/config.h
        cp config.h ../src/include
    fi
    rm -f config.h
    cd ..
else
    echo Error: conf/util/configure not found
    exit;
fi

if [ -f conf/mkheader ]; then
    rm -f src/bin/Makefile
    cp conf/mkheader src/bin/Makefile
    cat conf/common >> src/bin/Makefile
    cat conf/unixconf/maketail.0 >> src/bin/Makefile
    if [ -f src/bin/Makefile ]; then
        echo Changing directory to src/bin
        cd src/bin
        echo -n Creating nuconfig.c ...
        rm -f nuconfig.c
        $makeprog preamble > nuconfig.c
        cat nconfig.c >> nuconfig.c
        echo done
        echo -n Creating spconfig.c ...
        rm -f spconfig.c
        $makeprog preamble > spconfig.c
        $makeprog devs >> spconfig.c
        cat sconfig.c >> spconfig.c
        echo done
        echo -n Adding dependencies to makefile...
        $makeprog depend >> Makefile
        echo done
        if [ $stop_at_binmake -eq 1 ]; then
            rm -f ../../conf/mkheader.in ../../conf/configure \
              ../../conf/config.h.in ../../conf/config.status
            echo bin/Makefile created, cd to src/bin to continue
            echo
            echo Type \"$makeprog make\" to create subsidiary makefiles,
            echo then \"$makeprog all\" for binaries, then, as root,
            echo \"$makeprog install\" to install the programs.
            echo
            exit
        fi
        echo Creating makefiles in the subdirectories...
        $makeprog make
        echo ...done
        $makeprog all 2> ../../errs
        echo done
        echo Check the errs file for any problems.
        echo To finish the installation, cd to src/bin, become root, and type
        echo \"$makeprog install\".
        rm -f ../../conf/mkheader.in ../../conf/configure \
          ../../conf/config.h.in ../../conf/config.status
    fi
else
    echo Error: couldn\'t create makefile in this directory.
    exit;
fi
