#!/bin/sh
stop="K45lsi_mrdsnmpd"  # K45 + script
start="S55lsi_mrdsnmpd" # S55 + script
snmpLSIDir="/etc/lsi_mrdsnmp"
snmpagtdir="/etc/lsi_mrdsnmp/sas"
snmpconf="sas_snmpd.conf"
agent="/usr/sbin/lsi_mrdsnmpagent"
bagent="lsi_mrdsnmpagent"
snmpd="snmpd"
main="/usr/sbin/lsi_mrdsnmpmain"
mib_dat="sas_mib.dat"
datacollector="/usr/lib/libsas_objects.so"
datacollector_version="1"
script="lsi_mrdsnmpd"
initdir=/etc/init.d

source_file="/etc/lsi_mrdsnmp/LSI_StorSNMP.ini"
destination_file="${source_file}"".tmp"

last_install=0
SNMPDCONF=""
if [ -f /etc/redhat-release ]; then
        SNMPDCONF=/etc/snmp/snmpd.conf
fi

if [ -f /etc/SuSE-release ]; then
        suse_ver=`cat /etc/SuSE-release | grep 'VERSION = 9'| wc -l` # Check for SUSE 9 Operating System
        if [ "$suse_ver" == "1" ]; then
                SNMPDCONF=/etc/snmpd.conf
        else
                SNMPDCONF=/etc/snmp/snmpd.conf
        fi
fi


Modify_SNMPD_Conf()
{
	if [ -f ${SNMPDCONF} ]; then
		cat ${SNMPDCONF} | grep "pass .1.3.6.1.4.1.3582 /usr/sbin/lsi_mrdsnmpmain" >/dev/null
		if [ $? -eq 0 ]; then
	 		grep -v  'pass .1.3.6.1.4.1.3582 /usr/sbin/lsi_mrdsnmpmain' ${SNMPDCONF} > /tmp/snmpd.conf.tmp 
	 		mv -f /tmp/snmpd.conf.tmp ${SNMPDCONF} 
		fi
	fi		
}

Delete_Config_File()
{
 	if [ ! -f ${source_file} ]
	then
		[ -x $agent ] && rm -f $agent
		[ -x $main ]  && rm -f $main
		last_install=1					
		return 0
	fi
	rm -f ${destination_file}
	while [ true ]
	do
		read line
		if [ $? -ne 0 ]
		then
			break
		fi
		no_of_controller_line=`echo ${line}|grep NoOfInstalledController`
		dat_file_name=`echo ${line}|grep "${mib_dat}"` #<<<--- Dat File Name
		if [ "${no_of_controller_line}" = "" ]
		then
			if [ "${dat_file_name}" = "" ]
			then
				echo ${line} >>  ${destination_file}
			fi	
		elif [ "${no_of_controller_line}" != "" ]
		then
			no_of_controller=`echo ${line}|sed s/NoOfInstalledController=//`
			no_of_controller=`expr $no_of_controller - 1`
			if [ ${no_of_controller} -eq 0 ]
			then
				rm -f ${source_file}
				rm -f ${destination_file}
				
				[ -x $agent ] && rm -f $agent
				[ -x $main ]  && rm -f $main
				
				Modify_SNMPD_Conf
			        last_install=1					
				return 0
			fi
			echo "NoOfInstalledController="$no_of_controller >>${destination_file}
		fi	
	done < ${source_file}

	rm -f ${source_file}
	mv  -f ${destination_file} ${source_file} 
}



#1. Stopping SAS agent
apid=`pidof $bagent | cut -f1 -d" "`
if [ "$apid" != "" ] ; then
   [ -x ${initdir}/${script} ] &&  ${initdir}/${script} stop 
fi

#2. Delete data collector entry from INI file
Delete_Config_File

#3 If this is the last installation, unregister the service
if [ -f /etc/SuSE-release ]; then
if [ ${last_install} -eq 1 ]; then
	echo "Unregistering Service"
	chkconfig -d lsi_mrdsnmpd 2>|/dev/null
fi
fi
if [ -f /etc/redhat-release ]; then
	if [ ${last_install} = 1 ]; then
   		echo "Unregistering Service"
		chkconfig --del lsi_mrdsnmpd 2>|/dev/null
	fi
fi

#4. Delete data collector soft links
rm -rf ${datacollector}
rm -rf ${datacollector}.${datacollector_version}
rm -rf /opt/lsi/snmp/lib/libstorelib.so
rm -rf /opt/lsi/snmp/lib/libstorelib.so.3

#5. if last data collector is uninstalled, remove main agent
if [ ${last_install} -eq 1 ]; then
	rm -rf ${agent}
	rm -rf ${main}
	rm -rf ${snmpLSIDir}
		
	rm -rf /etc/rc0.d/${stop}
	rm -rf /etc/rc1.d/${stop}
	rm -rf /etc/rc6.d/${stop}
	rm -rf /etc/rc2.d/${start}
	rm -rf /etc/rc3.d/${start}
	rm -rf /etc/rc4.d/${start}
	rm -rf /etc/rc5.d/${start}
        #Now remove script link at /etc/init.d/lsi_mrdsnmp
	rm -rf ${initdir}/${script}
	
	
	
	apid=`pidof ${snmpd} | cut -f1 -d" "`
	if [ "$apid" != "" ] ; then
	   [ -x ${initdir}/${snmpd} ] &&  ${initdir}/${snmpd} restart
	else
	   [ -x ${initdir}/${snmpd} ] &&  ${initdir}/${snmpd} start
	fi
fi
