#!/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)
#       17.12.93 New dependency checks
#	17.03.94 sgi update (R.Huebner)
#	22.03.94 changed permission for Makefiles (M. Vogt)
#	05.04.94 new bin directory structure, dec alpha update (R.Huebner)
#	20.09.94 installation for open windows, patch for linux (R.Huebner)
#	09.01.95 installation for NextStep (G.Mamier)
#	09.01.95 installation for X11R6 (G.Mamier)
#
#                           (c) 1991 - 95 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 4.0         *"
echo "*                                              *"
echo "************************************************"
echo ""
echo " - for use with X11R4,X11R5 or X11R6 and Athena Toolkit"
echo " - needs ansi C-Compiler"
echo " - monochrome and color/grayscale displays"
echo " - on Sun Sparcs, DecStations, RS6000," 
echo "   HP 9000 series, 386/486 PC, Silicon Graphics"
echo "   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 ""

set mfpermission=664

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
if ( -e ./rpc/sources/Makefile) then
	echo -n "rpc     : Copying current Makefile to Makefile.bak ... "
	/bin/cp ./rpc/sources/Makefile ./rpc/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,rpc>/bin"
echo "         /<sun_os4x,ibm_aix,hp_ux,dec_mips,dec_alpha,sgi_irix,"
echo "         sun_solaris,pc_linux,next68k,nexti486,nexthp,other>"
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

# X11 Release
echo ""
echo ""
echo "Select X11 Release"
echo "  [1] X11R4 or X11R5"
echo "  [2] X11R6"
echo -n "Enter number 1-2 > "
set xrelease=$<
switch ($xrelease)
case 1:
      set ldoptions = ( '-L$(XDIR) -lXaw -lXmu -lXt -lXext -lX11 -lm ' )
      breaksw
default:
      set ldoptions = ( '-L$(XDIR) -lXaw -lXmu -lXt -lSM -lICE -lXext -lX11 -lm ' )
      breaksw
endsw



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

set uname_avail = 0
echo ""
echo ""
echo "Select your machine"
echo "  [1] SPARCstation"
echo "  [2] Decstation"
echo "  [3] RS 6000"
echo "  [4] HP 9000"
echo "  [5] Silicon Graphics"
echo "  [6] PC 386/486 Linux"
echo "  [7] NeXTStep Motorola"
echo "  [8] NeXTStep Intel"
echo "  [9] NeXTStep HP PA"
echo "  [0] Other"
echo -n "Enter number 0-9 > "
set machine=$<
switch ($machine)
case 1:
	echo "MACHINE = sun4" >> ./snns.make.tmp
	set ccflags = "-O"
	set fpoptions = ""
	set thismachine = "sun_os4x"
	set machinename = "Sparc Series"
	breaksw
case 2:
	echo "MACHINE =  dec" >> ./snns.make.tmp
	set ccflags = "-O2 -Olimit 3000 -Dconst=''"
	set fpoptions = "-float"
        set machinename = "Digital DecStation"
	set thismachine = "dec_mips"
	breaksw
case 3:
	echo "MACHINE = RS6000" >> ./snns.make.tmp
	set ccflags = "-O"
	set fpoptions = "-float"
	set machinename = "IBM Risc Station 6000"
	set thismachine = "ibm_aix"
	breaksw
case 4:
	echo "MACHINE = HP9000" >> ./snns.make.tmp
	set ccflags = "-O -Aa -D_HPUX_SOURCE"
	set fpoptions = ""
	set machinename = "Hewlett-Packard Series 9000"
	set thismachine = "hp_ux"
	breaksw
case 5:
	echo "MACHINE = sgi" >> ./snns.make.tmp
	set ccflags = "-O"
	set fpoptions = ""
	set machinename = "Silicon Graphics"
	set thismachine = "sgi_irix"
	breaksw
case 6:
	echo "MACHINE = 386" >> ./snns.make.tmp
	set ccflags = "-O -DSYSTEMV"
	set machinename = "PC System V/386"
	set thismachine = "pc_linux"
	breaksw
case 7:
	echo "MACHINE = 68040" >> ./snns.make.tmp
        set mydef = '-Duint="unsigned int"'
        set ccflags = "-O -I $rootdir/kernel/sources/nextincludes $mydef"
        set fpoptions = ""
        set machinename = "NeXTStep NeXTStation"
        set thismachine = "next68k"
	set uname_avail = `which uname | grep "no uname in" | wc -l`
        breaksw
case 8:
        echo "MACHINE = 486" >> ./snns.make.tmp
        set mydef = '-Duint="unsigned int"'
        set ccflags = "-O -I $rootdir/kernel/sources/nextincludes $mydef"
        set fpoptions = ""
        set machinename = "NeXTStep Intel 486"
        set thismachine = "nexti486"
	set uname_avail = `which uname | grep "no uname in" | wc -l`
        breaksw
case 9:
        echo "MACHINE = PA" >> ./snns.make.tmp
        set mydef = '-Duint="unsigned int"'
        set ccflags = "-O -I $rootdir/kernel/sources/nextincludes $mydef"
        set fpoptions = ""
        set machinename = "NeXTStep HP PA" 
        set thismachine = "nexthp"
	set uname_avail = `which uname | grep "no uname in" | wc -l`
        breaksw
default:
	echo "MACHINE = other" >> ./snns.make.tmp
	set ccflags = ""
	set fpoptions = ""
	set machinename = "Unknown"
	set thismachine = "other"
	breaksw
endsw


echo ""
echo ""

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


if ( $thismachine == dec_mips ) then
  echo -n "Do you have an Alpha Workstation ? (y/n) [return] > "
  set is_alpha=$<
  if ( $is_alpha == y ) then
    set ccflags = "-non_shared -Dconst=''"
    set fpoptions = "-float "
    set ldoptions = ( $ldoptions "-ldnet_stub -non_shared" )
    set thismachine = "dec_alpha"
  endif
endif

if ( $uname_avail == 1) then
  set os = `tools/sources/uname -a`
else
  set os = `uname -a`
endif

echo -n "Do you want to use the GNU-C Compiler ? (y/n) [return] > "
set use_gcc=$<
set roptions = ""
if ( $use_gcc == y ) then
  set compiler = gcc
  set ccflags = "-O4"
  if ( $thismachine == dec_mips ) then
      set ccflags = ( $ccflags -fno-omit-frame-pointer -Dultrix)
  endif
  if ( $thismachine == sgi_irix ) then
      set ccflags = ( $ccflags -w)
  endif
  if ( ( $os[1] == SunOS ) && ($os[3]:r == 5 ) ) then
      set thismachine = "sun_solaris"
      set ldoptions = ( $ldoptions -lsocket )
      set roptions = ( '-R$(XDIR) -ldl -lc -lw -lnsl ')
  endif
  set fpoptions = ""
else
  set compiler = cc
  if ( ( $os[1] == SunOS ) && ($os[3]:r == 5 ) ) then
      set ccflags = "-O -w"
      set thismachine = "sun_solaris"
      set ldoptions = ( $ldoptions -lsocket )
  endif
endif
set ldoptions = ( $roptions $ldoptions )
set openwin = ""
if (( $os[1] == SunOS) && ($thismachine != sun_solaris)) then
  echo -n "Do you want to use Open Windows (Please read the note Readme.openwin) ? (y/n) [return] > "
  set openwin=$<
  if ($openwin == y) then
    if ( $use_gcc == y ) then
      set ldoptions = ( "-static" $ldoptions )
    else
      set ldoptions = ( "-Bstatic" $ldoptions )
    endif
  endif
endif

if ( $thismachine != pc_linux ) then
	echo "LEX = lex" >> ./snns.make.tmp
	set lex_lib = "-ll"
else
	echo "LEX = flex" >> ./snns.make.tmp
        set lex_lib = "-lfl" 
endif

set ldoptions = ( $ldoptions $lex_lib )

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

echo "COMPILER = $compiler" >> ./snns.make.tmp
echo "CCFLAGS = $ccflags" >> ./snns.make.tmp
echo "FPOPTIONS = $fpoptions" >> ./snns.make.tmp
echo "LEXLIB = $lex_lib" >> ./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 #############################################

if ( $openwin != y ) then
	if($xrelease == 1) then
		set xlib = /usr/local/X11R5/lib
	else
		set xlib = /usr/local/X11R6/lib
	endif
else
	set xlib = $OPENWINHOME/lib
endif

while ( ! ( -e $xlib/libX11.a || -e $xlib/libX11.so || -e $xlib/libX11.sa ) ) 
	echo "X Libraries not found in $xlib"
       	echo "Please enter full path of the X11 libraries"
	echo "or 'unknown' for manual insertion into Makefiles"
	echo -n "> "
        set xlib=$<
	if ($xlib == "unknown") break
end

echo "X11 libraries located."
echo "XDIR = $xlib" >> ./snns.make.tmp

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

echo ""
if ( $openwin != y ) then
	if($xrelease == 1) then
		set xh = /usr/local/X11R5/include
	else
		set xh = /usr/local/X11R6/include
	endif
else
	set xh = $OPENWINHOME/include
endif
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 "  4)  $rootdir/rpc/sources"
# echo "  3)  $rootdir/compiler/sources"
echo ""
if ( $openwin != y ) then
	if($xrelease == 1) then
		echo "X Windows Version 11 Release 5"	
	else
		echo "X Windows Version 11 Release 6"
	endif
else
   echo "Open Windows"
endif
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 $mfpermission $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 $mfpermission $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 $mfpermission $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 $mfpermission $COMPILERROOTDIR/Makefile

# endif

# tools
        set TOOLSROOTDIR = $rootdir/tools/sources
        /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 $mfpermission $TOOLSROOTDIR/Makefile

# RPC
        set RPCROOTDIR = $rootdir/rpc/sources
        /bin/cp ./snns.make.tmp $rootdir/rpc/sources/Makefile
        cat $rootdir/configuration/rpcstuff >> $RPCROOTDIR/Makefile
	if (! -d $rootdir/rpc/bin/$thismachine) then
	  mkdir $rootdir/rpc/bin/$thismachine
	endif
	if ( $InstallDir != "") then
	  set bindir = $InstallDir
	  if (! -d $bindir)  then
	    mkdir $bindir
	  endif
	  echo ""
	else
    	  set bindir = $rootdir/rpc/bin/$thismachine
	endif
	echo "s&____&$bindir&g" > $rootdir/configuration/Temp
	sed -f $rootdir/configuration/Temp $RPCROOTDIR/Makefile > $RPCROOTDIR/m.new
	/bin/mv  $RPCROOTDIR/m.new $RPCROOTDIR/Makefile
	/bin/rm $rootdir/configuration/Temp
	chmod $mfpermission $RPCROOTDIR/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 rpc       - to build the rpc 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/$thismachine"
	echo ""
	echo "All examples will be found in:"
	echo "$rootdir/examples"
	echo ""
	echo "Remark: You can use ./cleanup with the same parameters"
	echo "        used with the ./build program."
	echo ""

	breaksw
endsw

