#!/bin/sh
oem="Dell PercRAID"
sfile="percsnmpd" # startup file name
defsnmpconf="snmpd.conf"
snmpconf="percsnmpd.conf"
trapconf="percsnmptrap.conf"
agent="percagent"
main="percmain"
mibfile="perc.mib"
agtdir="/etc/percsnmp"

startup()
{
rm $initdir/${sfile} 2>/dev/null

cp $startup $initdir/${sfile} 2>/dev/null

ln -s ${initdir}/${sfile} $agtdir/${sfile} 2>/dev/null

ln -s $initdir/${sfile} $rcdir/rc0.d/K45${sfile} 2>/dev/null

ln -s $initdir/${sfile} $rcdir/rc1.d/K45${sfile} 2>/dev/null

ln -s $initdir/${sfile} $rcdir/rc6.d/K45${sfile} 2>/dev/null

ln -s $initdir/${sfile} $rcdir/rc2.d/S55${sfile} 2>/dev/null

ln -s $initdir/${sfile} $rcdir/rc3.d/S55${sfile} 2>/dev/null

ln -s $initdir/${sfile} $rcdir/rc4.d/S55${sfile} 2>/dev/null

ln -s $initdir/${sfile} $rcdir/rc5.d/S55${sfile} 2>/dev/null

}

install()
{
snmpdir1="/etc/snmp"
snmpdir2="/usr/local/share/snmp"
snmpdir3="/usr/share/snmp"

#### TASKS ####
#1.create the directory /etc/percsnmp if does not exist
if [ ! -d ${agtdir} ] 
then 
  mkdir ${agtdir}
fi

#3.Copy the executables
mv -f $agent $main /usr/sbin

#4.Copy the snmpd configuration file
if [ -d $snmpdir1 ]
then
  #if snmp config file exists, dont overwrite
  if [ -f ${snmpdir1}/${defsnmpconf} ] 
  then
    cp -f $snmpconf ${snmpdir1}/
    [ "$cmdline" = "1" ] && echo $snmpconf file is copied to ${snmpdir1}
    [ "$cmdline" = "1" ] && echo Please update your $defsnmpconf with proper data 
  else
    cp $snmpconf ${snmpdir1}/${defsnmpconf}
    [ "$cmdline" = "1" ] && echo $defsnmpconf file is copied to ${snmpdir1}
  fi
elif [ -d $snmpdir2 ]
then
    #if snmp config file exists, dont overwrite
    if [ -f ${snmpdir2}/${defsnmpconf} ] 
    then
      cp -f $snmpconf ${snmpdir2}/
      [ "$cmdline" = "1" ] && echo $snmpconf file is copied to ${snmpdir2}
      [ "$cmdline" = "1" ] && echo Please update your $defsnmpconf file with proper data 
    else
      cp $snmpconf ${snmpdir2}/${defsnmpconf}
      [ "$cmdline" = "1" ] && echo $defsnmpconf file is copied to ${snmpdir2}/
    fi
elif [ -d $snmpdir3 ]
then
    #if snmp config file exists, dont overwrite
    if [ -f ${snmpdir3}/${defsnmpconf} ] 
    then
      cp -f $snmpconf ${snmpdir3}/
      [ "$cmdline" = "1" ] && echo $snmpconf file is copied to ${snmpdir3}
      [ "$cmdline" = "1" ] && echo Please update your $defsnmpconf file with proper data 
    else
      cp $snmpconf ${snmpdir3}/${defsnmpconf}
      [ "$cmdline" = "1" ] && echo $defsnmpconf file is copied to ${snmpdir3}/
    fi
fi

#5. Copy the trap configuration file
#cp -f $trapconf ${agtdir} 

#6. Copy the mib file
#cp -f ${mibfile} ${agtdir} 

#7. Copy the readme.txt file
#cp -f readme.txt ${agtdir} 

#8. Installation complete
[ "$cmdline" = "1" ] && echo ${oem} SNMP Agent has been installed in your system
[ "$cmdline" = "1" ] && echo Please modify the configuration files
}

#Installtion script starts here!
#This script is common to all linux flavours.
#For installing Dell PERCRaid SNMP Agent

#Check the script called through command line or not.
[ "$1" = "" ] && cmdline=1 

if [ -d /etc/rc.d ] ; then 
  rcdir="/etc/rc.d"
else
  rcdir="/etc"
fi

if [ -d /etc/rc.d/init.d ] ; then
  initdir="/etc/rc.d/init.d"
else
  initdir="/etc/init.d"
fi

#1.Check snmpd is installed or not!
if ! [ -x /sbin/snmpd -o -x /usr/sbin/snmpd -o -x /usr/local/sbin/snmpd ] ; then
	echo snmpd is not installed.
	echo Can not continue installation of $oem Agent
	exit 1
fi

#2.Check snmptrap is installed or not!
if ! [ -x /usr/bin/snmptrap ] ; then
	if ! [ -x /usr/local/bin/snmptrap ] ; then
		echo Can not find snmptrap in /usr/bin
		echo Can not continue installation of $oem Agent
		echo Please install the ucd-snmp agent
		exit 1
	else
		[ -f /usr/bin/snmptrap ] && rm -f /usr/bin/snmptrap
		ln -s /usr/local/bin/snmptrap /usr/bin/snmptrap
	fi
fi

#3.Copy OEM correponding files
#First check redHat

redhat=`echo $MACHTYPE | grep redhat`
suse=`echo $MACHTYPE | grep suse`

if [ "$redhat" != "" -o -f /etc/redhat-release ] ; then
	arg="redhat"
elif [ "$suse" != "" ] ; then
	arg="suse"
elif [ -f /etc/turbolinux-release ] ; then
	arg="turbo"
elif [ -f /etc/debian_version ] ; then
	arg="debian"
elif ! [ -d /etc/init.d ] ; then
	arg="caldera"
fi

case "$arg" in
debian)
  startup=${sfile}.deb
  if [ ! -f ${agtdir}/${startup} ] ; then 
    echo ${startup} not found, can not install  
    exit 0
  fi
  for prefix in rh turbo cal suse
  do
     rm -f  ${sfile}.${prefix} 2>/dev/null
  done
  [ "$cmdline" = "1" ] && echo Installing $oem Agent for Debian Linux
  ;;
turbo)
  startup=${sfile}.turbo
  if [ ! -f ${agtdir}/${startup} ] ; then 
    echo ${startup} not found, can not install  
    exit 0
  fi
  for prefix in rh deb cal suse
  do
     rm -f  ${sfile}.${prefix} 2>/dev/null
  done

  [ "$cmdline" = "1" ] && echo Installing $oem Agent for Turbo Linux
  ;;
suse)
  startup=${sfile}.suse
  if [ ! -f ${agtdir}/${startup} ] ; then 
    echo ${startup} not found, can not install  
    exit 0
  fi
  for prefix in rh deb turbo cal 
  do
     rm -f  ${sfile}.${prefix} 2>/dev/null
  done
  [ "$cmdline" = "1" ] && echo Installing $oem Agent for SuSE Linux
  ;;
caldera)
  startup=${sfile}.cal
  if [ ! -f ${agtdir}/${startup} ] ; then 
    echo ${startup} not found, can not install  
    exit 0
  fi
  for prefix in rh deb suse turbo
  do
     rm -f  ${sfile}.${prefix} 2>/dev/null
  done
  [ "$cmdline" = "1" ] && echo Installing $oem Agent for Caldera Linux
  ;;
*)
  startup=${sfile}.rh
  if [ ! -f ${agtdir}/${startup} ] ; then 
    echo ${startup} not found, can not install  
    exit 0
  fi
  for prefix in deb suse turbo cal
  do
     rm -f  ${sfile}.${prefix} 2>/dev/null
  done
  [ "$cmdline" = "1" ] && echo Installing $oem Agent for RedHat Linux
esac

cd /etc/percsnmp/

#4. Copy the files
install

#5. Put it in init.d
startup

#6.
rm ${startup} -f
exit 0
