#!/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
      echo "That device does not appear to exist"
      continue
    endif
    set tarcmd="tar xf /dev/$tape"
    set skipcmd="$mt -f /dev/$tape fsf 1"
    set rewcmd="$mt -f /dev/$tape rew"

    # Do some initial monkeying with the tape, make sure everything works
    if (! { $rewcmd } ) then
      echo "Unable to rewind tape device"
      continue
    endif

    # Make sure this is really a non-rewinding device
    set junk1=`tar tf /dev/$tape`
    set junk2=`tar tf /dev/$tape`
    if ("$junk2" == "$junk1") then
      echo "Device $tape does not appear to be a NON-REWINDING tape device"
      continue
    endif
    if ("$junk2" == "") then
      set skipaftertar=1
    else
      set skipaftertar=0
    endif

    $rewcmd

    # Leave tape positioned after the installation script
    if (! { $skipcmd } ) then
      echo "Tape operation failed... cannot continue"
      exit 1
    endif

    set ok=1
  end
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 tardir=`(cd $tardir ; pwd)`
      set files=(`find $tardir/*.tar $tardir/*.tar.Z /dev/null* \( -name '*.tar' -o -name '*.tar.Z' \) -type l -print`)
      set links=()
      foreach x ($files)
	set links=($links $x:t)
      end
      set files=(`find $tardir/*.tar /dev/null* -name '*.tar' -print`)
      set tarfiles=()
      foreach x ($files)
	set add=yes
	foreach link ($links)
	  if ("$x:t" == "$link") then
	    set actual=`ls -l $tardir/$link | awk '{print $NF}'`
	    foreach y ($files)
	      if ("$y:t" == "$actual") set add=no
	    end
	  endif
	end
	if ("$add" == "yes") set tarfiles=($tarfiles $x:t)
      end
      set files=(`find $tardir/*.tar.Z /dev/null* -name '*.tar.Z' -print`)
      set zfiles=()
      foreach x ($files)
	set add=yes
	foreach link ($links)
	  if ("$x:t" == "$link") then
	    set actual=`ls -l $tardir/$link | awk '{print $NF}'`
	    foreach y ($files)
	      if ("$y:t" == "$actual") set add=no
	    end
	  endif
	end
	if ("$add" == "yes") set zfiles=($zfiles $x:t)
      end
      if ($#tarfiles == 0 && $#zfiles == 0) 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)`

# 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.$$
  else
    set diskfiles=($tarfiles)
    foreach x ($zfiles)
      set diskfiles=($diskfiles $x:r)
    end
    foreach x ($diskfiles)
      set found=no
      foreach y ($names)
	if ("$x" == "$y") set found=yes
      end
      if ("$found" == "no") then
	set names=($names $x)
	# Note.... we should really add this component's name to
	# descs[], but since some existing elements of that array
	# contain embedded spaces, that's extremely difficult to do.
	# So instead we'll leave descs[] shorter than names[], and all
	# following code should use names[i] instead of descs[i] when
	# i > $#descs.  Yuck!
	if (-f $tardir/$x) then
	  set size=`ls -lg $tardir/$x | awk '{print $5}'`
	else
	  set size=`ls -lg $tardir/$x.Z | awk '{print $5 "/compressed"}'`
	endif
	set sizes=($sizes $size)
      endif
    end
  endif
endif

echo $names
# See which components they want to install
set i=1
set selections=()
set lastselection=0
while ($i <= $#names)
  set ok=0
  if ( ! ( -f $tardir/$names[$i] || -f $tardir/$names[$i].Z ) ) then
    set selections=($selections n)
    set ok=1
  endif
  if ($i > $#descs) then
    set desc="$names[$i] (no description available)"
  else
    set desc="$descs[$i]"
  endif
  while (! $ok)
    if ("$names[$i]" == "required.tar") then
      echo "Will install: $desc (~$sizes[$i] bytes)"
      set selections=($selections y)
      set lastselection=$i
      set ok=1
    else

      echo -n "Install $desc (~$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@watson.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@watson.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@watson.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@watson.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 script 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 hparse hsh
    cd "$hroot/sysbin"
    rm -f $sysbindir/hermes
    cp hermes $sysbindir
    chmod +x $sysbindir/hermes
  endif
endif

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

echo "Hermes installation is complete."

exit 0
