#!/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 mac_extra_libs=""
set mac_extra_objs=""
set cadinstall=""
set programs=""

#default options (may be overridden in file "default.conf")
set CADDEFAULT=$1
set interpdefault="1"
set windefault="1"
set widgetdefault="1"

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

# Here's a partial implementation of GNU configure; at least to get
# the OS and machine-dependent bits.

set host_alias=`${SCRIPTS}/config.guess`
set host=`${SCRIPTS}/config.sub $host_alias`
set host_cpu=`echo $host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'`
set host_os=`echo $host | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'`
echo "Operating system is $host_os running on a $host_cpu"
echo ""

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 lddl_flags=

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=($<)
set needclean=""
if ("$interp" == "") then
   set interp="$interpdefault"
else
   if ( "$interp" != "$interpdefault" ) then
      if (!( -z defs.mak )) then
	 set needclean="yes"
      endif
   endif
   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

# Query the location of Tcl/Tk include and library files

if ($?usingTcl) then
   switch ($host_os)
      case darwin*:
         set lddexe="otool -L"
         set cfield=1  
         breaksw
      default:
         set lddexe="ldd"
         set cfield=3
         breaksw
   endsw

   set wishexe=`which wish | tail -1`
   set tclshareddir=`$lddexe $wishexe | grep libtcl | cut -d':' -f1 | cut -d' ' -f${cfield}`
   set tcllibdir=`echo $tclshareddir | sed -e's/\/libtcl.*$//'`
   set tclshared=`echo $tclshareddir | sed -e's/^.*libtcl/tcl/' -e's/\.[^\.]*$//'`
   set tclincldir=`echo $tcllibdir | sed -e 's/\/lib//'`/include  

   if ( -d $tclincldir ) then
      if ( -f ${tclincldir}/tcl.h ) then
         echo "Tcl include directory is $tclincldir"
      else
	 unset tclincldir
      endif
   else
      unset tclincldir
   endif

   set tkshareddir=`$lddexe $wishexe | grep libtk | cut -d':' -f1 | cut -d' ' -f${cfield}` 
   set tklibdir=`echo $tkshareddir | sed -e's/\/libtk.*$//'`
   set tkshared=`echo $tkshareddir | sed -e's/^.*libtk/tk/' -e's/\.[^\.]*$//'`
   set tkincldir=`echo $tklibdir | sed -e 's/\/lib//'`/include

   if ( -d $tkincldir ) then 
      if ( -f ${tkincldir}/tk.h ) then  
         echo "Tk include directory is $tkincldir"  
      else
         unset tkincldir
      endif
   else
      unset tkincldir
   endif

   if (!($?tclincldir)) then
      echo -n "Tcl include file directory: "
      set tclincldir=($<)
   endif
   set cflags=("$cflags" -I${tclincldir})

   if (!($?tkincldir)) then
      echo -n "Tk include file directory: "
      set tkincldir=($<)
   endif
   set cflags=("$cflags" -I${tkincldir})

endif

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

switch ($host_os)
   case darwin*:
      set shdlib_ext = ".dylib"
      # Avoid "-flat_namespace" and "-undefined suppress" if at all possible. . .
      # set lddl_flags = "-dynamiclib -flat_namespace -undefined suppress -bind_at_load"
      set lddl_flags = "-dynamiclib -bind_at_load"
      breaksw
   default:
      set shdlib_ext = ".so"
      set lddl_flags = "-shared"
      breaksw
endsw

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

switch ($host_os)
case linux*:
    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")
    set lddl_flags=("${lddl_flags}" '-Wl,--version-script=${NETGENDIR}/src/symbol.map')
    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 solaris*:
    if ( -f /opt/sfw/bin/cpp ) then
	set cpp="/opt/sfw/bin/cpp -P"
    else if ( -f /usr/ccs/lib/cpp ) then
	set cpp="/usr/ccs/lib/cpp -P"
    else
	set cpp="cpp -P"
    endif

    if ( -d /opt/sfw/lib ) then
	set cc="gcc -I/opt/sfw/include -L/opt/sfw/lib"
	set lddl_flags="-shared -fpic -mimpure-text"
    else
	set cc=cc
	set lddl_flags="-G"
    endif
    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 sunos*:
    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 irix*:
    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 bsd*:
    set dflags=("$dflags" -DULTRIX_VERSION=4)
    breaksw
case sysv*:
    set dflags=("$dflags" -DSYSV -DNO_SIGIO -DSYS_V -Uu4)
    set ranlib=/bin/true
    breaksw
case cygwin*:
    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
case darwin*:
    set cpp = "/usr/bin/cpp -P"
    if ($?usingX11) then
	echo "NOTE:  Since you are running Mac OS X using X11 (i.e. XDarwin), we're assuming"
	echo "       that you want to get the X11 library from either the normal"
 	echo "       library area or from the directory /usr/X11R6/lib.  If this"
 	echo "       is not correct, edit the defs.make file by hand after this"
	echo "       script completes."
        set dflags=("$dflags" -Dmacosx)
	set cflags=("$cflags" -I/sw/include -O2 -fno-common)
    endif
    if ($?usingTcl) then

	if ( -d $tcllibdir ) then
	    echo "Tcl library directory is $tcllibdir"
	else
	    unset tcllibdir
	    unset tclshared
	endif

	if ( -d $tklibdir ) then
	    echo "Tk library directory is $tklibdir"  
	else
	    unset tklibdir
	    unset tkshared
	endif

	if (!($?tcllibdir)) then
	    echo -n "Tcl shared object library directory: "
	    set tcllibdir=($<)
	endif
	if (!($?tclshared)) then
	    echo -n "Tcl shared object: "
	    set tclshared=($<)
	endif
	if (!($?tklibdir)) then
	    echo -n "Tk shared object library directory: "
	    set tklibdir=($<)
	endif
	if (!($?tkshared)) then
	    echo -n "Tk shared object: "
	    set tkshared=($<)
	endif
	set mac_extra_libs =("$mac_extra_libs" -L${tcllibdir} -L${tklibdir} \
		-l${tclshared} -l${tkshared})
	set mac_extra_objs =("$mac_extra_objs" '${MAGICDIR}/magic/tclmagic${SHDLIB_EXT}')
    endif
    breaksw
default:
    breaksw
endsw

switch ($host_cpu)
case i486*:
case i586*:
case i686*:
    if ($?usingCygwin) then
        set cflags=("$cflags"  -mpentium)
    else
        set cflags=("$cflags"  -m486)
    endif
    breaksw
case m68k*:
    set dflags=("$dflags" -Dmc68000)
    breaksw
default:
    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
   if (!( -z defs.mak )) then
      mv -f defs.mak old.defs.mak >& /dev/null
   endif
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
MAC_EXTRA_LIBS	       = $mac_extra_libs
MAC_SHARED	       = $mac_extra_objs

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
SHDLIB_EXT	       = $shdlib_ext
LDDL_FLAGS	       = $lddl_flags
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 ("$needclean" == "yes") then
cat - << CAT_EOF 
You have changed interpreters, so you MUST do "make clean" first.
CAT_EOF
endif 

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 widgetdefault="$widgetdefault"

CAT_EOF
