#!/bin/sh
# cd Autorun for Active System Console
#


ROOTdir=`pwd` 
ARCH="ia64"
if [ $HOSTTYPE != $ARCH ]; then
  ARCH="i386"
fi;
RVERSION=`uname -r`
ULANG=`echo $LANG | sed "s/\..*$//;s/_.*$//;" `

if [ -e "/etc/redhat-release" ]; then
    NVERSION="RedHat"
elif [ -e "/etc/SuSE-release" ]; then
    NVERSION="SUSE"
elif [ -e "/etc/fedora-release" ]; then
    NVERSION="RedHat"
elif [ -e "/etc/debian-release" ]; then
    NVERSION="RedHat"
elif [ -e "/etc/centos-release" ]; then
    NVERSION="RedHat"
else
    NVERSION=`uname -s`
fi;

if [ "$UID" -eq "0" ]; then
  ADMIN=true
else
  ADMIN=false
fi;

echo "Active System Console"
echo ":"
echo "Checking for Installed Packages"
#detect installed applications
CACHERIVER=`rpm -qa | grep CacheRiver`
if [ $? != 0 ]; then 
  CACHERIVER="MISSING"
  echo "   Intel(R) CacheRiver: NOT INSTALLED"
else
  echo "   Intel(R) CacheRiver: $CACHERIVER"
fi;
XERCES=`rpm -qa | grep XERCES-3.1.1`
if [ $? != 0 ]; then 
   XERCES="MISSING"
   echo "   Xerces: NOT INSTALLED"
else
   echo "   Xerces: $XERCES"
fi;
APPCORE=`rpm -qa | grep APPCORE` 
if [ $? != 0 ]; then  
   APPCORE="MISSING" 
   echo "   AppCore: NOT INSTALLED" 
else 
   echo "   AppCore: $APPCORE" 
fi; 
ASC=`rpm -qa | grep ASC-`
if [ $? != 0 ]; then 
   ASC="MISSING"
   echo "   Intel(R) Active System Console: NOT INSTALLED"
else
   echo "   Intel(R) Active System Console: $ASC"
fi;
echo ":"

#check everything, exit on failure otherwise install and launch ASC
if [ $ADMIN == false ]; then
   echo "You must be root to uninstall"
   exit
fi;

function delcronjob
{
   crontab -l | grep -v "ascservicemonitor" > /tmp/__asccronjoblist__
   crontab /tmp/__asccronjoblist__
   rm -f /tmp/__asccronjoblist__
}

function delbmcresetter
{
	if [ -e "/etc/SuSE-release" ]; then
            if [ -e "/etc/rc.d/boot.local" ]; then
		   cat /etc/rc.d/boot.local | grep -v "bmcresetter" > /tmp/rc_local_back
		   mv -f /tmp/rc_local_back /etc/rc.d/boot.local
                   rm -f /tmp/rc_local_back
            fi;
         else
                # REDHAT variants       
            if [ -e "/etc/rc.d/rc.local" ]; then
                   cat /etc/rc.d/rc.local | grep -v "bmcresetter" > /tmp/rc_local_back
                   mv -f /tmp/rc_local_back /etc/rc.d/rc.local
                   rm -f /tmp/rc_local_back
            fi;
         fi;
}

#uninstall RPMs

     #stop services monitor
     delcronjob
     delbmcresetter

     
     #uninstall ASC
      if [ $ASC != "MISSING" ]; then
         echo "Removing Intel(R) Active System Console"
         rpm -e $ASC
      fi;

     #uninstall APPCORE
      if [ $APPCORE != "MISSING" ]; then
         echo "Removing Intel(R) AppCore"
         rpm -e $APPCORE
      fi;

     #uninstall Cache River 2.0
      if [ $CACHERIVER != "MISSING" ]; then
         echo "Removing Intel(R) CacheRiver"
         rpm -e $CACHERIVER
      fi;


      #uninstall XERCES 3.1.1
      if [ $XERCES != "MISSING" ]; then
         echo "Removing XERCES"
         rpm -e $XERCES
      fi;

echo ":"


echo "Verifing Uninstallation..."
#verify uninstall
if [ -e /etc/init.d/ascpolicy ]; then
   echo "ascpolicy service still exists, attempting to manually remove"
   /etc/init.d/ascpolicy stop
   rm -rf /etc/init.d/ascpolicy   
fi;
if [ -e /etc/init.d/lighttpd ]; then
   echo "lighttpd service still exists, attempting to manually remove"
   /etc/init.d/lighttpd stop
   rm -rf /etc/init.d/lighttpd
fi;
if [ -e /etc/sysconfig/lighttpd ]; then
  echo "attempting to remove: /etc/sysconfig/lighttpd"
  rm -rf  /etc/sysconfig/lighttpd
fi;
if [ -e /etc/lighttpd ]; then
  echo "attempting to remove: /etc/lighttpd"
  rm -rf /etc/lighttpd
fi;
if [ -e /var/log/lighttpd ]; then
  echo "attempting to remove: /var/log/lighttpd"
  rm -rf /var/log/lighttpd
fi;
if [ -e /etc/ld.so.conf.d/asc.conf ]; then
   echo "asc config file exists, attempting to remove manually"
   rm -rf /etc/ld.so.conf.d/asc.conf
fi;

`ls /var/log/asc/* 1>/dev/null 2>/dev/null`
if [ $? -ne 0 ]; then
  if [ -e /var/log/asc ]; then
    echo "attempting to remove: /var/log/asc"
    rm -rf /var/log/asc
  fi;
else
  echo "directory not empty, cannot remove: /var/log/asc"
fi;

# Always delete this folder
if [ -e /usr/local/asc ]; then
  echo "attempting to remove: /usr/local/asc"
  rm -rf /usr/local/asc/
fi;

#cleanup for CR
rm -rf /root/crlog || true 


`ls /var/lib/php/session/sess_* 1>/dev/null 2>/dev/null`
if [ $? -eq 0 ]; then
  echo "directory not empty, cannot remove: /var/lib/php/session"
fi;


echo ":"
echo "uninstallation complete"
