#!/bin/csh

# ****************************************************************************
#  FILE     : configure
#  SHORTNAME: config

#  PURPOSE  : Shell scipt to create three make files
#	      1) kernel in the directory kernel/source
#	      2) X user interface in xgui/source
#	      3) tools in tools/source

#  AUTHOR   : Tilman Sommer, Niels Mache, Ralf Huebner
#  DATE     : 17.02.92

#  MODIFICATIONS : 
#	01.02.91 lines 222-225, 241-256 inserted. (Thomas Korb)
#	22.05.91 some cosmetic changes (A. Zell)
#       25.06.91 line 165 -DSYSTEMV (-> compiler, setbuffer())
#       28.11.91 changes for V1.4: new Makefiles, sun4 dir. renamed to sparc
#                (N. Mache)
#       17.12.91 changes for X11R4: new directory paths (R. Huebner)
#	17.02.92 build all, build compiler commented out (A. Zell)
#	25.02.92 changes for X11R5, installation of RS6000 (R. Huebner)
#	03.03.92 query, if X11 Files not found (R. Huebner)
#	23.03.93 update for version 3.0 (R. Huebner)
#       04.05.93 1) check for $xh/X11/Xaw/Label.h, not only for directory 
#                2) allow 'unknown' for lib and header paths
#                (H. Bayer)
#       13.08.93 Tools options added (R. Huebner)
#       24.08.93 Changes for Solaris (R. Huebner)
#
#                           (c) 1991, 92, 93 by SNNS-Group
#             Institute for Parallel and Distributed High Performance Systems
#                             University of Stuttgart, FRG
# ****************************************************************************



echo "************************************************"
echo "*                                              *"
echo "*  SNNS  - Stuttgart Neural Network Simulator  *"
echo "*                                              *"
echo "*          Installation of Version 3.1         *"
echo "*                                              *"
echo "************************************************"
echo ""
echo " - for use with X11R4 or X11R5 and Athena Toolkit"
echo " - needs ansi C-Compiler"
echo " - monochrome and color/grayscale displays"
echo " - on Sun 3, Sun 4 and SPARCstations, DecStations, RS6000," 
echo "   HP 9000 series, 386 PC and (hopefully) other machines"
echo " - It is possible to install SNNS for different types of"
echo "   machines in the same directory."
echo ""
echo -n "Do you really want to create new makefiles? (y/n) [return] > "
set confirm=$<
switch ($confirm)
case y:
	breaksw
default:
	echo "EXIT: snns.make not changed."
	exit 0
endsw
echo ""
echo ""

if ( -e ./kernel/sources/Makefile) then
	echo -n "kernel  : Copying current Makefile to Makefile.bak ... "
	/bin/cp ./kernel/sources/Makefile ./kernel/sources/Makefile.bak
	echo "Done."
endif
if ( -e ./xgui/sources/Makefile) then
	echo -n "xgui    : Copying current Makefile to Makefile.bak ... "
	/bin/cp ./xgui/sources/Makefile ./xgui/sources/Makefile.bak
	echo "Done."
endif
if ( -e ./compiler/sources/Makefile) then
	echo -n "compiler: Copying current Makefile to Makefile.bak ... "
	/bin/cp ./compiler/sources/Makefile ./compiler/sources/Makefile.bak
	echo "Done."
endif
if ( -e ./tools/sources/Makefile) then
	echo -n "tools   : Copying current Makefile to Makefile.bak ... "
	/bin/cp ./tools/sources/Makefile ./tools/sources/Makefile.bak
	echo "Done."
endif


cat ./configuration/header >! ./snns.make.tmp
echo "# Created by `logname` at `date`" >> ./snns.make.tmp
echo "" >> ./snns.make.tmp
echo "# -- SNNS  Installation  Directories  &  Libraries --" >> ./snns.make.tmp
echo "" 
echo "For installation, we need the full path to your current directory"
echo "but without auto-mount prefix (like /tmp_mnt/...)"
echo "Current directory:"
echo "        " $cwd
echo -n "Is this your current directory without auto-mount prefix? (y/n) [return] > "
set confirm=$<
switch ($confirm)
case n:
	echo ""
	echo "Please enter the FULL path of your current directory"
	echo "but without auto-mount prefix."
	echo -n "> "
	set rootdir=$<
	breaksw
default:
	set rootdir = `echo $cwd`
	breaksw
endsw

echo "SNNSDIR = $rootdir" >> ./snns.make.tmp


echo ""
echo "Installation directory:"
echo -n "        " $rootdir
echo "/<kernel,xgui,tools>/bin/<dec,sun3,sun4,rs6000,hp9000>"
echo -n "Do you want to change the installation directory ? (y/n) [return] > "
set confirm=$<
switch ($confirm)
case y:
	echo ""
	echo "Please enter the FULL path of your new installation directory"
	echo "but without auto-mount prefix."
	echo -n "> "
	set InstallDir=$<
	breaksw
default:
	set InstallDir
	breaksw
endsw


# machine #############################################

echo ""
echo ""
echo "Select your machine"
echo "  [1] Sun 4 Series and SPARCstations"
echo "  [2] Sun 3 "
echo "  [3] Decstation"
echo "  [4] RS 6000"
echo "  [5] HP 9000"
echo "  [6] PC 386 Unix (Linux)"
echo "  [7] Other"
echo -n "Enter number 1-7 > "
set machine=$<
set ldoptions = ( '-L$(XDIR) -lXaw -lXmu -lXt -lXext -lX11 -lm' )
switch ($machine)
case 1:
	echo "MACHINE = sun4" >> ./snns.make.tmp
	set ccflags = "-O"
	set fpoptions = ""
	set thismachine = "sparc"
	set machinename = "Sparc Series"
	breaksw
case 2:
	echo "MACHINE = sun3" >> ./snns.make.tmp
	set ccflags = "-O"
	set machinename = "Sun 3 Series"
	set thismachine = "sun3"
	echo ""
	echo "Does your Sun 3 have a 68881 fpu ?"
	echo -n "(y/n) [return] > "
	set fpu=$<
	switch ($fpu)
	case n:
		set fpoptions = "-fsingle"
		breaksw
	default:
		set fpoptions = "-fsingle -f68881"
		breaksw
	endsw
	breaksw
case 3:
	echo "MACHINE =  dec" >> ./snns.make.tmp
	set ccflags = "-O2 -Olimit 3000 -Dconst=''"
	set fpoptions = "-float"
        set machinename = "Digital DecStation"
	set thismachine = "dec"
	breaksw
case 4:
	echo "MACHINE = RS6000" >> ./snns.make.tmp
	set ccflags = "-O"
	set fpoptions = "-float"
	set machinename = "IBM Risc Station 6000"
	set thismachine = "RS6000"
	breaksw
case 5:
	echo "MACHINE = HP9000" >> ./snns.make.tmp
	set ccflags = "-O +Obb3000 -Aa -D_HPUX_SOURCE"
	set fpoptions = ""
	set machinename = "Hewlett-Packard Series 9000"
	set thismachine = "HP9000"
	breaksw
case 6:
	echo "MACHINE = 386" >> ./snns.make.tmp
	echo "ccflags = -O -DSYSTEMV" >> ./snns.make.tmp
	set machinename = "PC System V/386"
	set thismachine = "pc386"
	breaksw
default:
	echo "MACHINE = other" >> ./snns.make.tmp
	set ccflags = ""
	set fpoptions = ""
	set machinename = "Unknown"
	set thismachine = "other"
	breaksw
endsw


echo "MACHINENAME= $thismachine" >> ./snns.make.tmp


echo ""
echo ""

# compiler ##########################################

set os = `uname -a`
echo -n "Do you want to use the GNU-C Compiler ? (y/n) [return] > "
set use_gcc=$<
if ( $use_gcc == y ) then
  set compiler = gcc
  set ccflags = "-O4 -ansi"
  if ( $thismachine == dec ) then
      set ccflags = ( $ccflags -fno-omit-frame-pointer -Dultrix)
  endif
  set fpoptions = ""
else
  set compiler = cc
  if ( ( $os[1] == SunOS ) && ($os[3]:r == 5 ) ) then
      set ccflags = "-O -w"
  endif 
endif

echo "COMPILER = $compiler" >> ./snns.make.tmp
echo "CCFLAGS = $ccflags" >> ./snns.make.tmp
echo "FPOPTIONS = $fpoptions" >> ./snns.make.tmp

echo ""

# ranlib #############################################

if ( -e /usr/bin/ranlib ) then
	echo "RLIB = ranlib" >> ./snns.make.tmp
else
	echo "RLIB = echo" >> ./snns.make.tmp
endif

echo ""
echo ""

# libraries #############################################

set xlib = /usr/local/X11R5/lib
while (! -e $xlib/libX11.a )
	echo "X Libraries not found in $xlib"
        echo "Please enter full path of the X11 libraries"
	echo -n "> "
        set xlib=$<
	if ($xlib == "unknown") break
end
echo "X11 libraries located."
echo "XDIR = $xlib" >> ./snns.make.tmp

# includes #############################################

echo ""
set xh = /usr/local/X11R5/include
while (! -e $xh/X11/Xaw/Label.h )
	echo "X11 include directory is not $xh"
        echo "Please enter full path of the X11 include files"
	echo -n "> "
        set xh=$<
	if ($xh == "unknown") break
end
echo "X11 include directory located."
echo "XH = $xh" >> ./snns.make.tmp



echo ""
echo ""
echo "************************************************"
echo "*                                              *"
echo "*  SNNS  - Stuttgart Neural Network Simulator  *"
echo "*                                              *"
echo "*             selected configuration           *"
echo "*                                              *"
echo "************************************************"
echo ""
echo "Machine: " $machinename
echo ""
echo "Compiler: " $compiler
echo ""
echo "SNNS sources in " 
echo "  1)  $rootdir/kernel/sources"
echo "  2)  $rootdir/xgui/sources"
echo "  3)  $rootdir/tools/sources"
# echo "  3)  $rootdir/compiler/sources"
echo ""
echo "X Windows Version 11 Release 5 "
echo "  libraries in                 " $xlib
echo "  header file directory in     " $xh
echo ""
echo -n "Is this correct ? (y/n) [return] > "
set confirm=$<
switch ($confirm)
case n:
	echo "EXIT: snns.make unchanged."
	/bin/rm -f ./snns.make.tmp
	exit 0
default:
	echo ""
	echo -n "Building makefiles ..."

# if ( 0 ) then

# Kernel
        set KERNELROOTDIR = $rootdir/kernel/sources
        /bin/cp ./snns.make.tmp $KERNELROOTDIR/Makefile
        cat $rootdir/configuration/kernelstuff >> $KERNELROOTDIR/Makefile
#	cat $rootdir/configuration/depend.kernel >> $KERNELROOTDIR/Makefile
	chmod 744 $KERNELROOTDIR/Makefile
	if (! -d $rootdir/kernel/bin/$thismachine) then
	  mkdir $rootdir/kernel/bin/$thismachine
	endif
	if ( $InstallDir != "" ) then
	  set bindir = $InstallDir
	  if (! -d $bindir)  then
	    mkdir $bindir
	  endif
	else
    	  set bindir = $rootdir/kernel/bin/$thismachine
	endif
	echo "s&____&$bindir&g" > $rootdir/configuration/Temp
	sed -f $rootdir/configuration/Temp $KERNELROOTDIR/Makefile > $KERNELROOTDIR/m.new
	/bin/mv  $KERNELROOTDIR/m.new $KERNELROOTDIR/Makefile
	/bin/rm $rootdir/configuration/Temp
	chmod 744 $KERNELROOTDIR/Makefile



# XGUI
        set XGUIROOTDIR = $rootdir/xgui/sources
        echo "LDOPTIONS = $ldoptions" >> ./snns.make.tmp
        /bin/cp ./snns.make.tmp $rootdir/xgui/sources/Makefile
        cat $rootdir/configuration/xguistuff >> $XGUIROOTDIR/Makefile
#	cat $rootdir/configuration/depend.xgui >> $XGUIROOTDIR/Makefile
	if (! -d $rootdir/xgui/bin/$thismachine) then
	  mkdir $rootdir/xgui/bin/$thismachine
	endif
	if ( $InstallDir != "") then
	  set bindir = $InstallDir
	  if (! -d $bindir)  then
	    mkdir $bindir
	  endif
	  echo ""
	  echo -n "Copying xgui files: help.hdoc, default.cfg ..."
	  /bin/cp $rootdir/xgui/bin/help.hdoc   $InstallDir
	  /bin/cp $rootdir/xgui/bin/default.cfg $InstallDir
	  echo "Done."
	  echo ""
	else
    	  set bindir = $rootdir/xgui/bin/$thismachine
	endif
	echo "s&____&$bindir&g" > $rootdir/configuration/Temp
	sed -f $rootdir/configuration/Temp $XGUIROOTDIR/Makefile > $XGUIROOTDIR/m.new
	/bin/mv  $XGUIROOTDIR/m.new $XGUIROOTDIR/Makefile
	/bin/rm $rootdir/configuration/Temp
	chmod 744 $XGUIROOTDIR/Makefile

# Compiler
#	set COMPILERROOTDIR = $rootdir/compiler/sources
#        /bin/cp ./snns.make.tmp $rootdir/compiler/sources/Makefile
#       cat $rootdir/configuration/compilerstuff >> $COMPILERROOTDIR/Makefile
#	cat $rootdir/configuration/$thismachine.compiler >> $COMPILERROOTDIR/Makefile
#	cat $rootdir/configuration/depend.compiler >> $COMPILERROOTDIR/Makefile
#	if (! -d $rootdir/compiler/bin/$thismachine) then
#	  mkdir $rootdir/compiler/bin/$thismachine
#	endif
#	if ( $InstallDir != "") then
#	  set bindir = $InstallDir
#	  if (! -d $bindir)  then
#	    mkdir $bindir
#	  endif
#	else
#    	  set bindir = $rootdir/compiler/bin/$thismachine
#	endif
#	echo "s&____&$bindir&g" > $rootdir/configuration/Temp
#	sed -f $rootdir/configuration/Temp $COMPILERROOTDIR/Makefile > $COMPILERROOTDIR/m.new
#	/bin/mv  $COMPILERROOTDIR/m.new $COMPILERROOTDIR/Makefile
#	/bin/rm $rootdir/configuration/Temp
#	chmod 744 $COMPILERROOTDIR/Makefile

# endif

# tools
        set TOOLSROOTDIR = $rootdir/tools/sources
        echo "LDOPTIONS = $ldoptions" >> ./snns.make.tmp
        /bin/cp ./snns.make.tmp $rootdir/tools/sources/Makefile
        cat $rootdir/configuration/toolstuff >> $TOOLSROOTDIR/Makefile
#	cat $rootdir/configuration/depend.tools >> $TOOLSROOTDIR/Makefile
	if (! -d $rootdir/tools/bin/$thismachine) then
	  mkdir $rootdir/tools/bin/$thismachine
	endif
	if ( $InstallDir != "") then
	  set bindir = $InstallDir
	  if (! -d $bindir)  then
	    mkdir $bindir
	  endif
	  echo ""
	  echo "Done."
	  echo ""
	else
    	  set bindir = $rootdir/tools/bin/$thismachine
	endif
	echo "s&____&$bindir&g" > $rootdir/configuration/Temp
	sed -f $rootdir/configuration/Temp $TOOLSROOTDIR/Makefile > $TOOLSROOTDIR/m.new
	/bin/mv  $TOOLSROOTDIR/m.new $TOOLSROOTDIR/Makefile
	/bin/rm $rootdir/configuration/Temp
	chmod 744 $TOOLSROOTDIR/Makefile


# END
	/bin/rm -f ./snns.make.tmp 
	echo "Done."


	echo ""
	echo ""
	echo "************************************************"
	echo "*                                              *"
	echo "*  SNNS  - Stuttgart Neural Network Simulator  *"
	echo "*                                              *"
	echo "************************************************"
	echo ""
	echo "If you start to build SNNS the first time for a new type"
	echo "of machine then first delete all object files with the"
	echo "command:"
	echo "                ./cleanup all"
	echo ""
	echo "Then type:"
	echo ""
	echo "./build all       - to build all SNNS modules"
	echo "./build sim       - to build kernel and xgui"
	echo "./build kernel    - to build kernel only"
	echo "./build xgui      - to build xgui only"
        echo "./build tools     - to build the tools only"
#	echo "./build compiler  - to build the Nessus compiler only"
	echo ""
	if ( $InstallDir != "" ) then
  	  echo "SNNS will be placed in:"
	  echo "$InstallDir"
	  echo ""
	  echo "All libraries will be placed in:"
	else
	  echo "All the relevant binaries will be placed in:"
	endif
	echo "$rootdir/(SNNS module)/bin"
	echo ""
	echo "All examples will be found in:"
	echo "$rootdir/(SNNS module)/examples"
	echo ""
	echo "Remark: You can use ./cleanup with the same parameters"
	echo "        used with the ./build program."
	echo ""

	breaksw
endsw

