
#####################################################################
# symbol_present()
# helper function
#####################################################################
symbol_present() 
{
   SYMBOL=$1
   shift
   BINLIST=$*

   for i in $BINLIST; do 
      if [ -f $i ]; then
         if [ "x" != "x`grep $SYMBOL $i`" ]; then
            return 1
         fi
      fi
   done	

   return 0
}

#####################################################################
# isSNMPSuitable()
# checks the various lib to see if the cmaX extension is loaded or
# or can be loaded
#
# returns: YES cmaX is loaded or can be loaded
#          NO  the current snmp stack can not load cmaX
#####################################################################
isSNMPSuitable()
{
   BINSTOCHECK="/usr/lib/libucdmibs.so* /usr/lib/libnetsnmpmibs.so* /usr/lib64/libucdmibs.so* /usr/lib64/libnetsnmpmibs.so*"
   CMAXLOCATION=""
   SNMP_SUITABLE="YES"

   # First, we will check for linked in cmaX extensions
   symbol_present init_cmaX $BINSTOCHECK		
   RETVAL=$?
	
   if [ $RETVAL -eq 1 ]; then
       echo "snmp_check" "NOTE: Built-in hp ProLiant management Extension (cmaX) in SNMP stack found."
      SNMP_SUITABLE="YES"
      return
   fi

   #If we got this far then cmaX is NOT already in the stack.
   #Check whether we can link this cmaX stuff dynamically
   BINSTOCHECK="/usr/lib/libucdmibs.so* /usr/lib/libnetsnmpmibs.so*"
   symbol_present deinit_dlmod $BINSTOCHECK		
   RETVAL=$?
   if [ $RETVAL -eq 1 ]; then
      CMAXLOCATION=/usr/lib/libcmaX.so
   fi

   BINSTOCHECK="/usr/lib64/libucdmibs.so* /usr/lib64/libnetsnmpmibs.so*"
   symbol_present deinit_dlmod $BINSTOCHECK		
   RETVAL=$?
   if [ $RETVAL -eq 1 ]; then
      CMAXLOCATION=/usr/lib64/libcmaX64.so
   fi

   if [ -e $CMAXLOCATION ]; then
      SNMP_SUITABLE="YES"
      return
   else
      SNMP_SUITABLE="NO"
      return
   fi
}

#####################################################################
# getSNMPConfFile()
#####################################################################
getSNMPConfFiles() 
{
   CMACONFFILE=/opt/compaq/cma.conf

   case "$OS_DIST" in
      Red*|VMware*)
         SNMPDCONFDEFPATH="/etc/snmp:/usr/share/snmp:/usr/lib/snmp:~/.snmp"
         SNMPDCONFFILE="/etc/snmp/snmpd.conf"
      ;;
      SuSE*|Unitedlinux*)
         SNMPDCONFDEFPATH="/etc:/usr/share/snmp:/usr/lib/snmp:~/.snmp"
         SNMPDCONFFILE="/usr/share/snmp/snmpd.conf"
      ;;
      Debian*)
         SNMPDCONFDEFPATH="/etc/snmp:/usr/share/snmp:~/.snmp"
         SNMPDCONFFILE="/etc/snmp/snmpd.conf"
      ;;
      *)
         SNMPDCONFDEFPATH="/etc:/usr/share/snmp:/usr/lib/snmp:~/.snmp"
         SNMPDCONFFILE="/etc/snmp/snmpd.conf"
      ;;
   esac

   TMPPATH=`echo $SNMPDCONFDEFPATH | sed 's/:/ /g'`
   for i in $TMPPATH; do
     [ -f "$i/$SNMPDCONF" ] && SNMPDCONFUSED="$SNMPDCONFUSED $i/$SNMPDCONF"
     [ -f "$i/ucd$SNMPDCONF" ] && SNMPDCONFUSED="$SNMPDCONFUSED $i/ucd$SNMPDCONF"
     [ -f "$i/net$SNMPDCONF" ] && SNMPDCONFUSED="$SNMPDCONFUSED $i/net$SNMPDCONF"
   done
}

#####################################################################
# getSNMPConfig()
#####################################################################
getSNMPConfig()
{
   SNMPDCONF=snmpd.conf
   SNMPDCONFUSED=""

   getSNMPConfFiles

   echo "SNMPDCONFDEFPATH=$SNMPDCONFDEFPATH" 

#echo "SNMPDCONFUSED=$SNMPDCONFUSED" 
   SNMPDLOCALWRITECOMMUSED=""
   SNMPDLOCALREADCOMMUSED=""
   SNMPDNONLOCALWRITECOMMUSED=""
   SNMPDNONLOCALREADCOMMUSED=""
   SNMPDTRAPDEFCOMM=""
   SNMPDTRAPDESTANDCOMM=""
   SNMPDSYSCONTACT=""
   SNMPDXLOCATION=""
   SNMPDLOCATION=""
   SNMPDAGENTAEXCLUDE=""
 
   # bail if no snmp conf file found.
   if [ -z "$SNMPDCONFUSED" ]; then
	 return
   fi
 
   TMPPATH=/tmp/tmp$$
 
   #Get the R/W community string and system
   egrep -h '^[ 	]*rwcommunity[ 	].*$' $SNMPDCONFUSED > $TMPPATH
   while read TEMPSTR1 TEMPSTR2 TEMPSTR3; do
      if [ -z "$TEMPSTR3" ]; then
         RWCOMLIST="${RWCOMLIST}$TEMPSTR2 Any_Client\\n" 
      else
         RWCOMLIST="${RWCOMLIST}$TEMPSTR2 $TEMPSTR3\\n" 
      fi
   done < $TMPPATH 
 
   # Get the Read Only Community string and system
   egrep -h '^[ 	]*rocommunity[ 	].*$' $SNMPDCONFUSED > $TMPPATH
   while read TEMPSTR1 TEMPSTR2 TEMPSTR3; do
      if [ -z "$TEMPSTR3" ]; then
         ROCOMLIST="${ROCOMLIST} $TEMPSTR2 anyclient\\n" 
      else
         ROCOMLIST="${ROCOMLIST}$TEMPSTR2 $TEMPSTR3\\n" 
      fi
   done < $TMPPATH 

   SNMPDTRAPDEFCOMM=`egrep -h '^[ 	]*trapcommunity.*$' $SNMPDCONFUSED | sed -e 's/$/\\\\n/' | tr -d '\n'`

   SNMPDTRAPDESTANDCOMM=`egrep -h '^[ 	]*trapsink.*$' $SNMPDCONFUSED | sed -e 's/$/\\\\n/' | tr -d '\n'`

   SYSCONTACT=`egrep -h '^[ 	]*syscontact' $SNMPDCONFUSED | sed -e 's/$/\\\\n/' | tr -d '\n'`

#   SNMPDXLOCATION=`egrep -h '^[	 ]*dlmod[	 ]*cmaX' $SNMPDCONFUSED | sed -e 's/$/\\\\n/' | tr -d '\n'`

   SYSLOCATION=`egrep -h '^[ 	]*syslocation' $SNMPDCONFUSED | sed -e 's/$/\\\\n/' |  tr -d '\n'`

   return 0
}

#####################################################################
# getSNMPAgentConfig
#####################################################################
getSNMPAgentConfig()
{
   AGENTS_EXCLUDED=""
   [ -f $CMACONFFILE ] && AGENTS_EXCLUDED="`egrep -h '^exclude.*$' $CMACONFFILE | sed -e 's/$/\\\\n/'`"
   [ -f $CMACONFFILE ] && NO_TAINTED_KERNEL="`egrep -h '^notaint.*$' $CMACONFFILE | sed -e 's/notaint[ 	]*//g'`"
}

#####################################################################
# getPreConfigSettings() 
# this function will try to find add the possible 
# pre configured setting and populate the appropriate
# EV so that the screens will be pre-populated
#####################################################################
getPreConfigSettings()
{
   # We have to load the cmaX extensions for the management stack
   # to work properly.  Find out if the current stack is suitable.
   # If it is not, the user has a little bit more to do for
   # everything to work.
   isSNMPSuitable

   if [ "$SNMP_SUITABLE" = "YES" ]; then
      # We need to populate a few EV's with the current
      # snmp setting so the user will see them
      getSNMPConfig

      getSNMPAgentConfig
   fi   

# DEBUG
echo SNMP_SUITABLE=$SNMP_SUITABLE
echo AGENTS_EXCLUDED=$AGENTS_EXCLUDED
echo NO_TAINTED_KERNEL=$NO_TAINTED_KERNEL
echo SNMPDLOCALWRITECOMMUSED=$SNMPDLOCALWRITECOMMUSED
echo SNMPDLOCALREADCOMMUSED=$SNMPDLOCALREADCOMMUSED
echo SNMPDNONLOCALWRITECOMMUSED=$SNMPDNONLOCALWRITECOMMUSED
echo SNMPDNONLOCALREADCOMMUSED=$SNMPDNONLOCALREADCOMMUSED
echo SNMPDTRAPDEFCOMM=$SNMPDTRAPDEFCOMM
echo SNMPDTRAPDESTANDCOMM=$SNMPDTRAPDESTANDCOMM
echo SNMPDSYSCONTACT=$SNMPDSYSCONTACT
echo SNMPDXLOCATION=$SNMPDXLOCATION
echo SNMPDLOCATION=$SNMPDLOCATION
echo SNMPDAGENTAEXCLUDE=$SNMPDAGENTAEXCLUDE

}
