#!/bin/csh -f
# rcsid="$Header: /usr/cvsroot/magic/scripts/config,v 1.1.1.1 2005/05/09 23:29:24 tim Exp $"
# config now takes one argument, which is the value to set CADDEFAULT.  This
# is because csh exits if ~cad does not exists, and not all versions of csh
# (e.g., some Solaris versions) recognize "set nonomatch".  "sh" and variants
# handle tilde expansion much more gracefully, so ~cad is evaluated in the
# Makefile (sh) and passed as the single argument to scripts/config.

set MAGIC_VERSION=`cat VERSION | cut -d. -f1-2`
set MAGIC_REVISION=`cat VERSION | cut -d. -f3`

echo ""
echo "-------------------------------------------------------"
echo "The 'make config' script is deprecated.  Please use the"
echo "following GNU autoconf compile procedure instead:"
echo ""
echo "   ./configure [options]"
echo "   make"
echo "   make install"
echo "-------------------------------------------------------"
echo ""

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

# default options (may be overridden in file "default.conf")
set CADDEFAULT=$1
set interpdefault="1"
set windefault="1"
set calmadefault="yes"
set oglrenderdefault="yes"
set cifdefault="yes"
set plotdefault="yes"
set filelockdefault="no"
set readlinedefault="yes"
set threadeddefault="yes"
set nonmanhdefault="yes"
set routedefault="yes"
set rsimdefault="yes"
set lefdefault="yes"
set newdotdefault="yes"

if ( -f ${SCRIPTS}/default.conf ) then
    source ${SCRIPTS}/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
  cat - << 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${MAGICDIR}'
set dflags=(-DMAGIC_VERSION=\\\"'${MAGIC_VERSION}'\\\")
set dflags=("$dflags" -DMAGIC_REVISION=\\\"'${MAGIC_REVISION}'\\\")
set oflags=
set lddl_flags=

set rl_defs=
set rl_libs=

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.

You can create other custom configurations yourself.  Read the
maintainer's manuals for details.  In particular, you can have more than
one graphics driver installed.
 
We haven't been able to test all of these configurations.  If 
you develop modifications to Magic or to this configuration program,
please let us (magic-hackers@csl.cornell.edu) know.
-------------------------------------------------------------------------------
Magic can work with the following scripting languages.
	1) None
	2) SCHEME (embedded)
	3) 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"
    set unused=("$unused" lisp)
    breaksw
case "2":
    echo "Embedded SCHEME interpreter selected"
    set dflags=("$dflags" -DSCHEME_INTERPRETER)
    set modules=("$modules" lisp)
    set cadinstall=("$cadinstall" lisp)
    set extra_libs=("$extra_libs" '${MAGICDIR}/lisp/liblisp.o')
    breaksw
case "3":
    echo "Tcl/Tk interpreter extension selected"
    set usingTcl
    set dflags=("$dflags" -DMAGIC_WRAPPER -DTCL_DIR=\\\"'${TCLDIR}'\\\")
    set unused=("$unused" lisp)
    breaksw
endsw
end

# Some complicated stuff to find library and include file paths for Tcl.
# We make an effort to find directories in standard places.  If this fails,
# we query the user for the information.

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

# These programs are only valid for non-Tcl use
if (!($?usingTcl)) then
    set programs=("$programs" magicusage extcheck graphics)
    set all_target="tcl"
    set install_target="install-tcl"
else
    set cadinstall=("$cadinstall" graphics)
    set all_target="magic"
    set install_target="install-magic"
endif

cat - << CAT_EOF

-------------------------------------------------------------------------------
Magic works with the following window configurations.  OpenGL + X11 is preferred,
where OpenGL is available and rendering is fast.
	1) X11
	2) OpenGL
CAT_EOF

echo ""
echo "Please select one or more of the above configurations by number."
echo -n "Multiple choices should be separated by whitespace [$windefault]:  "
set windows=($<)
if ("$windows" == "") then
   set windows="$windefault"
else
   set windefault="$windows"
endif

foreach i ($windows)
switch ($i)
case "1":
    if ($?usingTcl) then
	echo "Tk/Tcl with X11 selected"
	set usingX11
	set gr_dflags=($gr_dflags -DX11 -DXLIB)
	set gr_libs=("$gr_libs" -lX11)
	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_libs=("$gr_libs" -lX11)
	set gr_srcs=("$gr_srcs" '${X11_SRCS}')
	set gr_hsrcs=("$gr_hsrcs" '${X11HELPER_SRCS}')
	set gr_hprog=("$gr_hprog" '${X11HELP_PROG}')
    endif
    breaksw
case "2":
    if ($?usingTcl) then
	echo "Tk/Tcl with OpenGL selected"
	set usingOGL
	set dflags=("$dflags" -DTHREE_D)
	set gr_dflags=("$gr_dflags" -DOGL)
	set gr_libs=("$gr_libs" -lGLU -lGL -lXi -lXmu -lXext -lm)
	set gr_srcs=("$gr_srcs" '${TOGL_SRCS}')
	if (!($?usingX11)) then
	    set usingX11
	    set gr_dflags=($gr_dflags -DXLIB)
	    set gr_libs=("$gr_libs" -lX11)
	endif
    else
	echo "OpenGL selected"
	set usingOGL
	set gr_dflags=("$gr_dflags" -DOGL)
	set gr_libs=("$gr_libs" -lGLU -lGL -lXi -lXmu -lXext -lm)
	set gr_srcs=("$gr_srcs" '${OGL_SRCS}')
	if (!($?usingX11)) then
	    set usingX11
	    set gr_libs=("$gr_libs" -lX11)
	    set gr_dflags=("$gr_dflags" -DXLIB -DX11HELP_PROG=\\\"'${X11HELP_PROG}'\\\")
	    set gr_hsrcs=("$gr_hsrcs" '${X11HELPER_SRCS}')
	    set gr_hprog=("$gr_hprog" '${X11HELP_PROG}')
        endif
    endif
    breaksw
default:
    echo "Unrecognized selection:  $i"
    echo "Exiting."
    exit 1
    breaksw
endsw
end

if ($?usingTcl) then
    set gr_srcs=("$gr_srcs" 'grTkCommon.c')
endif

switch ($host_os)
   case darwin*:
      set shdlib_ext = ".dylib"
      # "-flat_namespace" and "-undefined suppress" reinstated because the "symbol.map"
      # file explicitly defines which symbols are exported and which are local.
      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 dflags=("$dflags" -DSHDLIB_EXT=\\\"${shdlib_ext}\\\")

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

# Check for "limits.h" or "sys/param.h"  
# This is for dealing with DoubleInt arithmetic in utils/double.c.
# Note that it is not necessary to find either of these files, as the
# behavior will default to DoubleInt = long long int.
#
if ( -f /usr/include/limits.h ) then
  set dflags=("$dflags" -DHAVE_LIMITS_H)
else if ( -f /usr/include/sys/param.h ) then
  set dflags=("$dflags" -DHAVE_SYS_PARAM_H) 
endif       

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 -DHAVE_VA_COPY)
    set oflags=("-O6 -fomit-frame-pointer")
    set lddl_flags=("${lddl_flags}" '-Wl,--version-script=${MAGICDIR}/magic/symbol.map')
    if ($?usingOGL) then
	set gr_libs=("$gr_libs" -lstdc++)
    endif
    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 netbsd*:
    set cpp = "/usr/libexec/cpp -P -traditional"
    if ($?usingX11) then
	echo "NOTE:  Since you are running NetBSD using X11, 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.mak file by hand after this"
	echo "       script completes."
    set gr_cflags=("$gr_cflags" -I/usr/X11R6/include)
 	set gr_libs=(-L/usr/X11R6/lib "$gr_libs")
    endif
    breaksw
case freebsd*:
    set cpp = "/usr/libexec/cpp -P -traditional"
    if ($?usingX11) then
	echo "NOTE:  Since you are running FreeBSD using X11, 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 gr_cflags=("$gr_cflags" -I/usr/X11R6/include)
 	set gr_libs=(-L/usr/X11R6/lib "$gr_libs")
    endif
    breaksw
case osf*:
    set dflags=("$dflags" -DALPHA -DUSE_IO_PROBE)
    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)
    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 cflags=("$cflags" -Bstatic)
    breaksw
case irix*:
    echo "SGI/IRIX selected. "
    set compileIRIX
    set cc=cc
    set cpp = "/usr/lib/cpp -P"
    set dflags=("$dflags" -DSYSV -DIRIX -D_BSD_SIGNALS)
# These values do not seem to be warranted (which IRIX system needs them?)
#   set cflags=("$cflags" -cckr -n32 -woff 1174,1116,1233,1552,1009,1171,1014,1177)
#   set oflags=(-mips4 -O3 -TARG:platform=ip27 -OPT:Olimit=0:roundoff=3:div_split=ON:alias=typed)
    set cflags=("$cflags" -cckr)
    set oflags=(-mips4 -O3)
    set gr_cflags=("$gr_cflags" -I/usr/include/X11)
    set ranlib=/bin/true
    breaksw
case os2*:
    echo "OS/2 Warp selected"
    set dflags=("$dflags" -DOS2)
    breaksw
case sysv*:
    set dflags=("$dflags" -DSYSV )
    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)
    set exeext=".exe"
    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/usr/X11R6/include -I/sw/include -O2 -fno-common)
	set gr_cflags=("$gr_cflags" -I/usr/X11R6/include -I/sw/include)
 	set gr_libs=(-L/usr/X11R6/lib "$gr_libs")
    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
    endif
    breaksw
case hpux*:
    set dflags=("$dflags" -DSYSV -DHAVE_VA_COPY)
    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
case mips*:
    set cflags=("$cflags" -systype bsd43 -O -Olimit 3000 -signed)
    breaksw
endsw

#
# Workaround for broken OpenGL client-side rendering
#
if ($?usingOGL) then
cat - << CAT_EOF

-------------------------------------------------------------------------
OpenGL can render faster with client-side rendering.  This feature is
broken in a few video card drivers and may cause parts of the layout
to be drawn in incorrect colors and/or styles.  If this happens,
reconfigure with client-side rendering option = no and recompile.

CAT_EOF

echo -n "Use client-side rendering? [$oglrenderdefault]  "

set oglrender=$<
if ($oglrender == "") then
    set oglrender=$oglrenderdefault
else
    set oglrenderdefault=$oglrender
endif
if ($oglrender != "no" && $oglrender != "n" && $oglrender != "yes" && $oglrender != "y") then
    echo Unrecognized answer:  $oglrender
    echo "Exiting."
    exit 5
endif
if ($oglrender == "n" || $oglrender == "no") then
    set gr_dflags=("$gr_dflags" -DOGL_SERVER_SIDE_ONLY)
endif

endif

cat - << CAT_EOF

-------------------------------------------------------------------------------
Sometimes is it handy to eliminate modules from Magic, to make it smaller.
The following modules are optional:
    CALMA - module to read and write GDS-II format
    CIF - module to read and write CIF
    PLOT - plotting code
    READLINE - readline support on the command line
    THREADS - support for multithreaded graphics in X11
    NONMANHATTAN - support for non-manhattan geometry
    ROUTE - a whole bunch of routing tools
    RSIM - an interface to (I)RSIM
    .magic - choice of new or old system startup (macro definition) file

CAT_EOF

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

Magic under Tcl/Tk allows some modules to be compiled as auto-loading
modules.  To do so, set the option to "module".  The feature will be
compiled as a separate library which will automatically be loaded when
the feature is first invoked.

CAT_EOF

else

# Any options set to "module" need to be reset to "yes" if we're
# not compiling under the Tcl interpreter.

    if ($routedefault == "m" || $routedefault == "module") then
	set routedefault="yes"
    endif
    if ($plotdefault == "m" || $plotdefault == "module") then
	set plotdefault="yes"
    endif
    if ($lefdefault == "m" || $lefdefault == "module") then
	set lefdefault="yes"
    endif

endif

echo -n "Include CALMA module? [$calmadefault]  "
set calma=$<
if ($calma == "") then 
    set calma=$calmadefault
else
    set calmadefault=$calma
endif
if ($calma != "no" && $calma != "n" && $calma != "yes" && $calma != "y") then
    echo Unrecognized answer:  $calma
    echo "Exiting."
    exit 5
endif
if ($calma == "y" || $calma == "yes") then
    set dflags=("$dflags" -DCALMA_MODULE)
    set modules=("$modules" calma)
    set extra_libs=("$extra_libs" '${MAGICDIR}/calma/libcalma.o')
else
    set unused=("$unused" calma)
endif

echo -n "Include CIF module? [$cifdefault]  "
set cif=$<
if ($cif == "") then 
    set cif=$cifdefault
else
    set cifdefault=$cif
endif
if ($cif != "no" && $cif != "n" && $cif != "yes" && $cif != "y") then
    echo Unrecognized answer:  $cif
    echo "Exiting."
    exit 5
endif
if ($cif == "y" || $cif == "yes") then
    set dflags=("$dflags" -DCIF_MODULE)
    set modules=("$modules" cif)
    set extra_libs=("$extra_libs" '${MAGICDIR}/cif/libcif.o')
else
    set unused=("$unused" cif)
endif

echo -n "Include PLOT module? [$plotdefault]  "
set plot=$<
if ($plot == "") then 
    set plot=$plotdefault
else
    set plotdefault=$plot
endif
if ($plot != "no" && $plot != "n" && $plot != "yes" && $plot != "y" \
	&& $plot != "m" && $plot != "module") then
    echo Unrecognized answer:  $plot
    echo "Exiting."
    exit 5
endif
if ($?usingTcl) then
    if ($plot == "m" || $plot == "module") then
	set dflags=("$dflags" -DPLOT_AUTO)
	set programs=("$programs" plot)
    endif
else
    if ($plot == "m" || $plot == "module") then
	echo Module option only available under Tcl interpreter.
	echo "Exiting."
	exit 5
    endif
endif
if ($plot == "y" || $plot == "yes") then
    set dflags=("$dflags" -DPLOT_MODULE)
    set modules=("$modules" plot)
    set extra_libs=("$extra_libs" '${MAGICDIR}/plot/libplot.o')
endif
if ($plot == "n" || $plot == "no") then
    set unused=("$unused" plot)
endif

echo -n "Include multiuser file-locking? [$filelockdefault] "
set filelock=$<
if ($filelock == "") then 
    set filelock=$filelockdefault
else
    set filelockdefault=$filelock
endif
if ($filelock != "no" && $filelock != "n" && $filelock != "yes" && $filelock != "y") then
    echo Unrecognized answer:  $filelock
    echo "Exiting."
    exit 5
endif
if ($filelock == "yes" || $filelock == "y") then
    set dflags=("$dflags" -DFILE_LOCKS)
endif

if ($?usingTcl) then
   echo "Using Tcl: READLINE module not available (see documentation)"
   set unused=("$unused" readline)
else

echo "Include READLINE module? [$readlinedefault] "
echo -n "  (use 'force' to force compilation of internal readline version) "
set readline=$<
if ($readline == "") then 
    set readline=$readlinedefault
else
    set readlinedefault=$readline
endif
if ($readline != "no" && $readline != "n" && $readline != "yes" && $readline != "y" && $readline != "force") then
    echo Unrecognized answer:  $readline
    echo "Exiting."
    exit 5
endif
# check for readline on system first. . .
# readline must be version 4.0 or better (contains function rl_pre_input_hook())
#
set have_readline="no"
if ($readline == "y" || $readline == "yes" || $readline == "force") then
    set dflags=("$dflags" -DUSE_READLINE)
# some systems are now putting termcap functions in ncurses. . . check this!
    set termlib=-ltermcap
    if ( -f /usr/lib/libncurses.so || -f /usr/lib/libncurses.a || -f /usr/lib/libncurses.dylib ) then
	if ( -f /usr/lib/libncurses.a ) then
	    if ( `nm /usr/lib/libncurses.a | grep tgetent | wc -l` > 0 ) then
		set termlib=-lncurses
	    endif
	else
	    if ( `nm /usr/lib/libncurses.so | grep tgetent | wc -l` > 0 ) then
		set termlib=-lncurses
	    endif
	    if ( `nm /usr/lib/libncurses.dylib | grep tgetent | wc -l` > 0 ) then
		set termlib=-lncurses
	    endif
	endif
    else if (!( -f /usr/lib/libtermcap.so || -f /usr/lib/libtermcap.a )) then
	echo "Cannot find termcap library functions. . . compiling without readline"
	set readline="no"
    endif

# Use system readline if a shared library exists and is at least version 4.2.
# Otherwise, compile and use the version included with the magic distribution.

    if ( $readline != "no" && $readline != "force" && -f /usr/lib/libreadline.so ) then
	if ( `nm /usr/lib/libreadline.so | grep rl_filename_completion_function | wc -l` > 0) then
	    set have_readline="yes"
	    set dflags=("$dflags" -DHAVE_READLINE)
	    set rl_libs=("$rl_libs" -lreadline ${termlib}) 
	endif
    endif
    if ($?compileIRIX) then
       echo "NOTE:  Must compile with GNU make (gmake), not the IRIX make"
    endif
endif

if ($have_readline == "no" && ($readline == "y" || $readline == "yes" || $readline == "force")) then
    set modules=("$modules" readline)
    set cppflags=("$cppflags" '-I${MAGICDIR}/readline')
    set rl_libs=("$rl_libs" '-L${MAGICDIR}/readline/readline' -lreadline ${termlib})
    if ($?compileIRIX) then
	set rl_defs=("-DNEED_EXTERN_PC")
	set rl_libs=("$rl_libs" -lm)
    endif
endif

# !usingTcl . . .
else
    set unused=("$unused" readline)
endif

if ($?usingTcl) then
set threaded="yes"
else
if ($?usingX11) then
echo -n "Include THREADS support? [$threadeddefault]  "
set threaded=$<
if ($threaded == "") then 
    set threaded=$threadeddefault
else
    set threadeddefault=$threaded
endif
if ($threaded != "no" && $threaded != "n" && $threaded != "yes" && $threaded != "y") then
    echo Unrecognized answer:  $threaded
    echo "Exiting."
    exit 5
endif  
#
# check for pthreads on system first. . .
#
if ($threaded == "y" || $threaded == "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" 'grX11thread.c')
	set gr_hsrcs=
	set gr_hprog=
    endif
endif
# usingX11
endif
# !usingTcl
endif


echo -n "Include NONMANHATTAN extensions? [$nonmanhdefault]  "
set nonmanh=$<
if ($nonmanh == "") then 
    set nonmanh=$nonmanhdefault
else
    set nonmanhdefault=$nonmanh
endif
if ($nonmanh != "no" && $nonmanh != "n" && $nonmanh != "yes" && $nonmanh != "y") then
    echo Unrecognized answer:  $nomanh
    echo "Exiting."
    exit 5
endif
if ($nonmanh == "y" || $nonmanh == "yes") then
    set dflags=("$dflags" -DNONMANHATTAN)
endif

echo -n "Include ROUTE module? [$routedefault]  "
set route=$<
if ($route == "") then
    set route=$routedefault
else
    set routedefault=$route
endif
if ($route != "no" && $route != "n" && $route != "yes" && $route != "y" \
	&& $route != "m" && $route != "module") then
    echo Unrecognized answer:  $route
    echo "Exiting."
    exit 5
endif
if ($?usingTcl) then
    if ($route == "m" || $route == "module") then
	set dflags=("$dflags" -DROUTE_AUTO)
	set modules=("$modules" garouter grouter irouter mzrouter gcr)
	set programs=("$programs" router)
    endif
else
    if ($route == "m" || $route == "module") then
	echo Module option only available under Tcl interpreter.
	echo "Exiting."
	exit 5
    endif
endif
if ($route == "y" || $route == "yes") then
    set dflags=("$dflags" -DROUTE_MODULE)
    set modules=("$modules" garouter grouter irouter mzrouter router gcr)
    set programs=("$programs" net2ir)
    set extra_libs=("$extra_libs" '${MAGICDIR}/garouter/libgarouter.o' \
	' ${MAGICDIR}/mzrouter/libmzrouter.o ${MAGICDIR}/router/librouter.o' \
	' ${MAGICDIR}/irouter/libirouter.o ${MAGICDIR}/grouter/libgrouter.o' \
	' ${MAGICDIR}/gcr/libgcr.o')
endif
if ($route == "n" || $route == "no") then
   set unused=("$unused" garouter grouter irouter mzrouter router gcr)
endif

if ($?usingTcl) then
    echo "Using Tcl: RSIM modules not available (see documentation)"
else

echo -n "Include RSIM module? [$rsimdefault]  "
set sim=$<
if ($sim == "") then 
    set sim=$rsimdefault
else
    set rsimdefault=$sim
endif
if ($sim != "no" && $sim != "n" && $sim != "yes" && $sim != "y") then
    echo Unrecognized answer:  $sim
    echo "Exiting."
    exit 5
endif
if ($sim == "y" || $sim == "yes") then
    set dflags=("$dflags" -DRSIM_MODULE)
endif

# !usingTcl . . .
endif

echo -n "Include LEF module? [$lefdefault]  "
set lef=$<
if ($lef == "") then 
    set lef=$lefdefault
else
    set lefdefault=$lef
endif
if ($lef != "no" && $lef != "n" && $lef != "yes" && $lef != "y" \
	&& $lef != "m" && $lef != "module") then
    echo Unrecognized answer:  $lef
    echo "Exiting."
    exit 5
endif
if ($?usingTcl) then
    if ($lef == "m" || $lef == "module") then
	set dflags=("$dflags" -DLEF_AUTO)
	set programs=("$programs" lef)
    endif
else
    if ($lef == "m" || $lef == "module") then
	echo Module option only available under Tcl interpreter.
	echo "Exiting."
	exit 5
    endif
endif
if ($lef == "y" || $lef == "yes") then
    set dflags=("$dflags" -DLEF_MODULE)
    set modules=("$modules" lef)
    set extra_libs=("$extra_libs" '${MAGICDIR}/lef/liblef.o')
endif
if ($lef == "n" || $lef == "no") then
    set unused=("$unused" lef)
endif

echo -n "Use new system macros? [$newdotdefault]  "
set newdotmagic=$<
if ($newdotmagic == "") then 
    set newdotmagic=$newdotdefault
else
    set newdotdefault=$newdotmagic
endif
if ($newdotmagic != "no" && $newdotmagic != "n" && $newdotmagic != "yes" && $newdotmagic != "y") then
    echo Unrecognized answer:  $newdotmagic
    echo "Exiting."
    exit 5
endif
if ($newdotmagic == "y" || $newdotmagic == "yes") then
    set dflags=("$dflags" -DUSE_NEW_MACROS)
endif

set cflags=(-g "$cflags")

# generate database.h from database.h.in
${SCRIPTS}/makedbh database/database.h.in database/database.h

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

# Change CADDIR to install in a different place
CADDIR                 = $CADDIR
BINDIR                 = \${CADDIR}/bin
LIBDIR                 = \${CADDIR}/lib
MANDIR                 = \${CADDIR}/man
SYSDIR                 = \${CADDIR}/lib/magic/sys
SCMDIR                 = \${CADDIR}/lib/magic/scm
TCLDIR                 = \${CADDIR}/lib/magic/tcl

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

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

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

M4		       = m4
RM                     = rm -f
CP                     = cp
AR                     = ar
ARFLAGS                = crv
LD                     = ld -r
LINK		       = ld -r
SHDLIB_EXT             = $shdlib_ext
LDDL_FLAGS             = $lddl_flags
RANLIB                 = $ranlib
EXEEXT		       = $exeext

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

READLINE_DEFS          = $rl_defs
READLINE_LIBS          = $rl_libs

DEPEND_FLAG            = $depend_flag
DEPEND_FILE            = Depend

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

ALL_TARGET	       = $all_target
INSTALL_TARGET	       = $install_target

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

cat - << CAT_EOF
Next step: type "make".

After it completes, you'll have a magic binary in the magic subdirectory
CAT_EOF
if (!($?usingTcl) || $threaded == "n" || $threaded == "no") then
cat - << CAT_EOF
and a graphics event capture program in the graphics subdirectory.
CAT_EOF
endif

cat - << CAT_EOF
Final step: type "make install".

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

Log files of the make process are found in make.log and install.log.
CAT_EOF

if (!($?usingTcl) || $threaded == "n" || $threaded == "no") then
cat - << CAT_EOF

When running, magic needs to find the X*Helper 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

unalias rm
if ( -f ${SCRIPTS}/default.conf ) then
    rm -f ${SCRIPTS}/default.conf
endif
cat > ${SCRIPTS}/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 oglrenderdefault="$oglrenderdefault"
set calmadefault="$calmadefault"
set cifdefault="$cifdefault"
set plotdefault="$plotdefault"
set filelockdefault="$filelockdefault"
set readlinedefault="$readlinedefault"
set threadeddefault="$threadeddefault"
set nonmanhdefault="$nonmanhdefault"
set routedefault="$routedefault"
set rsimdefault="$rsimdefault"
set lefdefault="$lefdefault"
set newdotdefault="$newdotdefault"

CAT_EOF
