#!/bin/csh -f


if (! $?KHOROS_HOME) then
   echo ""
   echo "installit: Error, The environment variable KHOROS_HOME has not"
   echo "           been set. Please set the KHOROS_HOME environment variable"
   echo "           to the top level directory of Khoros, and re-execute"
   echo "           the installit program"
   echo " 	    For more information, please consult the installation"
   echo "           manual file KHOROS_HOME/release/install"
   exit 0
endif

set path = ( . $KHOROS_HOME/bin $path /usr/local/bin /usr/lang /usr/bsd \
		/usr/ucb /usr/bin /bin /usr/etc /etc /usr/bin/X11 )

set TEMPDIR = /tmp

echo ""
echo "                  Khoros 1.0 (Patch 5) Installation"
echo "   This script prompts you for the binary, library  and"
echo "   include  directories, and the BOOTSTRAP flags"
echo "   necessary to compile Khoros."
echo ""
echo "   Note, the symbols BinDir, KLibDir and IncRoot will"
echo "   be changed in the KHOROS_HOME/repos/config/imake_conf/Site.def"
echo "   file based on your response to the prompts."
echo ""
echo "   The compile will take approximately 3 to 5 hours."
echo "   The compile status for Khoros will be logged to the file"
echo "   KHOROS_HOME/src/make.World. You can use the command"
echo "    % tail -f make.World  to watch the compile."
echo ""
echo "    ################################################################"
echo "    # Did you remember to check the Imake configuration            #"
echo "    # parameters in the directory:                                 #" 
echo "    # KHOROS_HOME/repos/config/imake_conf                          #"
echo "    #                                                              #"
echo "    # For information on configuring the Khoros Imake parameters,  #"
echo "    # consult the file 'chapter9' in the directory:                #"
echo "    # KHOROS_HOME/release                                          #"
echo "    ################################################################"
echo ""
echo -n "Do you wish to continue the installation (y/n) [y]: "
set resp = $<
if ($resp == "n" || $resp == "N") then
   echo ""
   echo ""
   echo "Exiting the Khoros installation script...."
   echo ""
   exit 0
endif

set SITE_FILE = $KHOROS_HOME/repos/config/imake_conf/Site.def

if  (! -e Makefile.org) then
    \mv Makefile Makefile.org
endif
#
# Get and Set the directory for the Khoros binaries
#

set bdir = `awk '{ if( $1 == "#define" ) { if ($2 == "BinDir") { print $3 } } }' $SITE_FILE`
echo ""
echo ""
echo ""
echo "Enter the directory for Khoros binaries or return for the default:"
echo ""
if ($bdir == "KHOROS_HOME/bin") then
   echo -n  "Default [$KHOROS_HOME/bin]: "
else
   echo -n "Default [$bdir]: "
endif
set bindir = $<

if (($bindir == "" && $bdir == "KHOROS_HOME/bin") || ($bindir == "KHOROS_HOME/bin")) then
  set bindir = $KHOROS_HOME/bin
  set bdir = KHOROS_HOME/bin
else 
  if ($bindir == "" && $bdir != "KHOROS_HOME/bin") then
    set bindir = $bdir
  else
    set bdir = $bindir
  endif
endif

echo ""
echo "$SITE_FILE"
echo "Changing the '#define BinDir to #define BinDir $bdir'"
echo ""
set cmd = '{ if( $1 == "#define" ) { if ($2 == "BinDir") { if ($3 != "") printf("%s %s '$bdir'\n", $1, $2) } else print $0 } else { print $0 } } '

awk " $cmd " $SITE_FILE > $SITE_FILE.new
\mv $SITE_FILE.new $SITE_FILE

touch $TEMPDIR/bindir.$$
echo $bindir >>  $TEMPDIR/bindir.$$
set new = `sed -e "s/\//\\\//g" $TEMPDIR/bindir.$$`

touch $TEMPDIR/binsed.$$
touch Makefile.bin
echo "/ BINDIR/s/BINDIR = .*/BINDIR = $new/" >> $TEMPDIR/binsed.$$
sed -f $TEMPDIR/binsed.$$ Makefile.org >> Makefile.bin

\rm -f $TEMPDIR/bindir.$$ $TEMPDIR/binsed.$$

if (! -d $bindir) then
  mkdir $bindir
endif

#
# Get and Set the directory for the Khoros libraries
#
set ldir = `awk '{ if( $1 == "#define" ) { if ($2 == "KLibDir") { print $3 } } }' $SITE_FILE`
echo ""
echo ""
echo ""
echo "Enter the directory for Khoros libraries or return for the default:"
echo ""
if ($ldir == "KHOROS_HOME/lib") then
   echo -n  "Default [$KHOROS_HOME/lib]: "
else
   echo -n  "Default [$ldir]: "
endif
set libdir = $<

if (($libdir == "" && $ldir == "KHOROS_HOME/lib") || ($libdir == "KHOROS_HOME/lib")) then
  set libdir = $KHOROS_HOME/lib
  set ldir = KHOROS_HOME/lib
else
  if ($libdir == "" && $ldir != "KHOROS_HOME/lib") then
    set libdir = $ldir
  else
    set ldir = $libdir
  endif
endif

echo ""
echo "$SITE_FILE"
echo "Changing the '#define KLibDir to #define KLibDir $ldir'"
echo ""
set cmd = '{ if( $1 == "#define" ) { if ($2 == "KLibDir") { if ($3 != "") printf("%s %s '$ldir'\n", $1, $2) } else print $0 } else { print $0 } } '

awk " $cmd " $SITE_FILE > $SITE_FILE.new
\mv $SITE_FILE.new $SITE_FILE

touch $TEMPDIR/libdir.$$
echo $libdir >>  $TEMPDIR/libdir.$$
set new = `sed -e "s/\//\\\//g" $TEMPDIR/libdir.$$`

touch $TEMPDIR/libsed.$$
touch Makefile.lib
echo "/ LIBDIR/s/LIBDIR = .*/LIBDIR = $new/" >> $TEMPDIR/libsed.$$
sed -f $TEMPDIR/libsed.$$ Makefile.bin >> Makefile.lib

\rm -f $TEMPDIR/libdir.$$ $TEMPDIR/libsed.$$

if (! -d $libdir) then
  mkdir $libdir
endif

#
# Get and Set the directory for the Khoros includes
#

againinc:
set idir = `awk '{ if( $1 == "#define" ) { if ($2 == "IncRoot") { print $3 } } }' $SITE_FILE` 
echo ""
echo ""
echo ""
echo "Enter the directory for Khoros include files or return for the default:"
echo ""
if ($idir == "KHOROS_HOME/include") then
   echo -n  "Default [$KHOROS_HOME/include]: "
else
   echo -n  "Default [$idir]: "
endif
set incdir = $<

if (($incdir == "" && $idir == "KHOROS_HOME/include") || ($incdir == "KHOROS_HOME/include")) then
  set incdir = $KHOROS_HOME/include
  set idir = KHOROS_HOME/include
else
  if ($incdir == "" && $idir != "KHOROS_HOME/include") then
    set incdir = $idir
  else
    set idir = $incdir
  endif
endif

echo ""
echo "$SITE_FILE"
echo "Changing the '#define IncRoot to #define IncRoot $idir'"
echo ""
set cmd = '{ if( $1 == "#define" ) { if ($2 == "IncRoot") { if ($3 != "") printf("%s %s '$idir'\n", $1, $2) } else print $0 } else { print $0 } } '

awk " $cmd " $SITE_FILE > $SITE_FILE.new
\mv $SITE_FILE.new $SITE_FILE

touch $TEMPDIR/incdir.$$
echo $incdir >>  $TEMPDIR/incdir.$$
set new = `sed -e "s/\//\\\//g" $TEMPDIR/incdir.$$`

if ( -e Makefile.inc) then
  \rm -f Makefile.inc
endif

touch $TEMPDIR/incsed.$$
touch Makefile.inc
echo "/ INCROOT/s/INCROOT = .*/INCROOT = $new/" >> $TEMPDIR/incsed.$$
sed -f $TEMPDIR/incsed.$$ Makefile.lib >> Makefile.inc

\rm -f $TEMPDIR/incdir.$$ $TEMPDIR/incsed.$$

if (! -d $incdir) then
  echo ""
  echo "Cannot find the Khoros include directory"
  echo "Please re-enter the directory name"
  echo ""
  goto againinc
endif


#
# Get and Set the bootstrap flags
#
echo ""
echo ""
echo ""
echo ""
echo "The following is a list of known bootstrap flags necessary for compiling"
echo "Khoros on different architectures."
echo ""
echo "Please Note: If you specify Bootstrapflags that are DIFFERENT than the"
echo "ones that are shown below, you must modify the appropriate {machine}.cf"
echo "file in the KHOROS_HOME/repos/config/imake_conf directory.  Add the same"
echo "Bootstrapflags to the #define BootstrapCFlags symbol that you will enter"
echo "below.  If you DON'T edit the appropriate {machine}.cf file, your"
echo "installation may fail\!"
echo ""
echo "For Mips Machine Use:     	 -DMips"
echo "For IBM 6000 Machine Use:          -D_IBMR2"
echo "For 386/486 Machine Use:      	 -DSYSV -DSYSV386 -DUSG"
echo "For Data General (DGUX) Use:     	 -DSYSV"
echo "For Ultrix Machine Use:    	 (none)"
echo "For Apollo Machine Use:    	 -DX_NOT_POSIX"
echo "For Next Machine Use:      	 -DUSG -D_POSIX_SOURCE"
echo "For luna 88k Machine Use:      	 -DUSG -D_POSIX_SOURCE"
echo "For SGI Machine Use:       	 -DUSG -D_POSIX_SOURCE"
echo "For Crays running UNICOS 5.*:	 -DCPP"
echo "For Crays running UNICOS 6.*:	 (none)"
echo "For Convexes running ConvexOS 8.1: -pcc"
echo "For Convexes running ConvexOS 9.1: (none)"
echo "For Sun Machine Use:     -Bstatic {compiling w/ HasSharedLibraries = NO}"
echo "                       or  (none) (compiling w/ HasSharedLibraries = YES}"
echo ""
echo "Enter 'q' to QUIT the installation."
echo -n  "Default []: "
set bot = $<

if ("$bot" == 'q' || "$bot" == 'Q') then
  \rm -f  Makefile.bin Makefile.lib 
  \mv Makefile.inc Makefile
  echo ""
  echo ""
  echo "Exiting the Khoros installation script...."
  echo ""
  echo ""
  exit 0
endif

touch $TEMPDIR/botsed.$$
touch Makefile.bot
echo "/ BOOTSTRAPCFLAGS/s/BOOTSTRAPCFLAGS =.*/BOOTSTRAPCFLAGS = $bot/" >> $TEMPDIR/botsed.$$
sed -f $TEMPDIR/botsed.$$ Makefile.inc >> Makefile.bot

\rm -f $TEMPDIR/botsed.$$

#
# Set the top level src directory for the Khoros includes
#
set topsrc = $KHOROS_HOME/src
touch $TEMPDIR/topsrc.$$
echo $topsrc >>  $TEMPDIR/topsrc.$$
set new = `sed -e "s/\//\\\//g" $TEMPDIR/topsrc.$$`

touch $TEMPDIR/topsed.$$
touch Makefile.top
echo "/ TOP/s/TOP = .*/TOP = $new/" >> $TEMPDIR/topsed.$$
sed -f $TEMPDIR/topsed.$$ Makefile.bot >> Makefile.top

\rm -f $TEMPDIR/topsrc.$$ $TEMPDIR/topsed.$$


\mv Makefile.top Makefile
\rm -f  Makefile.top Makefile.bin Makefile.lib Makefile.inc Makefile.bot

echo ""
echo ""
echo "....Starting the Khoros System Compile...."
echo -n "Do you wish to continue (y/n) [y]: "
set resp = $<
if ($resp == "n" || $resp == "N") then
   echo ""
   echo ""
   echo "Exiting the Khoros installation script...."
   echo ""
   exit 0
endif

set path = ( $KHOROS_HOME/bin $path )

echo ""
echo ""
echo "Configuring the Khoros Manual Pages..."
echo ""
echo ""

#  generate the tmac files so that vman will work 
$KHOROS_HOME/man/utils/tmac/customize &

make -k World KHOROS_HOME=$KHOROS_HOME >& make.World &
