#!/bin/sh
# Copyright 2000-2001, Compaq Computer Corporation.  All rights reserved.
#
#######################################################################
# 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 adds Compaq Management Agents eXtension (cmaX) ONLY.
#       If you need other SNMP extensions, this script can be modified
#       to include other extensions.
#######################################################################
USERNAME=`id -nu`
if [ "$USERNAME" != "root" ]; then
  echo "ERROR: This script can be executed by \"root\" only!"
  exit 1
fi

if [ -f /etc/redhat-release ]; then
  PREFIX="/usr/src/redhat/SOURCES/ucd-snmp"
elif [ -f /etc/SuSE-release ]; then
  PREFIX="/usr/src/packages/SOURCES/ucd-snmp"
else
  echo "ERROR: the Linux OS running on your system is not supported!"
  exit 1
fi 

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

SNMPVER=
if [ -d ${PREFIX}-4.1.2 ]; then
  SNMPVER=4.1.2
elif [ -d ${PREFIX}-4.1.1 ]; then
  SNMPVER=4.1.1
elif [ -d ${PREFIX}-4.2 ]; then
  SNMPVER=4.2
elif [ -d ${PREFIX}-4.2.1 ]; then
  SNMPVER=4.2.1
fi

if [ -z "$SNMPVER" ]; then
  echo
  echo "ERROR: None of following directories exists on your system:"
  echo "   ${PREFIX}-4.1.1"
  echo "   ${PREFIX}-4.1.2"
  echo "   ${PREFIX}-4.2"
  echo "   ${PREFIX}-4.2.1"
  exit 1
fi

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

SRCROOT="${PREFIX}-$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
if [ ! $SNMPVER = "4.2" -a ! $SNMPVER = "4.2.1" ]; then
  for i in $SRCROOT/configure $SRCROOT/agent/mibgroup/mibII/interfaces.c \
           /opt/compaq/foundation/src/interfaces.c.$SNMPVER; \
           do
    if [ ! -f "$i" ]; then
      echo
      echo "ERROR: \"$i\" does not exist!"
      ERROR=1
    fi
  done
fi

for i in /opt/compaq/foundation/src/cmaX.c \
         /opt/compaq/foundation/src/cmaX.h \
         $RCPREFIX/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

if [ ! $SNMPVER = "4.2" -a ! $SNMPVER = "4.2.1" ]; then
  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
fi

cp /opt/compaq/foundation/src/cmaX.[ch] ./agent/mibgroup
if [ ! $SNMPVER = "4.2" -a ! $SNMPVER = "4.2.1" ]; then
  ./configure --with-mib-modules=cmaX --with-ldflags="-lpthread" --prefix=/usr
else
  ./configure --disable-shared --with-mib-modules=cmaX --with-ldflags="-lpthread" --prefix=/usr
fi
make

if [ ! $SNMPVER = "4.2" -a ! $SNMPVER = "4.2.1" ]; then
  NEWSNMPDEXE="agent/snmpd"
else
  NEWSNMPDEXE="agent/snmpd"
fi

if [ ! -f $NEWSNMPDEXE ]; then
  echo
  echo "ERROR: rebuild \"$SRCROOT/$NEWSNMPDEXE\" 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 $NEWSNMPDEXE | grep "Compaq Management Agents eXtension" >/dev/null 2>&1
if [ "$?" != 0 ]; then
  echo
  echo "ERROR: rebuild \"$SRCROOT/$NEWSNMPDEXE\" 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"
  exit 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 ..."
$RCPREFIX/snmpd stop
echo done
if [ ! -f /usr/sbin/snmpd.orig.cma ]; then
  echo
  echo -n "Saving /usr/sbin/snmpd to /usr/sbin/snmpd.orig.cma ..."
  mv /usr/sbin/snmpd /usr/sbin/snmpd.orig.cma
  echo done
fi
echo
echo -n "Installing new snmpd as /usr/sbin/snmpd ..."
cp $NEWSNMPDEXE /usr/sbin/snmpd
echo done
echo
echo -n "Starting new snmpd ..."
$RCPREFIX/snmpd start 
echo done

exit 0
