#!/bin/csh -f

set VERSION="9.6"

echo ""
echo "		Welcome to IRSIM's configuration program."
echo "		This is IRSIM version $VERSION."
echo ""

set SCRIPTS=scripts
set modules=""
set extra_libs=""
set cadinstall=""
set programs=""

#default options (may be overridden in file "default.conf")
set interpdefault="1"
set windefault="2"
set osdefault="1"
set machdefault="1"
set fsimdefault="yes"
set userdefault="yes"
set powerdefault="yes"
set statsdefault="yes"
set clstatsdefault="yes"
set threadsdefault="yes"

set nonomatch
if ( -d ~cad ) then
    set CADDEFAULT=~cad
else
    set CADDEFAULT=/usr/local
endif
unset nonomatch

if ( -f default.conf ) then
    source default.conf
endif

if ( $?CAD_HOME ) then
    set CADDIR=$CAD_HOME
    cad - << CAT_EOF
You have \$CAD_HOME set to "$CAD_HOME".
CAT_EOF
else
   echo -n "You do not have CAD_HOME set."
   echo    "  Set it now or press return to accept the default."
   echo -n "Enter a value of CAD_HOME [${CADDEFAULT}]: "

   set cad_home=($<)
   if ( $cad_home != "" ) then
      set CADDIR=$cad_home
   else
      set CADDIR=${CADDEFAULT}
   endif
   echo "CAD_HOME set to $CADDIR"
endif

set cflags=
set cppflags='-I. -I${IRSIMDIR} -I${IRSIMDIR}/src/irsim'
set dflags=
set oflags=

set other_srcs=

set gr_cflags=
set gr_dflags=
set gr_libs=
set gr_srcs=
set gr_hsrcs=
set gr_hprog=

cat - << CAT_EOF

The \$CAD_HOME value will be reflected in the \$CADDIR varible in the
defs.mak files. It is only needed for installation so you can change
it by modifying the defs.mak after building.
-------------------------------------------------------------------------------
IRSIM can work with the following scripting languages.
        1) None
        2) Tcl/Tk (extension)

CAT_EOF

echo -n "Please select one of the above configurations by number [$interpdefault]:  "
set interp=($<)
if ("$interp" == "") then
   set interp="$interpdefault"
else
   set interpdefault="$interp"
endif

foreach i ($interp)
switch ($i)
case "1":
    echo "No interpreter selected"
    breaksw
case "2":
    echo "Tcl/Tk interpreter extension selected"
    set usingTcl
    set dflags=($dflags -DTCL_IRSIM -DTCL_DIR=\\\"'${TCLDIR}'\\\")
    breaksw
endsw
end

if ($?usingTcl) then
cat - << CAT_EOF
-------------------------------------------------------------------------------
Tcl/IRSIM Analyzer works with the following window configurations.
Tk/BLT is preferred, if available.  You may need to get a copy of BLT
and install it, if you don't have it.
	1) None
        2) Tk/BLT
CAT_EOF
else
cat - << CAT_EOF
-------------------------------------------------------------------------------
IRSIM Analyzer works with the following window configurations.  X11 is preferred.
	1) None
        2) X11
CAT_EOF
endif

echo -n ""
echo -n "Please select one of the above configurations [$windefault]:  "
set windows=($<)
if ("$windows" == "") then
   set windows="$windefault"
else
   set windefault="$windows"
endif

foreach i ($windows)
switch ($i)
case "1":
    echo "No graphics selected.  The IRSIM analyzer will not be compiled."
    set gr_srcs=("$gr_srcs" '${NULL_SRCS}')
    breaksw
default:
    if ($?usingTcl) then
	if ( -f /usr/include/blt.h ) then
            echo "Tk/Tcl with BLT selected"
	    set gr_dflags=($gr_dflags -DBLT -DHAVE_BLT)
	else
	    echo "BLT header file not found in /usr/include.  Will attempt"
	    echo "to compile anyway, but be warned that it may be necessary"
	    echo "to install BLT for IRSIM to compile correctly."
	    set gr_dflags=($gr_dflags -DBLT)
	endif
        set gr_dflags=($gr_dflags -DBLT)
        set gr_srcs=("$gr_srcs" '${TK_SRCS}')
    else
        echo "X11 selected"
        echo "Make sure your default link and include path contains the X11"
        echo "libraries and header files. Otherwise edit LDFLAGS in defs.mak"
        echo "to include them."
        set usingX11
        set gr_dflags=($gr_dflags -DX11 -DXLIB -DX11HELP_PROG=\\\"'${X11HELP_PROG}'\\\")
        set gr_srcs=("$gr_srcs" '${X11_SRCS}')
        set gr_hsrcs=("$gr_hsrcs" '${X11HELPER_SRCS}')
        set gr_hprog=("$gr_hprog" '${X11HELP_PROG}')
	set programs=("$programs src/analyzer")
	set gr_libs=("$gr_libs" -lX11)
    endif
    breaksw
endsw
end

set extra_libs=("$extra_libs" '${IRSIMDIR}/src/analyzer/libanalyzer.o')

cat - << CAT_EOF

-------------------------------------------------------------------------------
The following operating systems are known:

         1) Linux
         2) Solaris 2.x (aka SunOS 5.x)
         3) SunOS 4.x
         4) SGI IRIX
         5) BSD Unix systems (Ultrix, pre-4.0 SunOS, RISCos4.0, etc..)
         6) SYSV Unix systems (HPUX)
	 7) Cygwin (UNIX for MS Windows)
	 8) Generic (none of the above)
CAT_EOF

echo -n "Please select one of the above operating systems by number [$osdefault]:  "

set os=$<
if ($os == "") then
   set os=$osdefault
else
   set osdefault=$os
endif

set cpp = "/lib/cpp -P"
set cc=gcc
set depend_flag=-MM
set ranlib=ranlib
echo ""

switch ($os)
case "1":
    echo "Linux selected"
    set cpp = "/lib/cpp -P -traditional"
    set dflags=("$dflags" -Dlinux -DSYSV -DF_OK=0 -DW_OK=2)
    set dflags=("$dflags" -D__NO_STRING_INLINES -DISC)
    set dflags=("$dflags" -DNO_SIGIO -DLINUX)
    set oflags=("-O6 -fomit-frame-pointer")
    if ($?usingX11) then
        set gr_cflags=("$gr_cflags" -I/usr/X11R6/include)
        set gr_libs=("$gr_libs" -L/usr/X11R6/lib/)
    endif
    if ( -f /usr/lib/libbsd-compat.a ) then
       set gr_libs=("$gr_libs" -lbsd-compat)
    else if ( -f /usr/lib/libbsd.a ) then
       set gr_libs=("$gr_libs" -lbsd)
    endif
    breaksw
case "2":
    echo "Solaris 2.x selected"
    set cpp = "/usr/ccs/lib/cpp -P"
    set cc=cc
    set depend_flag=-xM1
    if ($?OPENWINHOME) then
       set OPENHOME=${OPENWINHOME}
    else
       set OPENHOME=""
    endif
    if ($?usingX11) then
       echo "NOTE:  Since you are on a Sun using X11, we're assuming that"
       echo "       you want to get the X11 libraries and header files from"
       echo "       either the normal library and header file area or from"
       echo "       the directories ${OPENHOME}/lib and ${OPENHOME}/include."
       echo "       If this is not correct, edit the defs.mak file by"
       echo "       hand after this script completes, or re-run this script with"
       echo "       the environment variable OPENWINHOME set properly."
       set gr_cflags=("$gr_cflags" "-I${OPENHOME}/include")
       set gr_libs=("-R${OPENHOME}/lib" "-L${OPENHOME}/lib" "$gr_libs")
    endif
    set dflags=("$dflags" -DSYSV -DSYS_V -DNO_SIGIO)
    set oflags=(-fast)
    set ranlib=/bin/true
    breaksw
case "3":
    echo "Sun OS 4.0 and later (pre-Solaris) selected"
    echo "NOTE:  You might have to load the libraries libsuntool.a,"
    echo "       libsunwindow.a, and libpixrect.a from CD if your software"
    echo "       was pre-loaded at the factory."
    if ($?OPENWINHOME) then
        set OPENHOME=${OPENWINHOME}
    else
        set OPENHOME=""
    endif
    if ($?usingX11) then
        echo "NOTE:  Since you are on a Sun using X11, we're assuming that"
        echo "       you want to get the X11 library from either the normal"
        echo "       library area or from the directory ${OPENHOME}/lib.  If"
        echo "       this is not correct, edit the defs.mak file by hand"
        echo "       after this script completes, or re-run this script with"
        echo "       the environment variable OPENWINHOME set properly."
        set gr_cflags=("-I${OPENHOME}/include")
        set gr_libs=("-L${OPENHOME}/lib" "$gr_libs")
    endif
    set dflags=("$dflags" -DSYS_V -DNO_SIGIO -UULTRIX_VERSION=4)
    set cflags=("$cflags" -Bstatic)
    breaksw
case "4":
    echo "SGI/IRIX selected. "
    set compileIRIX
    set cc=cc
    set cpp = "/usr/lib/cpp -P"
    set dflags=("$dflags" -DSYSV -DNO_SIGIO -DSYS_V -DIRIX -D_BSD_SIGNALS)
    set cflags=("$cflags" -cckr)
    set oflags=(-mips4 -O3)
    set gr_cflags=("$gr_cflags" -I/usr/include/X11)
    set ranlib=/bin/true
    breaksw
case "5":
    echo 'BSD Unix selected'
    set dflags=("$dflags" -DULTRIX_VERSION=4)
    breaksw
case "6":
    echo "SYSV selected"
    set dflags=("$dflags" -DSYSV -DNO_SIGIO -DSYS_V -Uu4)
    set ranlib=/bin/true
    breaksw
case "7":
    echo "Cygwin selected"
    set cflags=("$cflags" -I/usr/X11R6/include)
    set gr_libs=(-L/usr/X11R6/lib $gr_libs)
    set dflags=("$dflags" -DCYGWIN -Di386 -DWIN32 -DNO_SIGIO)
    set usingCygwin
    set cc=gcc
    set cpp = "/bin/cpp -P -traditional"
    breaksw
default:
    echo "Unknown UNIX variant---trying generic flag settings."
    breaksw
endsw

cat - << CAT_EOF

-------------------------------------------------------------------------------
Some machines need special compile-time switches:

        1) Intel 80x86-based workstations
        2) HP 68000-based workstations
        3) None of the above

CAT_EOF

echo -n "Please select one of the above machine types by number [$machdefault]:  "

set mach=$<
if ($mach == "") then
   set mach=$machdefault
else
   set machdefault=$mach
endif

switch ($mach)
case "1":
    echo "Intel x86 workstation selected"
    if ($?usingCygwin) then
        set cflags=("$cflags"  -mpentium)
    else
        set cflags=("$cflags"  -m486)
    endif
    breaksw
case "2":
    echo "HP 68000 workstation selected"
    set dflags=("$dflags" -Dmc68000)
    breaksw
default:
    echo "Normal compilation---no additional flags."
    breaksw
endsw

cat - << CAT_EOF

-------------------------------------------------------------------------------
Choose from the following options:
   fault simulator
   power estimation
   user subcircuits
   event statistics
   connection list statistics
CAT_EOF

if (!($?usingTcl)) then
cat - << CAT_EOF
   threaded graphics
CAT_EOF
endif

echo ""
echo -n "Include fault simulator? [$fsimdefault]  "
set fsim=$<
if ($fsim == "") then
    set fsim=$fsimdefault
else
    set fsimdefault=$fsim
endif
if ($fsim != "no" && $fsim != "n" && $fsim != "yes" && $fsim != "y") then
    echo Unrecognized answer:  $fsim
    echo "Exiting."
    exit 5
endif
if ($fsim == "y" || $fsim == "yes") then
    set dflags=("$dflags" -DFAULT_SIM)
    set modules=("$modules src/faultsim")
    set extra_libs=("$extra_libs" '${IRSIMDIR}/src/faultsim/libfaultsim.o')
else
    set other_srcs=("$other_srcs incstub.c");
endif

echo -n "Include power estimation? [$powerdefault]  "
set power=$<
if ($power == "") then
    set power=$powerdefault
else
    set powerdefault=$power
endif
if ($power != "no" && $power != "n" && $power != "yes" && $power != "y") then
    echo Unrecognized answer:  $power
    echo "Exiting."
    exit 5
endif
if ($power == "y" || $power == "yes") then
    set dflags=("$dflags" -DPOWER_EST)
endif

if ($?usingTcl) then
echo "User subcircuits currently unavailable in Tcl version"
set userckt="no"
else

echo -n "Include user subcircuits? [$userdefault]  "
set userckt=$<
if ($userckt == "") then
    set userckt=$userdefault
else
    set userdefault=$userckt
endif
if ($userckt != "no" && $userckt != "n" && $userckt != "yes" && $userckt != "y") then
    echo Unrecognized answer:  $userckt
    echo "Exiting."
    exit 5
endif
if ($userckt == "y" || $userckt == "yes") then
    set dflags=("$dflags" -DUSER_SUBCKT)
    set modules=("$modules src/usersubckt")
    set extra_libs=("$extra_libs" '${IRSIMDIR}/src/usersubckt/libusersubckt.o')
endif

endif

echo -n "Include event statistics? [$statsdefault]  "
set stat=$<
if ($stat == "") then
    set stat=$statsdefault
else
    set statsdefault=$stat
endif
if ($stat != "no" && $stat != "n" && $stat != "yes" && $stat != "y") then
    echo Unrecognized answer:  $stat
    echo "Exiting."
    exit 5
endif
if ($stat == "y" || $stat == "yes") then
    set dflags=("$dflags" -DSTATS)
endif

echo -n "Include connection list statistics? [$clstatsdefault]  "
set clstat=$<
if ($clstat == "") then
    set clstat=$clstatsdefault
else
    set clstatsdefault=$clstat
endif
if ($clstat != "no" && $clstat != "n" && $clstat != "yes" && $clstat != "y") then
    echo Unrecognized answer:  $clstat
    echo "Exiting."
    exit 5
endif
if ($clstat == "y" || $clstat == "yes") then
    set dflags=("$dflags" -DCL_STATS)
endif

if ($?usingTcl) then
set threads="yes"
else
if ($?usingX11) then
echo -n "Use threaded graphics? [$threadsdefault]  "
set threads=$<
if ($threads == "") then
    set threads=$threadsdefault
else
    set threadsdefault=$threads
endif
if ($threads != "no" && $threads != "n" && $threads != "yes" && $threads != "y") then
    echo Unrecognized answer:  $threads
    echo "Exiting."
    exit 5
endif
#
# pthreads check taken from magic's config script
#
if ($threads == "y" || $threads == "yes") then
    if ( -f /usr/lib/libpthread.so || -f /usr/lib/libpthread.a ) then
	set gr_dflags=("$gr_dflags" -DHAVE_PTHREADS)
	set gr_libs=($gr_libs -lpthread)
	set gr_srcs=("$gr_srcs" 'thread.c')
	set gr_hsrcs=
	set gr_hprog=
    endif
endif
# usingX11
endif
# !usingTcl
endif

set cflags=(-g "$cflags")

# add CADDIR to dflags
set dflags=($dflags -DCAD_DIR=\\\"'${CADDIR}'\\\")

cat - << CAT_EOF

Updating 'defs.mak' file.
Everything that all the Makefiles use is in this file. You can change
compilers, compiler flags, and libraries there if necessary.
CAT_EOF

if ( -f defs.mak ) then
    mv -f defs.mak old.defs.mak >& /dev/null
endif

cat > defs.mak << CAT_EOF
# This file was auto-generated by running the scripts/config program. Feel free
# to change the values in here to suit your needs. Beware that running scripts/config
# again will overwrite any changes!

SHELL                  = /bin/sh

VERSION                = $VERSION

# Change CADDIR to install in a different place
CADDIR                 = $CADDIR
BINDIR                 = \${CADDIR}/bin
LIBDIR                 = \${CADDIR}/lib
MANDIR                 = \${CADDIR}/man
DOCDIR                 = \${CADDIR}/doc
TCLDIR                 = \${CADDIR}/lib/irsim/tcl
PRMDIR                 = \${CADDIR}/lib/irsim

SCRIPTS                = \${IRSIMDIR}/scripts
MAIN_EXTRA_LIBS        = $extra_libs

MODULES               += $modules
PROGRAMS              += $programs
INSTALL_CAD_DIRS      += $cadinstall

# Define your make environment
# MAKE                   = gmake
# MAKEFLAGS              = -j4

RM                     = rm -f
CP                     = cp
AR                     = ar
ARFLAGS                = crv
LD                     = ld -r
RANLIB                 = $ranlib

CC                     = $cc
CPP                    = $cpp
CPPFLAGS               = $cppflags
DFLAGS                 = $dflags
CFLAGS                 = $cflags
# These CFLAGS may include switches for an optimized compile
# CFLAGS                 = $cflags $oflags

DEPEND_FLAG            = $depend_flag
DEPEND_FILE            = Depend

GR_CFLAGS              = $gr_cflags
GR_DFLAGS              = $gr_dflags
GR_LIBS                = $gr_libs
GR_SRCS                = $gr_srcs
GR_HELPER_SRCS         = $gr_hsrcs
GR_HELPER_PROG         = $gr_hprog

OTHER_SRCS	       = $other_srcs

OBJS      = \${SRCS:.c=.o}
LIB_OBJS  = \${LIB_SRCS:.c=.o}
CLEANS    = \${OBJS} \${LIB_OBJS} lib\${MODULE}.a lib\${MODULE}.o \${MODULE}
CAT_EOF

cat - << CAT_EOF

Configuration completed.  The following files were changed:

        defs.mak (old version in old.defs.mak)

CAT_EOF

if ($?usingTcl) then
cat - << CAT_EOF
Next step: type "make tcl".
CAT_EOF
else
cat - << CAT_EOF
Next step: type "make".
CAT_EOF
endif

cat - << CAT_EOF

After it completes, you'll have the irsim binary in the src/irsim directory
CAT_EOF
if (!($?usingTcl)) then
cat - << CAT_EOF
and a graphics event capture program in the src/analyzer subdirectory.
CAT_EOF
endif

if ($?usingTcl) then
cat - << CAT_EOF
Final step: type "make install-tcl".
CAT_EOF
else
cat - << CAT_EOF
Final step: type "make install".
CAT_EOF
endif

cat - << CAT_EOF

This will install the appropriate files in "$CADDIR".

CAT_EOF

if (!($?usingTcl)) then
cat - << CAT_EOF

When running, irsim needs to find the anXhelper program in your path.
The easiest way to do this is to:

        setenv CAD_HOME $CADDIR
        set path = ( \$path $CADDIR/bin )
CAT_EOF
endif

cat - << CAT_EOF

If you have any problems, please read a man page or two and try
to figure it out. If you're stuck, send us (magic-hackers@csl.cornell.edu)
mail and we'll try to help.
CAT_EOF

cat > default.conf << CAT_EOF
# This file was auto-generated by running the scripts/config program. Feel free
# to change the values in here to suit your needs. Beware that running
# scripts/config again will overwrite any changes!
set CADDEFAULT="$CADDIR"
set interpdefault="$interpdefault"
set windefault="$windefault"
set osdefault="$osdefault"
set machdefault="$machdefault"
set fsimdefault="$fsimdefault"
set userdefault="$userdefault"
set powerdefault="$powerdefault"
set statsdefault="$statsdefault"
set clstatsdefault="$clstatsdefault"

CAT_EOF
