#!/bin/csh -f 

# parse options
################
set thisdir = $cwd
set quiet_mode = 1
set mountpt = /cdrom

# defaults
################
set ebt_bin = "$thisdir/bin"
set ebt_lib = "$thisdir/lib"
set ebt_include = "$thisdir/include"
set ebt_data = "$thisdir/data"
set ebt_examples = "$thisdir/examples"
set ebt_csh = "$thisdir/bin"
set ebt_books = "$thisdir/ebt_books"
set ebt_tmp = "$thisdir/tmp"
set ebt_xdir = "X11"

# do server
########################
set ebt_doserver = -1
# -1: runtime browser

# arch specific programs
########################
set osv = `uname -sr`
switch ("$osv")
   case "IRIX 5*":
      set tocopts = "tf"
      set taropts = "xf"
      set rsh_prog = "rsh"
      set ps_prog = "ps -elf"
      set myarch = "sgi5"
      set myid = `whoami`
      set ebt_xdir = "Xsgi"
      set sys_size = `df . | tail -1 | awk '{print $5}'`
      set ebt_size = 64000
      breaksw
   case "SunOS 5*":
      set taropts = "xf"
      set tocopts = "tf"
      set rsh_prog = "rsh"
      set ps_prog = "ps -elf"
      set myarch = "sun5"
      set myid = "$USER"
      set ebt_xdir = "X11"
      set sys_size = `df . | tail -1 | awk '{print $4}'`
      set ebt_size = 32000
      breaksw
   case "SunOS 4*":
      set taropts = "xf"
      set tocopts = "tf"
      set rsh_prog = "rsh"
      set ps_prog = "ps -aux"
      set myarch = "sun4"
      set myid = "$USER"
      set ebt_xdir = "X11"
      set sys_size = `df . | tail -1 | awk '{print $4}'`
      set ebt_size = 32000
      breaksw
   default:
      ## unsupported architecture
      echo "Your operating system, $osv, is not supported by this software."
      echo "Install program will terminate."
      exit 1
      breaksw
endsw

# options
################
set release_size = 30
if ($sys_size < $ebt_size) then
  echo "Your system does not have the required 30MB of free space"
  echo " in the current directory, $cwd,"
  echo " to install this software."
  echo "Install program will terminate."
  exit 1
endif

# create directory structure
############################

echo "Creating subdirectories"
set dir_list = ("$thisdir/bin" "$thisdir/lib" "$thisdir/include" "$thisdir/data" "$thisdir/examples" "$thisdir/bin" "$thisdir/ebt_books" "$thisdir/tmp")
foreach dir ($dir_list)
  if (-e "$dir") then
    if (!(-d "$dir")) then
      echo "It seems there is a file $dir"
      echo " in this directory.  Since this software needs to create"
      echo " a directory of this name, the install process cannot continue."
      exit 1
    endif
  else
    mkdir "$dir" >& /dev/null
  endif
  if (!(-d "$dir")) then
    echo "It seems you do not have write permission in the current"
    echo "directory, $cwd.  The install process cannot continue."
    exit 1
  endif
end

# copy release files from cdrom
###############################

if (! -d "$mountpt/unix") then
  echo "Your CD-ROM seems to be no longer mounted.  This program"
  echo " cannot read the browser software release files.  Mount"
  echo " the CD-ROM and start the install program again."
  exit 1
endif
echo "Copying files from CD-ROM.  This will take a few minutes..."
foreach file (binsgi5 binsun4 binsun5 csh data ebtbooks examples include \
              libsgi5 libsun4 libsun5 motif ol)
  /bin/cp $mountpt/unix/$file.z ./$file.tar.Z
  echo -n "."
end
echo " "
/bin/cp $mountpt/unix/fullrun.auth .

# install binaries
#########################
   if (-f bin$myarch.tar.Z) then
      echo ' '
      echo "Installing the $myarch binaries in $ebt_bin..."
      uncompress -c bin$myarch.tar.Z | (cd $ebt_bin; tar $taropts -)
      if ($status != 0) goto errorfct
   endif


# install libraries
#########################
   if (-f lib$myarch.tar.Z) then
      echo ' '
      echo "Installing the $myarch libraries in $ebt_lib..."
      uncompress -c lib$myarch.tar.Z | (cd $ebt_lib; tar $taropts -)
      if ($status != 0) goto errorfct
   endif

# install include
#########################
echo ' '
echo "Installing the include files in $ebt_include..."
uncompress -c include.tar.Z | (cd $ebt_include; tar $taropts -)
if ($status != 0) goto errorfct

# install data
#########################
echo ' '
echo "Installing the data files in $ebt_data..."
uncompress -c data.tar.Z | (cd $ebt_data; tar $taropts -)
if ($status != 0) goto errorfct

# install examples
#########################
echo ' '
echo "Installing the examples in $ebt_examples..."
uncompress -c examples.tar.Z | (cd $ebt_examples; tar $taropts -)
if ($status != 0) goto errorfct

# install csh
#########################
echo ' '
echo "Installing the shellscripts in $ebt_csh..."
uncompress -c csh.tar.Z | (cd $ebt_csh; tar $taropts -)
if ($status != 0) goto errorfct

# install books
#########################
echo ' '
echo "Installing the demo book database in $ebt_books..."
uncompress -c ebtbooks.tar.Z | (cd $ebt_books; tar $taropts -)
if ($status != 0) goto errorfct

# motif setup
##############################
echo ' '
echo "Installing Motif data files..."
if (-d $ebt_tmp) then
   uncompress -c motif.tar.Z | (cd $ebt_tmp; tar $taropts -) >& /dev/null
   if ($status != 0) goto errorfct
endif

# environment stuff
#########################
set ebt_server_data = $ebt_data/security/fullrun.auth
cp fullrun.auth $ebt_server_data

# convert the .ebtrc file
#########################
echo ' '
echo 'Converting the example .ebtrc file...'
# Check for CD-ROM availability
if (! -d "$mountpt/unix") then
  echo "Your CD-ROM is no longer mounted.  This installation cannot"
  echo " build a configuration file."
  exit 1
endif
# Look at CD-ROM for sets of books to put in .ebtrc
set book_list = ""
foreach file ( `cd "$mountpt" ; /bin/ls` )
  if (-d "$mountpt/$file"  && -e "$mountpt/$file/booklist.txt") \
  set book_list = ($book_list $file)
end
# build .ebtrc
/bin/mv $ebt_examples/ebtrc/.ebtrc $ebt_examples/ebtrc/.ebtrc.old >& /dev/null
set sdata = `echo $ebt_data | sed "s/\//\\\//g"`
set sbooks = `echo $ebt_books | sed "s/\//\\\//g"`
set sserver = `echo $ebt_server_data | sed "s/\//\\\//g"`
sed "s/EBT_DATA/$sdata/g" $ebt_examples/ebtrc/ebtrc.orig | \
sed "s/EBT_BOOKS/$sbooks/g" | \
sed "s/EBT_XDIR/$ebt_xdir/g" | \
sed "s/EBT_SERVER/$sserver/g" > $ebt_examples/ebtrc/.ebtrc
# add lines for books on CD-ROM
foreach book_set ($book_list)
  set book_name = `cat $mountpt/$book_set/title.txt`
  echo "BOOK_DBASE      $mountpt/$book_set=$book_name" >> \
     $ebt_examples/ebtrc/.ebtrc
end
echo ' '
echo -n 'Hit Return to Continue: '
set result = $<

# library level index
##############################
if (-f $ebt_books/booklist.dt) then
   cat $ebt_books/booklist.dt >> $ebt_books/booklist.txt
endif
if (-x $ebt_bin/mklibidx) then
   clear
   echo ' '
   setenv EBTRC null
   unsetenv EBTRC
   echo 'Updating EBT library level index...'
   echo "$ebt_bin/mklibidx -bkdb $ebt_books"
   (cd $ebt_examples/ebtrc; $ebt_bin/mklibidx -bkdb $ebt_books)
   echo -n 'Hit Return to Continue: '
   set result = $<
endif

# cleaning up
##############################
echo ' '
echo 'Cleaning up...'
/bin/rm -f *.tar.Z
(sleep 3; /bin/rm -f install.lb fullrun.auth) &

# finished
##############################
echo ' '
echo "EBT DynaText Installation Complete."
exit 0

# error function
##############################
errorfct:
echo ' '
echo "An unexpected error occurred while extracting the software."
echo "This could be due to not having write privileges"
echo "in the current directory, or due to running out of"
echo "disk space.  Check these things and run the install"
echo "script again."
exit 1
