#!/bin/csh -f
# rcsid="$Header: /ufs/repository/magic/scripts/config,v 1.2 2002/06/18 11:00:00 tim Exp $"

set VERSION="7.2"

echo ""
echo "              Welcome to Magic's configuration program."
echo "                    This is Version $VERSION."
echo ""

set SCRIPTS=scripts
set modules=""
set unused=""
set cadinstall=""
set extra_libs=""
set mac_extra_libs=""
set mac_extra_objs=""
set programs=""

# default options (may be overridden in file "default.conf")
set interpdefault="1"
set windefault="1"
set osdefault="1"
set machdefault="1"
set calmadefault="yes"
set oglrenderdefault="yes"
set cifdefault="yes"
set plotdefault="module"
set filelockdefault="no"
set readlinedefault="yes"
set threadeddefault="yes"
set nonmanhdefault="yes"
set plowdefault="module"
set routedefault="module"
set rsimdefault="yes"
set newdotdefault="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
  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=
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=($<)
if ("$interp" == "") then
   set interp="$interpdefault"
else
   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_THREADS -DTCL_DIR=\\\"'${TCLDIR}'\\\")
    set unused=("$unused" lisp)
    breaksw
endsw
end

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

cat - << CAT_EOF

-------------------------------------------------------------------------------
Magic works with the following window configurations.  X11 is preferred.
	1) X11
	2) OpenGL
CAT_EOF
if (!($?usingTcl)) then
cat - << CAT_EOF
	3) SunView
	4) OS/2 Warp
CAT_EOF
endif

echo ""
echo -n "Please select one or more of the above configurations by number [$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 gr_dflags=("$gr_dflags" -DOGL)
	set gr_libs=("$gr_libs" -lGLU -lGL -lXi -lXmu -lXext)
	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)
	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
case "3":
    echo "SunView selected"
    set gr_dflags=("$gr_dflags" -DSUNVIEW)
    set gr_libs=("$gr_libs" -lsuntool -lsunwindow -lpixrect)
    set gr_srcs=("$gr_srcs" '${SUN_SRCS}')
    set gr_hsrcs=("$gr_hsrcs" '${SUNHELPER_SRCS}')
    set gr_hprog=("$gr_hprog" '${SUNHELP_PROG}')
    breaksw
case "4":
    echo "OS/2 Warp (X11-based) 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" '${OS2_SRCS}')
    set gr_hsrcs=("$gr_hsrcs" '${X11HELPER_SRCS}')
    set gr_hprog=("$gr_hprog" '${X11HELP_PROG}')
    breaksw
default:
    echo "Unrecognized selection:  $i"
    echo "Exiting."
    exit 1
    breaksw
endsw
end

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

cat - << CAT_EOF

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

	 1) Linux
	 2) NetBSD 1.x 
	 3) FreeBSD 2.x 
	 4) OSF/1 on a 64 bit machine (Digital Alpha AXP)
	 5) Solaris 2.x (aka SunOS 5.x)
	 6) SunOS 4.x 
	 7) SGI IRIX
	 8) OS/2 Warp
	 9) BSD Unix systems (Ultrix, pre-4.0 SunOS, RISCos4.0, etc..)
 	10) SYSV Unix systems (A/UX, HPUX)
	11) Cygwin (UNIX for MS Windows)
        12) Mac OS X (Darwin )
CAT_EOF

echo ""
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

if ($os == "12") then
 set shdlib_ext = ".dylib"
 set lddl_flags = "-dynamiclib"
 set mac_extra_libs =("$mac_extra_libs" -L/usr/local/lib -ltcl8.4 -ltk8.4)
 set mac_extra_objs =("$mac_extra_objs" '${MAGICDIR}/magic/tclmagic${SHDLIB_EXT}')
else
 set shdlib_ext = ".so"
 set lddl_flags = "-shared"
endif

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

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 oflags=("-O6 -fomit-frame-pointer")
    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 "2":
    echo "NetBSD 1.x selected"
    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 "3":
    echo "FreeBSD 2.x selected"
    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 "4":
    echo "64 bit OSF/1 selected (Alpha AXP)"
    set dflags=("$dflags" -DALPHA -DUSE_IO_PROBE)
    breaksw
case "5":
    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)
    set oflags=(-fast)
    set ranlib=/bin/true
    breaksw
case "6":
    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 cflags=("$cflags" -Bstatic)
    breaksw
case "7":
    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 "8":
    echo "OS/2 Warp selected"
    set dflags=("$dflags" -DOS2)
    breaksw
case "9":
    echo 'BSD Unix selected'
    breaksw
case "10":
    echo "SYSV selected"
    set dflags=("$dflags" -DSYSV )
    set ranlib=/bin/true
    breaksw
case "11":
    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)
    set usingCygwin
    set cc=gcc
    set cpp = "/bin/cpp -P -traditional"
    breaksw
case "12":
    echo "Mac OS X selected"
    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 -O2 -fno-common)
	set gr_cflags=("$gr_cflags" -I/usr/X11R6/include)
 	set gr_libs=(-L/usr/X11R6/lib "$gr_libs")
    endif
    breaksw
default:
    echo "Unrecognized selection:  $os"
    echo "Exiting."
    exit 2
    breaksw
endsw

cat - << CAT_EOF

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

	1) Intel 80x86-based workstations
	2) HP 68000-based workstations
	3) HP/PA based workstation
 	4) A MIPS workstation running RISCos4.0 or greater (not DECStations)
 	5) An Apple MacII
        6) An Apple PowerPC based Mac
	7) 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
case "3":
    echo "HP/PA workstation selected"
    breaksw
case "4":
    echo "MipsCo selected"
    set cflags=("$cflags" -systype bsd43 -O -Olimit 3000 -signed)
    breaksw
case "5":
    echo "MacII selected"
    set dflags=("$dflags" -DmacII)
    set cflags=("$cflags" -B /usr/lib/big/)
    breaksw
case "6":
    echo "PowerPC based Mac selected"
    set dflags=("$dflags" -DmacII)
    breaksw
case "7":
    echo "Normal compilation---no additional flags."
    breaksw
default:
    echo "Unrecognized selection:  $mach"
    echo "Exiting."
    exit 2
    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
    PLOW - adds plow and straighten commands
    ROUTE - a whole bunch of routing tools
    RSIM - an interface to (I)RSIM
    DRC EXTENSIONS - maxwidth, area, and rectangle rules
      DRC CIF EXTENSIONS - DRC rules applied to generated CIF layers
    .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.  If so, the default option will be "module".  The feature
will be compiled as a separate library which will automatically be
loaded when the feature is first invoked.  Note that auto-loading
may force the technology file to be reloaded, incurring a delay of
about a second.  If this behavior annoys you, choose "yes" and the
feature will be precompiled and available at startup.

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 ($plowdefault == "m" || $plowdefault == "module") then
	set plowdefault="yes"
    endif
    if ($plotdefault == "m" || $plotdefault == "module") then
	set plotdefault="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 PLOW module? [$plowdefault]  "
set plow=$<
if ($plow == "") then 
    set plow=$plowdefault
else
    set plowdefault=$plow
endif
if ($plow != "no" && $plow != "n" && $plow != "yes" && $plow != "y" \
	&& $plow != "m" && $plow != "module") then
    echo Unrecognized answer:  $plow
    echo "Exiting."
    exit 5
endif
if ($?usingTcl) then
    if ($plow == "m" || $plow == "module") then
	set dflags=("$dflags" -DPLOW_AUTO)
	set programs=("$programs" plow)
    endif
else
    if ($plow == "m" || $plow == "module") then
	echo Module option only available under Tcl interpreter.
	echo "Exiting."
	exit 5
    endif
endif
if ($plow == "y" || $plow == "yes") then
    set dflags=("$dflags" -DPLOW_MODULE)
    set modules=("$modules" plow)
    set extra_libs=("$extra_libs" '${MAGICDIR}/plow/libplow.o')
endif
if ($plow == "n" || $plow == "no") then
   set unused=("$unused" plow)
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 "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 ldflags=("$ldflags" -lm)
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
    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
SYSDIR                 = \${CADDIR}/lib/magic/sys
SCMDIR                 = \${CADDIR}/lib/magic/scm
TCLDIR                 = \${CADDIR}/lib/magic/tcl

SCRIPTS                = \${MAGICDIR}/scripts
MAIN_EXTRA_LIBS	       = $extra_libs
MAC_EXTRA_LIBS         = $mac_extra_libs
MAC_SHARED	       = $mac_extra_objs

MODULES               += $modules
UNUSED_MODULES        += $unused
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

READLINE_DEFS          = $rl_defs
READLINE_LIBS          = $rl_libs

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

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

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".

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

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 oglrenderdefault="$oglrenderdefault"
set calmadefault="$calmadefault"
set cifdefault="$cifdefault"
set plotdefault="$plotdefault"
set filelockdefault="$filelockdefault"
set readlinedefault="$readlinedefault"
set threadeddefault="$threadeddefault"
set nonmanhdefault="$nonmanhdefault"
set plowdefault="$plowdefault"
set routedefault="$routedefault"
set rsimdefault="$rsimdefault"
set newdotdefault="$newdotdefault"

CAT_EOF
