#!/bin/csh -f

set VERSION="1.1"

echo ""
echo "		Welcome to Netgen's configuration program."
echo "		This is Netgen 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="1"
set osdefault="1"
set machdefault="1"
set widgetdefault="1"

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${NETGENDIR}'
set dflags=
set oflags=

set gr_cflags=
set gr_dflags=
set gr_libs=
set gr_srcs=

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.
-------------------------------------------------------------------------------
Netgen 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_NETGEN -DTCL_DIR=\\\"'${TCLDIR}'\\\")
    set gr_srcs=("$gr_srcs" '${TCL_SRCS}')
    breaksw
endsw
end

# This is pretty much a requirement until I get the "dbug" package to compile
# under an ANSI interpreter
set dflags=($dflags -DDBUG_OFF)

if (! $?usingTcl) then
cat - << CAT_EOF
-------------------------------------------------------------------------------
Netgen works with the following window configurations.  "None" is preferred.
	1) None
        2) X11
CAT_EOF

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 Netgen GUI frontend will not be enabled."
    breaksw
default:
    if (! $?usingTcl) then
        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 -DHAVE_X11)
        set gr_srcs=("$gr_srcs" '${X11_SRCS}')
    endif
    breaksw
endsw
end

if ($?usingX11) then
# Here---determine what widget sets are available (Athena, Motif, Xw)?
cat - << CAT_EOF
-------------------------------------------------------------------------------
Netgen works with the following widget configurations.  "Motif" is preferred.
	1) Athena (Xaw)
        2) Motif (Xm)
	3) HP Widgets (Xw)

CAT_EOF

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

foreach i ($widget)
switch ($i)
case "2":
    echo "Motif widgets (Xm) selected"
    set gr_dflags=($gr_dflags -DX11_MOTIF_WIDGETS)
    set gr_libs=($gr_libs -lXm -lXt)
    breaksw
case "3":
    echo "HP widgets (Xw) selected"
    set gr_dflags=($gr_dflags -DX11_MOTIF_WIDGETS)
    set gr_libs=($gr_libs -lXw -lXt)
    breaksw
default:
    echo "Athena widgets (Xaw) selected"
    set gr_dflags=($gr_dflags -DX11_ATHENA_WIDGETS)
    set gr_libs=($gr_libs -lXaw -lXmu -lXt -lXext)
    breaksw
endsw
end

set gr_libs=($gr_libs -lX11)
endif
# (endif for ($?usingX11))

endif
# (endif for !($?usingTcl))

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 -DHAVE_RE_COMP)
    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

# now go build the makefile

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/netgen/tcl

SCRIPTS                = \${NETGENDIR}/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

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 netgen binary in the src/netgen directory
CAT_EOF

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

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 me (tim@bach.ece.jhu.edu)
mail and I'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 widgetdefault="$widgetdefault"

CAT_EOF
