#!/bin/bash
###################################
###  POST REMOVE SCRIPT          ###
###################################
if [ "$SIS_DEBUG_MODE" = "true" ]; then
  debug_logfile=/var/tmp/sdcss-kmod_postun$$.log;
  exec 1<&-       # Close STDOUT file descriptor
  exec 2<&-       # Close STDERR FD
  exec 1<>$debug_logfile # Open STDOUT as $LOG_FILE file for read and write.
  exec 2>&1       # Redirect STDERR to STDOUT
  set -x          # Turn on shell debugging
fi

# For dpkg (ubuntu) and rpm upgrades,  this is run during package upgrade.
# We don't to run this script on upgrade as it does indiscriminate cleanup
if [ "$1" = "upgrade" ] || [ "$1" = "1" ]; then exit 0; fi

run()
{
   $*; _rc=$?
   [ $_rc != 0 ] && logmsg "Error $_rc running: $*";
   return $_rc; 
}

if [ -f /etc/symantec/sis/sis.conf ]; then
   BASEDIR=`grep "^SisInstallDir=" /etc/symantec/sis/sis.conf |cut -d= -f2`
fi

if [ "$BASEDIR" = "" ]; then
   case `uname` in
      Linux) BASEDIR=`rpm -q sdcss-kmod --queryformat "%{INSTALLPREFIX}" 2>/dev/null` ;;
      SunOS) printf "Nothing to do" ;;
   esac
   [ $? != 0 ] && BASEDIR=""
fi

[ "$BASEDIR" = "" ] && BASEDIR=/usr/lib/symantec

   # Reboot Message (Solaris|Linux)
   case `uname` in 
   AIX|SunOS)
     printf "Nothing to do"  ;;
   Linux)
     grep -E '^sisips|^sisfim|^sisevt' /proc/modules >/dev/null 2>&1; module_loaded=$?
     if [ -f /etc/sisips/rebootRequired ] || [ $module_loaded = 0 ]; then
        printf "\n\nUninstall completed successfully. In order to remove the driver files"
        printf "\nfrom the system, a reboot is required."
        printf "\nPlease reboot your machine at the earliest convenience.\n\n"
     fi ;;
   esac

   # Dirs and Files cleanup
   # Do not delete /etc/symantec/sis/sis.conf, since it would be used by agent package also.
   rm -f /etc/init.d/sis{ips,ids,evt,ap}.init
   rm -f /etc/init.d/sisips.nfsd
   rm -rf /etc/symantec/sis/driver
   rm -f /etc/symantec/sis/rc.functions
   rm -f /etc/symantec/sis/sis*kernels.txt*
   
   if [ -d /etc/init ]; then
       rm -rf /etc/init/sisips-init.conf > /dev/null 2>&1
       rm -rf /etc/init/sisips-nfsd.conf > /dev/null 2>&1
   fi
   [ "$BASEDIR" != "" ] && [ "$BASEDIR" != "/" ] && rm -rf $BASEDIR/sdcssagent/driver/*


printf "\nSymantec Agent for Linux Kernel package (sdcss-kmod) uninstalled successfully.\n"
exit 0
