#! /bin/sh

echo "* This script will ask some questions about your system and build a"
echo "* Makefile for osc (Optimizing Sisal Compiler) installation."
echo "* If you already have a file called \"Makefile\" it will be overwritten!"
echo "* For some questions, a default response is given in []."
echo "* Pressing RETURN in response to such a question will enable the default."
echo "* Answer yes/no questions with y or n."
echo ""
echo "* Press RETURN to continue."
read ans

echo " "
echo "Is this system:"
echo "1.  Sun running SunOS"
echo "2.  Some other sequential machine running UNIX"
echo "3.  Sequent Balance running DYNIX"
echo "4.  Alliant FX series running Concentrix"
echo "5.  Encore Multimax running Umax"
echo "6.  Sequent Symmetry running DYNIX"
echo "7.  Cray Y-MP or X-MP running UNICOS"
echo "8.  Cray 2 running UNICOS"
echo "9.  SGI running IRIX"
echo "10. IBM RS6000 running AIX"
echo "Enter a number: [1]"
read ans

LIBM='other'

FPO=''
DPO=''
PAR=''
GANGD=''

TheFF='f77'
TheCC='cc'
TheAR='ar r'
TheINSTALL='/bin/cp'

PROCS='1'

RHOST='-DMY_UGH'
HOST2=''

case "X$ans" in
X|X1) VERSION='sun3'; 
    HOST='-DSUN'; 
    echo "Is a 68881 floating point chip installed?"; 
    read ans;
    if [ $ans = 'y' ]
    then
      FPO='-f68881'
      DPO='-DF68881'
    fi ;;
X2) VERSION='sunix';
    HOST='-DSUNIX' ;;
X3) VERSION='balance';
    HOST='-DBALANCE';
    echo "Enter the number of available processors";
    read ans;
    PROCS=$ans;
    TheFF='fortran'
    TheCC='cc -i';
    echo "Run make in parallel?"; 
    read ans;
    if [ $ans = 'y' ]
    then
      PAR='&'
    fi;
    echo "Is LLNL gang daemon software installed?"; 
    read ans;
    if [ $ans = 'y' ]
    then
      GANGD='-DGANGD'
    fi ;;
X4) VERSION='alliant';
    HOST='-DALLIANT';
    TheFF='fortran'
    TheCC='fxc -w -ce -OM';
    LIBM="alliant";
    echo "Enter the number of available processors";
    read ans;
    PROCS=$ans ;;
X5) VERSION='encore';
    HOST='-DENCORE';
    echo "Enter the number of available processors";
    read ans;
    PROCS=$ans ;;
X6) VERSION='symmetry';
    HOST='-DSYMMETRY';
    TheFF='fortran'
    TheCC='cc -i'
    echo "Enter the number of available processors";
    read ans;
    PROCS=$ans;
    echo "Run make in parallel?"; 
    read ans;
    if [ $ans = 'y' ]
    then
      PAR='&'
    fi;
    echo "Are Weitek 1167 floating point accelerators installed?"; 
    read ans;
    if [ $ans = 'y' ]
    then
      FPO='-f1167'
      DPO='-Dw1167'
    fi ;;
X7) VERSION='cray';
    HOST='-DCRAY';
    RHOST='-DCRAYXY';
    TheCC='scc';
    TheFF='cf77'
    LIBM='cray';
    TheAR='bld r'
    echo "Enter the number of available processors";
    read ans;
    PROCS=$ans;;
X8) VERSION='cray';
    HOST='-DCRAY';
    RHOST='-DCRAY2';
    TheCC='scc';
    TheFF='cf77'
    LIBM='cray';
    TheAR='bld r'
    echo "Enter the number of available processors";
    read ans;
    PROCS=$ans;;
X9) VERSION='sgi';
    LIBM="sgi";
    HOST='-DSGI';
    echo ""
    echo "* WARNING: The Sisal run time system on the SGI uses schedctl to"
    echo "*          establish gang management of parallel execution and "
    echo "*          sysmp to bind the worker processes (Runtime/p-ppp.c)."
    echo ""
    echo "Enter the number of available processors";
    read ans;
    PROCS=$ans;;
X10) VERSION='rs6000';
    HOST='-DRS6000' ;;
*)  VERSION='unknown';
    HOST='-DUNKNOWN' ;;
esac

if [ $RHOST = '-DCRAYXY' ]
then
    echo ""
    echo "* WARNING: The Sisal run time system on the CRAY X-MP and Y-MP"
    echo "*          uses _semclr(31) and _semts(31) as well as LOCKTEST,"
    echo "*          LOCKON, and LOCKOFF, assuming that the microtasking"
    echo "*          library will not interfere with the 2 intrinsics. If"
    echo "*          this is not the case, then make approapriate changes"
    echo "*          to Runtime/locks.h."
    echo ""
fi

if [ $RHOST = '-DCRAY2' ]
then
    echo ""
    echo "* WARNING: The Sisal run time system on the CRAY 2 uses _getsem"
    echo "*          and _csem as well as LOCKTEST, LOCKON, and LOCKOFF,"
    echo "*          assuming that the microtasking library will not "
    echo "*          interfere with the 2 intrinsics.  If this is not the"
    echo "*          case, then make appropriate changes to Runtime/locks.h."
    echo ""
fi

if [ $VERSION = 'cray' ]
then
    TheRANLIB='touch'
    TheTIMEM=''
else
    TheRANLIB='touch'
    echo "Is ranlib supported? [y]"
    read ans
    case "X$ans" in
      X|Xy) TheRANLIB='ranlib'
    esac

    TheTIMEM='-DUSE_TIMES'
    echo "Is getrusage supported? [y]"
    read ans
    case "X$ans" in
     X|Xy) TheTIMEM='';;
     Xn) echo ""
         echo "* WARNING: The Sisal run time system will use TIMES to gather"
         echo "*          timing information during execution. On the SGI the"
	 echo "*          HZ value is assumed to be 100. On all other machines"
	 echo "*          it is assumed to be 60 (man times,Runtime/p-timer.c,"
	 echo "*          Backend/If1opt/if1timer.c)."
	 echo "";
    esac
fi;

OPT=''
ROPT=''
echo "Optimize the installed code? [y]"
read ans
case "X$ans" in
   X|Xy) OPT='-O'; 
	 ROPT='-O'
	 if [ $HOST = '-DCRAY' ]
	 then ROPT=''
	      OPT='' 
	 fi
	 if [ $HOST = '-DALLIANT' ]
	 then ROPT='-Oigv' 
	       OPT='-Oig'
	 fi;;
   Xn)   if [ $HOST = '-DCRAY' ]
	 then ROPT='-h noopt'
	      OPT='-h noopt'
         fi;;
esac

DBX='-g'
echo "Compile for run time dbx use via \"-g\"? [n]"
read ans
case "X$ans" in
   X|Xn) DBX=''
esac

echo "Enter path to directory for executables: [/usr/local/bin]"
read BIN_PATH
if [ "X$BIN_PATH" = 'X' ]
then
   BIN_PATH='/usr/local/bin'
fi

echo "Enter path to man pages: [/usr/man/manl]"
read MAN_PATH
if [ "X$MAN_PATH" = 'X' ]
then
   MAN_PATH='/usr/man/manl'
fi

echo ""
echo "* Makefile construction in progress..."
DATE=`date`
cat >Makefile <<EOF
# Makefile for SISAL
# Generated $DATE by $0.

# ******************************************************************************
# ************************   MACROS TO CONFIGURE MAKEFILE   ********************
# ******************************************************************************
# **** DOCUMENTATION SYMOBLS: [] = optional, {} = pick one
# **** COMMAND LINE MACRO DEFINITIONS WILL OVERRIDE THOSE SHOWN HERE

# **** PATHS TO COMMANDS USED BY THE MAKEFILE--CHECK FOR ACCURACY
CC          = $TheCC
INSTALL     = $TheINSTALL
RANLIB      = $TheRANLIB
AR          = $TheAR

# **** HOST SYSTEM
# **** HOST = -D{ENCORE,ALLIANT,etc.}
HOST = $HOST $RHOST

# *** NUMBER OF AVAILABLE PROCESSORS IN THE HOST SYSTEM
PROCS = $PROCS

# **** FLOATING POINT CHIP (FOR EXAMPLE, SUN)
# **** FPO = -f68881
FPO = $FPO
DPO = $DPO

# **** IS GANGD TO BE USED: ONLY SUPPORTED ON BALANCE
# **** GANGD = [-DGANGD]
GANGD = $GANGD

# **** SHOULD THE MAKEFILE GO IN PARALLEL: ONLY SUPPORTED ON BALANCE
# **** PAR = [&]
PAR = $PAR

# **** OPTIMIZE THE GENERATED ASSEMBLY CODE
# **** OPT = [-O]
OPT = $OPT
ROPT = $ROPT

# **** COMPILE FOR RUN TIME DBX USE
# **** DBX = [-g]
DBX = $DBX

# **** ABSOLUTE PATHS TO EXECUTABLE (BIN_PATH) AND MAN PAGE INSTALLATION
# **** SITES. (Ex. BIN_PATH = /usr/local/bin, MAN_PATH = /usr/man/manl)
BIN_PATH = $BIN_PATH
MAN_PATH = $MAN_PATH


#*******************************************************************************
# ************************* DO NOT MODIFY ANYTHING ELSE ************************
#*******************************************************************************

PR          = -DMAX_PROCS=${PROCS}
FF          = ${TheFF}

T_DEF1      = BIN_PATH=\${BIN_PATH}
T_DEF2      = MAN_PATH=\${MAN_PATH}

LIBM        = ${LIBM}

TIMEM       = ${TheTIMEM}

CC_OPTS     = \${HOST} \${PR} \${FPO} \${DPO} \${GANGD} \${DBX} \${TIMEM}


F_CC_CMD    = "CC=\${CC} \${HOST}"
T_CC_CMD    = "CC=\${CC} \${CC_OPTS} \${OPT}" "\${T_DEF1}" "\${T_DEF2}" \
	      "PAR=\${PAR}"
R_CC_CMD    = "CC=\${CC} \${CC_OPTS} \${ROPT}" "PAR=\${PAR}" "AR=\${AR}"
CC_CMD      = "CC=\${CC} \${CC_OPTS} \${OPT}" "PAR=\${PAR}" "AR=\${AR}"


BINSTALL    = \${INSTALL}
SINSTALL    = \${INSTALL}

BINSTALL_CMD = "BINSTALL=\${BINSTALL}" "BIN_PATH=\${BIN_PATH}"
SINSTALL_CMD = "SINSTALL=\${SINSTALL}" "MAN_PATH=\${MAN_PATH}"


# **** LOCAL (all without install)
local: tools frontend backend runtime

# **** ALL
all: tools frontend backend runtime install

# **** FRONTEND
frontend:
	cd Frontend; make local \${F_CC_CMD}; cd ..

# **** TOOLS
tools:
	cd Tools; make local \${T_CC_CMD}; cd ..

# **** BACKEND
backend: if1ld if1opt if2mem if2up if2part if2gen
if1ld:
	cd Backend/If1ld;    make local \${CC_CMD}; cd ../..
if1opt:
	cd Backend/If1opt;   make local \${CC_CMD}; cd ../..
if2mem:
	cd Backend/If2mem;   make local \${CC_CMD}; cd ../..
if2up:
	cd Backend/If2up;    make local \${CC_CMD}; cd ../..
if2part:
	cd Backend/If2part;  make local \${CC_CMD}; cd ../..
if2gen:
	cd Backend/If2gen;  make local \${CC_CMD}; cd ../..

# **** RUNTIME
runtime:
	cd Runtime; make local \${R_CC_CMD}; cd ..

# **** INSTALL
install: install_tools install_if1ld install_if1opt install_if2mem \\
	 install_if2up install_if2part install_if2gen install_runtime \\
	 install_frontend

install_tools:
	cd Tools; \\
	make install -i \${T_CC_CMD} \${BINSTALL_CMD} \${SINSTALL_CMD};\\
	cd ..
install_frontend:
	cd Frontend;   make install -i \${BINSTALL_CMD} \${SINSTALL_CMD};\\
	cd ..
install_if1ld:
	cd Backend/If1ld;   make install -i \${BINSTALL_CMD} \${SINSTALL_CMD};\\
	cd ../..
install_if1opt:
	cd Backend/If1opt;  make install -i \${BINSTALL_CMD} \${SINSTALL_CMD};\\
	cd ../..
install_if2mem:
	cd Backend/If2mem;  make install -i \${BINSTALL_CMD} \${SINSTALL_CMD};\\
	cd ../..
install_if2up:
	cd Backend/If2up;   make install -i \${BINSTALL_CMD} \${SINSTALL_CMD};\\
	cd ../..
install_if2part:
	cd Backend/If2part; make install -i \${BINSTALL_CMD} \${SINSTALL_CMD};\\
	cd ../..
install_if2gen:
	cd Backend/If2gen; make install -i \${BINSTALL_CMD} \${SINSTALL_CMD};\\
	cd ../..
install_runtime:
	cd Runtime;\\
	make \${LIBM} install -i \${BINSTALL_CMD} \${SINSTALL_CMD} "FF=\${FF}" "AR=\${AR}" "RANLIB=\${RANLIB}";\\
	cd ../..

# **** CLEAN
clean:
	cd Tools;            make clean; cd ..
	cd Frontend;         make clean; cd ..
	cd Backend/If1ld;    make clean; cd ../..
	cd Backend/If1opt;   make clean; cd ../..
	cd Backend/If2mem;   make clean; cd ../..  
	cd Backend/If2up;    make clean; cd ../..  
	cd Backend/If2part;  make clean; cd ../..  
	cd Backend/If2gen;   make clean; cd ../..  
	cd Runtime;          make clean; cd ..
EOF

echo ""
echo "* Makefile has been built."
echo "* Please check it over to ensure it is as you wish."
echo "* When you are satisfied, enter \"make all\" to build and install osc."
