#!/bin/sh
#######################################################################
# This script helps you rebuild UCD SNMP agent (snmpd) to include
# Compaq Management Agents eXtension (cmaX).
# You must first install the UCD SNMP souce RPM, un-tar the source and
# apply the patches, if any. Read README file for the instructions
# on installing source RPM.
# Supported UCD SNMP versions are 4.1.1 and 4.1.2.
# Note: this script add cmaX ONLY. If you need other SNMP extensions,
#       this script can be modified to include other extensions.
#######################################################################
PREFIX="/usr/src/redhat/SOURCES/ucd-snmp"
SNMPVER=
if [ -d ${PREFIX}-4.1.2 ]; then
  SNMPVER=4.1.2
elif [ -d ${PREFIX}-4.1.1 ]; then
  SNMPVER=4.1.1
fi

if [ -z "$SNMPVER" ]; then
  echo
  echo "ERROR: Following directories do not exist on your system:"
  echo "   ${PREFIX}-4.1.1"
  echo "   ${PREFIX}-4.1.2"
  exit 1
fi

echo
echo "UCD SNMP source version $SNMPVER will be used!"

SRCROOT="/usr/src/redhat/SOURCES/ucd-snmp-$SNMPVER"
cd $SRCROOT

if [ $? != 0 ]; then
  echo
  echo "ERROR: \"cd $SRCROOT\" failed!"
  exit 1
fi

if [ ! -f ./configure ]; then
  echo
  echo "ERROR: \"$SRCROOT/configure\" script does not exist!"
  exit 1
fi

ERROR=0
for i in $SRCROOT/configure $SRCROOT/agent/mibgroup/mibII/interfaces.c \
         /opt/compaq/foundation/src/interfaces.c.$SNMPVER \
         /opt/compaq/foundation/src/cmaX.c \
         /opt/compaq/foundation/src/cmaX.h \
         /etc/rc.d/init.d/snmpd \
         /usr/sbin/snmpd; \
         do
  if [ ! -f "$i" ]; then
    echo
    echo "ERROR: \"$i\" does not exist!"
    ERROR=1
  fi
done
if [ "$ERROR" = "1" ]; then
  exit 1
fi

echo
echo "Ready to rebuild snmpd, source directory is `pwd`"
echo -n "Press Enter to continue ... "
read ANS

mv ./agent/mibgroup/mibII/interfaces.c ./agent/mibgroup/mibII/interfaces.c.orig
cp /opt/compaq/foundation/src/interfaces.c.$SNMPVER ./agent/mibgroup/mibII/interfaces.c
cp /opt/compaq/foundation/src/cmaX.[ch] ./agent/mibgroup
./configure --with-mib-modules=cmaX --with-ldflags="-lpthread" --prefix=/usr
make

if [ ! -f ./agent/snmpd ]; then
  echo
  echo "ERROR: rebuild \"$SRCROOT/agent/snmpd\" failed!"
  echo "ERROR: Please rerun this script and redirtect output to a file using command: "
  echo "ERROR:    % snmpdbld >snmpdbld.out 2>&1"
  exit 1
fi

strings ./agent/snmpd | grep "Compaq Management Agents eXtension" >/dev/null 2>&1
if [ "$?" != 0 ]; then
  echo
  echo "ERROR: rebuild \"$SRCROOT/agent/snmpd\" failed!"
  echo "ERROR: new snmpd does not contain cmaX!"
  echo "ERROR: Please rerun this script and redirtect output to a file using command: "
  echo "ERROR:    % snmpdbld >snmpdbld.out 2>&1"
fi

echo
echo "\"snmpd\" had been successfully rebuilt!"

while true; do
  echo
  echo -n "Do you want to replace /usr/sbin/snmpd with new snmpd (y/n)? "
  read ANS
  case "$ANS" in
    y|Y)
       break
       ;;
    n|N)
       exit 1
       ;;
  esac
done

echo
echo -n "Stopping running snmpd ..."
/etc/rc.d/init.d/snmpd stop
echo done
echo
echo -n "Saving /usr/sbin/snmpd to /usr/sbin/snmpd.orig ..."
mv /usr/sbin/snmpd /usr/sbin/snmpd.orig
echo done
echo
echo -n "Installing new snmpd as /usr/sbin/snmpd ..."
cp ./agent/snmpd /usr/sbin/snmpd
echo done
echo
echo -n "Starting new snmpd ..."
/etc/rc.d/init.d/snmpd start 
echo done

exit 0
