#!/bin/csh -f

# This shell script installs Hermes from a distribution tape (release
# 0.5alpha or later).  The second file on the tape should be a
# directory of the rest of the tape.  The shell script reads the
# directory and asks the user which of the available components should
# be installed.  This shell script appears in a tar file in the
# initial file on a distribution tape.  The typical sequence is to
# extract this shell script by running tar on the distribution tape,
# and then run the shell script.

# The shell script can now also install from tar files residing on disk.

# See where they want to install from
set ok=0
while (! $ok)
  echo -n "Install from disk or tape? "
  set medium="$<"
  if ("$medium" != "disk" && "$medium" != "tape") then
    echo "Please respone 'disk' or 'tape'"
  else
    set ok=1
  endif
end

if ("$medium" == "tape") then
  # Figure out what tape control program to use... mt if it exists, else
  # tctl if it exists, else we're out of luck
  set mt="`which mt`"
  set match=`expr "x$mt" : "x/"`
  if ("$match" == 0) then
    set mt="`which tctl`"
    set match=`expr "x$mt" : "x/"`
    if ("$match" == 0) then
      echo "Cannot continue becuase neither mt nor tctl is available"
      exit 1
    endif
  endif

  # Ask which tape device to use
  set ok=0
  while (! $ok)
    echo -n "Name of non-rewinding tape device: "
    set tape="$<"
    set x=`expr "$tape" : "/dev/\(.*\)"`
    if ("x$x" != "x") then
      set tape="$x"
    endif
    if (-e "/dev/$tape") then
      set ok=1
    else
      echo "That device does not appear to exist"
    endif
  end
  set tarcmd="tar xf /dev/$tape"
  set skipcmd="$mt -f /dev/$tape fsf 1"
  set rewcmd="$mt -f /dev/$tape rew"
endif

if ("$medium" == "disk") then
  # Find out where the tar files exist
  set ok=0
  while (! $ok)
    echo -n "Directory containing (compressed or uncompressed) tar files: "
    set tardir="$<"
    if (-d "$tardir") then
      set tarfiles=()
      if ("`sh -c 'echo $tardir/*.tar'`" != "$tardir/*.tar") then
        foreach x ($tardir/*.tar)
          set tarfiles=($tarfiles $x:t)
        end
      endif
      set zfiles=()
      if ("`sh -c 'echo $tardir/*.tar.Z'`" != "$tardir/*.tar.Z") then
        foreach x ($tardir/*.tar.Z)
	  set zfiles=($zfiles $x:t)
        end
      endif
      if ("$tarfiles" == "" && "$zfiles" == "") then
	echo "There are no tar files in $tardir"
      else
	set ok=1
      endif
    else
      echo "Directory $tardir does not exist"
    endif
  end
endif

# Figure out where the files will be going
set ok=0
while (! $ok)
  echo -n "Directory where Hermes should be installed: "
  set hroot="$<"
  if (-d "$hroot") then
    set ok=1
  else
    set ynok=0
    while (! $ynok)
      echo -n "Directory $hroot does not exist... create it? "
      set answer="$<"
      if ("$answer" == "y" || "$answer" == "Y") then
	set ynok=1
      else if ("$answer" == "n" || "$answer" == "N") then
	set ynok=1
      else
	echo "Please answer 'y' or 'n'"
      endif
      if ("$answer" == "y" || "$answer" == "Y") then
	set dirs=()
	set dir="$hroot"
	set nextdir=`expr "$dir{}xxx" : '\(.*\)/'`
	while ( "$nextdir" != "" )
	  set dirs=($dir $dirs)
	  set dir="$nextdir"
	  set nextdir=`expr "$dir{}xxx" : '\(.*\)/'`
	end
	if ("$dir" != "/") then
	  set dirs=($dir $dirs)
	endif
	set madedirs=()
	set ok=1
	foreach dir ($dirs)
	  if (! -d $dir) then
	    mkdir $dir
	    if ($status) then
	      echo "Unable to create directory $dir"
	      foreach madedir ($madedirs)
		rmdir $madedir
	      end
	      set ok=0
	      break
	    else
	      set madedirs=($dir $madedirs)
	    endif
	  endif
	end
      endif
    end
  endif
end
set hroot=`(cd "$hroot" ; pwd)`

if ("$medium" == "tape") then
  # Do some initial monkeying with the tape, make sure everything works
  while (! { $rewcmd } )
    echo -n "Problem with tape device... please fix and hit RETURN..."
    set jumk="$<"
  end

  # See whether we need to forward space the tape after a tar command
  set junk=`tar tf /dev/$tape`
  set junk=`tar tf /dev/$tape`
  if ("$junk" == "") then
    set skipaftertar=1
  else
    set skipaftertar=0
  endif

  $rewcmd
  if (! { $skipcmd } ) then
    echo "Tape operation failed... cannot continue"
    exit 1
  endif
endif

# Read the directory and extract component names, sizes, and descriptions
if ("$medium" == "tape") then
  rm -rf /tmp/install-hermes.$$
  mkdir /tmp/install-hermes.$$
  (cd /tmp/install-hermes.$$ ; $tarcmd ; if ($skipaftertar) $skipcmd)
  set dirname=/tmp/install-hermes.$$/directory
else if ("$medium" == "disk") then
  if (-f $tardir/directory) then
    set dirname=$tardir/directory
  else
    set dirname=""
  endif
else
  set dirname=""
endif

if ("$dirname" == "") then
  if ("$medium" == "disk") then
    set names=($tarfiles)
    set sizes=()
    foreach x ($names)
      set size=`ls -lg $tardir/$x | awk '{print $5}'`
      set sizes=($sizes $size)
    end
    foreach x ($zfiles)
      if (! ( " $tarfiles " =~ *${x:r}* ) ) then
        set names=($names $x:r)
	set size=`ls -lg $tardir/$x | awk '{print $5}'`
	set sizes=($sizes $size/compressed)
      endif
    end
    set descs=($names)
  else
    set names=()
    set sizes=()
    set descs=()
  endif  
else
  set names=(`awk '{print $1}' < $dirname`)
  set sizes=(`awk '{print $2}' < $dirname`)
  set descs=(`awk '{printf("%c",39);for(i=3;i<=NF;i++)printf("%s ",$i);printf("%c\n",39)}' < $dirname`)
  eval "set descs=($descs)"
  if ("$medium" == "tape") then
    rm -rf /tmp/install-hermes.$$
  endif
endif

# See which components they want to install
set i=1
set selections=()
set lastselection=0
while ($i <= $#names)
  set ok=0
  while (! $ok)
    if ("$names[$i]" == "required.tar") then
      echo "Will install: $descs[$i] (~$sizes[$i] bytes)"
      set selections=($selections y)
      set lastselection=$i
      set ok=1
    else
      echo -n "Install $descs[$i] (~$sizes[$i] bytes)? "
      set answer="$<"
      if ("$answer" == "y" || "$answer" == "Y") then
	set selections=($selections y)
	set lastselection=$i
	set ok=1
      else if ("$answer" == "n" || "$answer" == "N") then
	set selections=($selections n)
	set ok=1
      else
	echo "Please answer 'y' or 'n'"
      endif
    endif
  end
  set i=`expr $i + 1`
end  

# Ask whether they mind shooting off a message so we know they installed it
echo ""
echo "Subject to your approval, this shell script will automatically send"
echo "a message via electronic mail to hermes@ibm.com, notifying the"
echo "Hermes group at IBM that you have installed Hermes.  If you"
echo "do not wish this message to be sent, answer 'n' to the following"
echo "question."
set ok=0
while (! $ok)
  echo -n "OK to send notification of installation to hermes@ibm.com? "
  set answer="$<"
  if ("$answer" == "y" || "$answer" == "Y") then
    set email=1
    echo "Thank you.  We would appreciate hearing from you regarding"
    echo "your experiences with Hermes.  Please send all correspondence"
    echo "to hermes@ibm.com."
    set ok=1
  else if ("$answer" == "n" || "$answer" == "N") then
    set email=0
    echo "No mail will be sent by this shell script.  We would nevertheless"
    echo "appreciate hearing from you regarding your experiences with"
    echo "Hermes.  Please send all correspondence to hermes@ibm.com."
    set ok=1
  else
    echo "Please answer 'y' or 'n'"
  endif
end

# Now extract/skip tar files until we've passed the last selected component
set i=1
while ($i <= $lastselection)
  if ($selections[$i] == "y") then
    echo -n "Extracting component $names[$i]..."
    if ("$medium" == "disk") then
      if (-f $tardir/$names[$i]) then
	(cd $hroot ; tar xf $tardir/$names[$i] ; exit $status)
	set tarstatus=$status
      else
	(cd $hroot ; zcat $tardir/$names[$i] | tar xf - ; exit $status)
	set tarstatus=$status
      endif
    else
      (cd $hroot ; $tarcmd ; exit $status)
      set tarstatus=$status
      if ($tarstatus == 0) then
        if ($skipaftertar) then
	  $skipcmd
	  set tarstatus=$status
	endif
      endif
    endif
    if ($tarstatus != 0) then
      echo ""
      echo "Tar command returned nonzero status (continuing)"
    else
      echo done
    endif
  else
    if ($?skipcmd) then
      echo -n "Skipping component $names[$i]..."
      $skipcmd
      if ($status) then
	echo ""
	echo "Problems skipping tar file... cannot continue"
	exit 1
      endif
      echo done
    endif
  endif
  set i=`expr $i + 1`
end

if ("$medium" == "tape") then
  echo -n "Rewinding tape..."
  $rewcmd
  if ($status) then
    echo ""
    echo "Problem rewinding tape /dev/$tape"
    exit 1
  else
    echo done
  endif
endif

if (-d "$hroot/sysbin") then
  set ok=0
  while (! $ok)
    echo -n "Install Hermes shell scripts in system directory? "
    set answer="$<"
    if ("$answer" == "y" || "$answer" == "Y" \
	   || "$answer" == "n" || "$answer" == "N") then
      set ok=1
    else
      echo "Please answer 'y' or 'n'"
    endif
  end

  if ("$answer" == "y" || "$answer" == "Y") then
    set ok=0
    while (! $ok)
      echo -n "Directory to install shell scripts: "
      set sysbindir="$<"
      if (-d "$sysbindir") then
	set sysbindir=`(cd "$sysbindir"; pwd)`
	set ok=1
      else
	echo "Directory $sysbindir does not exist"
      endif
    end
    cd "$sysbindir"
    rm -f hermi hermes
    cd "$hroot/sysbin"
    make install SYSBINDIR="$sysbindir"
  endif
endif

if ($email) then
  echo "Sending electronic mail to hermes@ibm.com"
  echo "Hermes installed on `date`" | mail hermes@ibm.com
endif

echo "Hermes installation is complete."

exit 0
