#!/bin/csh 

# Install Site/SunNet/Domain Manager 2.3 jumbo patch.
#
# Usage: install_patch
#
# Note: You should change directory to the patch directory before running
#       this script.
#
# History:  Oct. 5, 1996, 1.0 - Initial release.
#           JUl 13, 1998,     - Modified for patch 105589-02

# Initial message.

echo " "
echo "This script will install the Site/SunNet/Domain Manager 2.3 Patch 105589-02"
echo " "

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

if (`whoami` != "root") then
  echo " "
  echo "  You must to be user root to run this script.  Please become root"
  echo "  and retry this script."
  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 Site/SunNet/Domain"
  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/105589-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 2.5 MB space under the $SNMHOME directory <y or n>? "
#set answer = $<
#if ("$answer" != "y" && "$answer" != "Y") then
#  echo " "
#  echo "  Aborting patch installation to avoid filling file system."
#  echo " "
#  echo "  Free up sufficient space on the file system and then re-run"
#  echo "  this script"
#  echo " "
#  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 " done"
        breaksw
    case 4.*:
        echo " done"
        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 snmver23 = `$SNMHOME/bin/snm_version $SNMHOME/bin/snm |grep 2.3 | awk '{print $2}'`

if ("$snmver23" != "2.3") then
  echo "This patch is not for this version of Site/SunNet/Domain Manager"
  echo "Terminating the installation of this patch."
  exit 1
endif

#determine the patch level

# Kill all SNM daemons.
echo " "
start:
echo "  Have you stopped all running Site/SunNet/Domain Manager applications,"
echo -n "  including the grapher, browser and discover tools <y or n>? "
set answer = $<
if ("$answer" != "y" && "$answer" != "Y") then
  echo " "
  echo "  Then please stop all Site/SunNet/Domain Manager applications now..."
  echo " "
  goto start
else
  echo " "
endif

echo "  Killing all Site/SunNet/Domain Manager daemons..."

# Kill all na.* daemons.

set daemonpids  = `ps ax | grep na"\." | awk '{print $1}'`
set daemonnames = `ps ax | grep na"\." | awk '{print $5}'`
set index = 1

foreach daemon ($daemonpids)
  (kill -9 $daemon > /dev/null)
  echo "    $daemonnames[$index]"
  @ index++
end

# 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

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

set daemonpids  = `ps ax | grep cc_sender | grep -v grep | awk '{print $1}'`
set daemonnames = `ps ax | grep cc_sender | 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 " "

# Remove activity and request log files.

#echo -n "  Do you want to remove the activity and request log files <y or n>? "
#set answer = $<
#if ("$answer" != "n" && "$answer" != "N") then
#   rm -rf /var/adm/snm/activity.log
#   rm -rf /var/adm/snm/request.log
#   echo "  Done."
#else
#   echo "  Activity and request log files will not be removed"
#endif

echo " "
echo -n "  Backing up Site/SunNet/Domain Manager files to be updated..."
if ("$snmver23" != "") then
mv $SNMHOME/lib/libnetmgt.so.3.0      $SNMHOME/lib/libnetmgt.so.3.0_pre_p105589-02
mv $SNMHOME/agents/na.ping            $SNMHOME/agents/na.ping_pre_p105589-02
mv $SNMHOME/agents/na.snmp-trap       $SNMHOME/agents/na.snmp-trap_pre_p105589-02
mv $SNMHOME/5.x/agents/na.ping        $SNMHOME/5.x/agents/na.ping_pre_p105589-02
mv $SNMHOME/5.x/agents/na.snmp-trap   $SNMHOME/5.x/agents/na.snmp-trap_pre_p105589-02
mv $SNMHOME/5.x/lib/libnetmgt.so.3    $SNMHOME/5.x/lib/libnetmgt.so.3_pre_p105589-02
mv $SNMHOME/x86/agents/na.ping        $SNMHOME/x86/agents/na.ping_pre_p105589-02
mv $SNMHOME/x86/agents/na.snmp-trap   $SNMHOME/x86/agents/na.snmp-trap_pre_p105589-02
mv $SNMHOME/x86/lib/libnetmgt.so.3    $SNMHOME/x86/lib/libnetmgt.so.3_pre_p105589-02
mv $SNMHOME/bin/snm_cmd               $SNMHOME/bin/snm_cmd_pre_p105589-02
mv $SNMHOME/bin/v2mib2schema          $SNMHOME/bin/v2mib2schema_pre_p105589-02
mv $SNMHOME/man/man1/v2mib2schema.1   $SNMHOME/man/man1/v2mib2schema.1_pre_p105589-02
endif
echo " done"

echo -n "  Installing 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 -n "  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 = ( \
   32674 \
   65092 \
   51645 \
   20591 \
   55887 \
   22103 \
   01467 \
   52363 \
   21734 \
   61532 \
   43835 \
   11883 \
   )
 
# The following checksums are computed from the installed files.

set sums = `/bin/sum  $SNMHOME/lib/libnetmgt.so.3.0 | awk '{print $1}'`
set sums = ($sums `/bin/sum  $SNMHOME/agents/na.ping | awk '{print $1}'`)
set sums = ($sums `/bin/sum  $SNMHOME/agents/na.snmp-trap | awk '{print $1}'`)
set sums = ($sums `/bin/sum  $SNMHOME/5.x/agents/na.ping | awk '{print $1}'`)
set sums = ($sums `/bin/sum  $SNMHOME/5.x/agents/na.snmp-trap | awk '{print $1}'`)
set sums = ($sums `/bin/sum  $SNMHOME/5.x/lib/libnetmgt.so.3 | awk '{print $1}'`)
set sums = ($sums `/bin/sum  $SNMHOME/x86/agents/na.ping | awk '{print $1}'`)
set sums = ($sums `/bin/sum  $SNMHOME/x86/agents/na.snmp-trap | awk '{print $1}'`)
set sums = ($sums `/bin/sum  $SNMHOME/x86/lib/libnetmgt.so.3 | awk '{print $1}'`)
set sums = ($sums `/bin/sum  $SNMHOME/bin/snm_cmd | awk '{print $1}'`)
set sums = ($sums `/bin/sum  $SNMHOME/bin/v2mib2schema | awk '{print $1}'`)
set sums = ($sums `/bin/sum  $SNMHOME/man/man1/v2mib2schema.1 | awk '{print $1}'`)

# The following names correspond the to above checksums.

set filenames = ( \
   lib/libnetmgt.so.3.0 \
   agents/na.ping \
   agents/na.snmp-trap \
   5.x/agents/na.ping \
   5.x/agents/na.snmp-trap \
   5.x/lib/libnetmgt.so.3 \
   x86/agents/na.ping \
   x86/agents/na.snmp-trap \
   x86/lib/libnetmgt.so.3 \
   bin/snm_cmd \
   bin/v2mib2schema \
   man/man1/v2mib2schema.1 \
   )

set index = 1
set err = 0
foreach check ($verify)
  if ($check != $sums[$index]) then
    echo " "
    echo "  $SNMHOME/$filenames[$index] is corrupt."
    echo "  Checksum was $sums[$index], and should have been $check"
    echo " "
    set err = 1
  endif
  @ index++
end

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

if ($err != 0) then
  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."
  echo " "
  echo "You may need to get a fresh copy of the patch."
  echo "**********************************************************************"
  exit 1

else
  echo " done"
endif

# All patch files were OK.  Installation can continue.

 
#  echo -n "  Changing permissions on snm_discover..."
#  chown root $SNMHOME/bin/snm_discover
#  chmod 4755 $SNMHOME/bin/snm_discover 
#  echo " done"
#  echo -n "  Building tt.dbase..."
#  set var_dir=`/bin/grep snmdb-directory /etc/snm.conf | /bin/awk '{print $2}'`
#  cp $SNMHOME/agents/tt.dbase $var_dir
#  chmod 644 $var_dir/tt.dbase
#  echo "done"
#  cd /tmp
#  echo -n "  Running ranlib..."
#  ranlib $SNMHOME/lib/libnetmgt.sa.3.0
#  echo " done"

#  echo -n "  Running ldconfig..."
#  ldconfig
#  echo " done"

#set snmpddir = /etc/init.snmpd
#if ( -e $snmpddir) then
#echo -n "  "
         /etc/init.snmpd start
#endif 
endif

echo " "
echo " "
echo "Patch 105589-02 for Site/SunNet/Domain Manager 2.3 has been installed."
echo " "
echo "Please note:"
echo " "
echo "If you have previously run the getagents script to install the"
echo "SunNet Manager agents on to other systems, you must now re-run"
echo "getagents to update those systems with the new agent libraries"
echo "and binaries delivered by this patch."
echo " "
echo "Please restart your Site/SunNet/Domain Manager now."
echo " "
exit
