#!/bin/csh 

# Install Cooperative Console 1.1  jumbo patch.
#
# Usage: install_patch
#
# Note: You should change directory to the patch directory before running
#       this script.
#
# History:  
# Initial message.

echo " "
echo "This script will install the Cooperative Console 1.1 Jumbo"
echo "Patch 103580-02"
echo " "

# Check to see if we are root.  Exit if we're not with a message.

if (`whoami` != "root") then
  echo "You need to be root to run this script.  Please become root"
  echo "and retry this script.  Thanks."
  echo " "
  exit 1
endif

# Check to see if the SNMHOME environment variable is set.  This script
# will not work unless SNMHOME is set.

if (! $?SNMHOME) then
  echo " "
  echo "  The SNMHOME environment variable is not defined."
  echo " "
  echo "  This environment variable indicates where SunNet "
  echo "  Manager is installed on your system e.g. /usr/snm "
  echo "  This script cannot update your installation without knowing "
  echo "  this information."
  echo " "
  echo "  Please define SNMHOME and then re-run this script."
  echo " "
  exit 1
endif

if (! -e $SNMHOME) then
  echo " "
  echo "  The directory that the SNMHOME environment variable refers to"
  echo "  ($SNMHOME) was not found"
  echo " "
  echo "  Please correct SNMHOME and then re-run this script."
  echo " "
  exit 1
endif


# Local variables.

# The following variable needs to be set to the directory into which you have
# copied the jumbo patch raw files (usually from a patch tape, or possibly
# you have ftp'd these files from The Internet somewhere.

set patchdir = /tmp/103580-02

# If you have not explicity set the previous variable, or if it is set to a
# non-existent directory, this script will try to operate with the current
# working directory as the patch directory.

echo " "
echo "Checking that patch is accessable..."
if (! -e $patchdir) then
  echo " "
  echo "  The patch was not found at $patchdir"
  echo " "
  echo "  Assuming that the patch is located in the present directory"
  echo " "
  set patchdir = .
endif

echo -n "  Have you reserved 14M space under $SNMHOME directory y or n?"
set answer = $<
if ("$answer" != "y" && "$answer" != "Y") then
  echo " "
  echo "  Abort, otherwise patch may flood file system "
  echo " "
  echo "  Free up sufficient space on the file system and then re-run"
  echo "  this script"
  exit 1 
endif

# determine  OS
echo " "
echo -n "Checking that machine is running SunOS..."
set os_level = `uname -r 2>/dev/null`

switch ($os_level)
    case '':
        echo SUNOS Continue..
        breaksw
    case 4.*:
        echo SUNOS Continue..
        breaksw
    case 5.*:
        echo This patch is not for SVR4
        exit 1
    case *:
        echo This patch is not for SVR4
        exit
    default:
        echo "no such OS\n
        exit 1
endsw
 
# determine the SNM version

set snmver222 = `$SNMHOME/bin/snm_version $SNMHOME/bin/snm |grep 2.2.2 | awk '{print $2}'`
set snmver223 = `$SNMHOME/bin/snm_version $SNMHOME/bin/snm |grep 2.2.3 | awk '{print $2}'`

if ("$snmver222" != "2.2.2" && "$snmver223" != "2.2.3") then
  echo "this patch is not for your SNM product"
  exit 1
endif

#determine the patch level

set patchlvl2 = `$SNMHOME/bin/snm_version $SNMHOME/bin/snm |grep 2.2.2 | awk '{print $9}'`
 
set patchlvl1 = `$SNMHOME/bin/snm_version $SNMHOME/bin/snm |grep 2.2.3 | awk '{print $9}'`

if ($patchlvl2 > 4 || $patchlvl1 > 4) then
  echo "this patch is not for your CC product"
  exit 1
endif

# Kill any remaining processes with "snm" in their names.

set daemonpids  = `ps ax | grep snm | grep -v snmd | grep -v xnews | grep -v grep | awk '{print $1}'`
set daemonnames = `ps ax | grep snm | grep -v snmd |  grep -v xnews | grep -v grep | awk '{print $5}'`
set index = 1
foreach daemon ($daemonpids)
  (kill -9 $daemon > /dev/null)
  echo "    $daemonnames[$index]"
  @ index++
end

echo "  Done."
echo " "

echo -n "  Copying patch files to $SNMHOME... "
cd $patchdir
cp -r `arch`/snm/* $SNMHOME
if ($status != 0) then
  echo "***************************************************************************"
  echo "***************************************************************************"
  echo "The patch file directory ($patchdir) does not contains the patch"
  echo "files.  Please re-run this script after changing into the patch directory."
  echo "***************************************************************************"
  echo "***************************************************************************"
  exit 1
endif

echo "done."

echo "  Verifying checksums of all installed patch files."

# In the following three arrays, the order is important.  The checksums and
# filenames are listed in order of the output from the "ls" command.

# The following checksums came hard-coded with the patch tape.

set verify = ( \
  57298 \
  58665 \
  63871 \
  12210  \
  13343  \
  36965 \
  58539 \
  65472 \
  28277 \
  60768 \
  43580 \
  22233 \
  00132 \
  24622 \
  12563 \
  39335 \
  32670 \
  58024 \
  21129 \
  09502 \
  06720 \
  )

# The following checksums are computed from the installed files.
echo "start sum"

set sums = `/bin/sum $SNMHOME/agents/cc_sender | awk '{print $1}'`
set sums = ($sums `/bin/sum $SNMHOME/bin/cc_config|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/bin/cc_receiver|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/bin/install_cc_license|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/bin/modify_el|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/filter/filter_none.ccf|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/filter/infrastructure.ccf|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/filter/passall.cct|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/filter/routers-only.ccf|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/filter/views-only.cct|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/help/cc_config.info|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/help/cc_receiver.info|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/icons/holding.icon|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/icons/holding.iconmask|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/lib/libcoop.a|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/lib/libcoop.sa.1.1|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/lib/libcoop.so.1.1|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/man/man1/cc_config.1|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/man/man1/cc_receiver.1|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/man/man8/cc_sender.8|awk '{print $1}'`)
set sums = ($sums `/bin/sum $SNMHOME/struct/cooptools.schema|awk '{print $1}'`)

# The following names correspond the to above checksums.

set filenames = ( \
  agents/cc_sender \
  bin/cc_config \
  bin/cc_receiver \
  bin/install_cc_license \
  bin/modify_el \
  filter/filter_none.ccf \
  filter/infrastructure.ccf \
  filter/passall.cct \
  filter/routers-only.ccf \
  filter/views-only.cct \
  help/cc_config.info \
  help/cc_receiver.info \
  icons/holding.icon \
  icons/holding.iconmask \
  lib/libcoop.a \
  lib/libcoop.sa.1.1 \
  lib/libcoop.so.1.1 \
  man/man1/cc_config.1 \
  man/man1/cc_receiver.1 \
  man/man8/cc_sender.8 \
  struct/cooptools.schema \
  )

echo "start verify"
set index = 1
set err = 0
foreach check ($verify)
  echo -n "    $filenames[$index] ($sums[$index])... "
  if ($check != $sums[$index]) then
    echo " "
    echo "$SNMHOME/$filenames[$index] is corrupt."
    echo "It's checksum was $sums[$index], and should have been $check"
    set err = 1
  else
    echo "verify OK."
  endif
  @ index++
end

# Print error message and abort if any of the patch files was corrupted.

if ($err != 0) then
  echo " "
  echo "************************************************************************
***"
  echo "************************************************************************
***"
  echo "One or more of the installed patch files were corrupted.  See the above"
  echo "messages for details.  Please check your patch files for integrity.  You
"
  echo "may need to get a new patch tape or re-install SNM."
  echo "************************************************************************
***"
  echo "************************************************************************
***"
  exit 1

else

# All patch files were OK.  Installation can continue.
  cd /tmp
  ranlib $SNMHOME/lib/libcoop.a
  ranlib $SNMHOME/lib/libcoop.sa.1.1
  ranlib $SNMHOME/lib/libcoop.so.1.1

  echo "ranlib done"
  ldconfig
  echo "ldconfig done."

set snmpddir = /etc/init.snmpd

if ( -e $snmpddir) then
         /etc/init.snmpd start
endif 
endif

echo "Installation is complete.  Please note:"
echo " "
echo "If you have previously run the getagents script to install the SunNet"
echo "Manager onto other systems, then you will need to run getagents again,"
echo "if you wish to update the agent binaries and libraries on those systems."
 
echo " "
echo "You may now restart your SunNet Manager."
exit
