#!/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.2 and 4.2.1.
# 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.
#######################################################################

vendorversion() {
  # $1 contains the ENV for Vendor
  # $2 contains the ENV for Version
  # Return 0 when succeed
  if [ ! -f /etc/issue ]; then
    echo "/etc/issue does not exist!" 1>&2
    return 1
  fi
  #echo "ARG1=$1"
  #echo "ARG2=$2"
  if [ -z "$1" -o -z "$2" ]; then
     return 2 
  fi
  grep "Red *Hat .* 6\.2 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="6.2"
    return 0 
  fi
  grep "Red *Hat .* 7\.1 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="7.1"
    return 0 
  fi
  egrep "Red *Hat .* 7\.2 |Red *Hat .* 7\.1\.[0-9]" /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="7.2"
    return 0 
  fi
  grep "SuSE .* 7\.0 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="SuSE"
    eval $2="7.0"
    return 0 
  fi
  grep "SuSE .* 7\.2 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="SuSE"
    eval $2="7.2"
    return 0 
  fi
  grep "SLES-7 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="SuSE"
    eval $2="SLES-7"
    return 0
  fi
  grep "Caldera .* 3\.1 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="Caldera"
    eval $2="3.1"
    return 0 
  fi
  return 9
}

ejectcd () {
  EJECTRETRIES=$1
  [ -z "$EJECTRETRIES" ] && EJECTRETRIES=3
  #echo "EJECTRETRIES=$EJECTRETRIES"
  EJECTRETURN=1
  while [ $EJECTRETRIES > 0 ]; do
    eject /dev/cdrom >/dev/null 2>&1
    df | egrep "/mnt/cdrom$|/media/cdrom$" >/dev/null 2>&1
    if [ $? != 0 ]; then
      EJECTRETURN=0
      break
    fi
    EJECTRETRIES=`expr $EJECTRETRIES - 1`
  done
  return $EJECTRETURN
}

USERNAME=`id -nu`
if [ "$USERNAME" != "root" ]; then
  echo "ERROR: This script can be executed by \"root\" only!"
  exit 1
fi

VENDOR="Unknown"
vendorversion "VENDOR" "VERSION"

if [ "$VENDOR" = "RedHat" ]; then
  PREFIX="/usr/src/redhat/BUILD/ucd-snmp"
  case "$VERSION" in
    7\.1)
       UCDSNMPNAME="ucd-snmp"
       SNMPRPMVER="4.2"
       SNMPSPECSDIR="/usr/src/redhat/SPECS"
       SNMPSPECFILE="ucd-snmp.spec"
       SNMPSRCDIR="/usr/src/redhat/SOURCES"
       SNMPSRCRPMFILE="/mnt/cdrom/SRPMS/ucd-snmp-4.2-*.src.rpm"
       SNMPSRCRPMCD="Red Hat Linux 7.1 CD #2"
       SNMPSRCTARFILE="ucd-snmp-4.2.tar.gz"
       SNMPSRCTARDIR="ucd-snmp-4.2"
       SNMPSRCPATCHFILES="ucd-snmp-4.1.2-bug18153.patch ucd-snmp-4.2-fromcvs.patch ucd-snmp-4.2-null.patch ucd-snmp-4.2-rpm40.patch"
      ;;
    7\.2)
       UCDSNMPNAME="ucd-snmp"
       SNMPRPMVER="4.2.1"
       SNMPSPECSDIR="/usr/src/redhat/SPECS"
       SNMPSPECFILE="ucd-snmp.spec"
       SNMPSRCDIR="/usr/src/redhat/SOURCES"
       SNMPSRCRPMFILE="/mnt/cdrom/SRPMS/ucd-snmp-4.2.1-*.src.rpm"
       SNMPSRCRPMCD="Red Hat Linux 7.2 Professional CD #4"
       SNMPSRCTARFILE="ucd-snmp-4.2.1.tar.gz"
       SNMPSRCTARDIR="ucd-snmp-4.2.1"
       SNMPSRCPATCHFILES="ucd-snmp-4.2-null.patch ucd-snmp-4.2-rpm40.patch ucd-snmp-4.2-smux.patch ucd-snmp-4.2-ia64.patch ucd-snmp-4.2.1-manpage.patch"
      ;;
    *)
      echo "ERROR: Only Red Hat 7.1 and 7.2 Professional are supported!"
      echo "       You're running Red Hat version $VERSION"
      exit 1
      ;;
  esac
elif [ "$VENDOR" = "SuSE" ]; then
  PREFIX="/usr/src/packages/BUILD/ucd-snmp"
  if [ "$VERSION" = "SLES-7" ]; then
    type autoconf >/dev/null 2>&1
    if [ "$?" != "0" ]; then
      echo "ERROR: \"autoconf\" RPM is required for rebuilding snmpd!"
      echo "       Install \"autoconf\" RPM from CD #1 and rerun this script!"
      exit 1
    fi
  fi
  case "$VERSION" in
    SLES-7)
      UCDSNMPNAME="ucdsnmp"
      SNMPRPMVER="4.2.1"
      SNMPSPECSDIR="/usr/src/packages/SPECS"
      SNMPSPECFILE="ucdsnmp.spec"
      SNMPSRCDIR="/usr/src/packages/SOURCES"
      SNMPSRCRPMFILE="/cdrom/full-names/src/ucdsnmp-4.2.1-*.src.rpm"
      SNMPSRCRPMCD="SuSE SLES-7 CD #2"
      SNMPSRCTARFILE="ucd-snmp-4.2.1.tar.bz2"
      SNMPSRCTARDIR="ucd-snmp-4.2.1"
      SNMPSRCPATCHFILES="ucd-snmp-4.2.1-bufferoverflow.dif ucd-snmp-4.2.1-destdir.dif ucd-snmp-4.2.1-linux.dif ucd-snmp-4.2.1-security.dif ucd-snmp-4.2.1-security2.dif ucd-snmp-4.2.1-security3.dif"
      ;;
    *)
      echo "ERROR: Only SuSE Linux Enterprise Server 7 is supported!"
      echo "       You're running SuSE version $VERSION"
      exit 1
      ;;
  esac
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

# Make sure the binary UCD SNMP RPM is installed
SNMPBINVER=`rpm -q $UCDSNMPNAME 2>/dev/null | sed 's/^'"$UCDSNMPNAME"'-//; s/\([^-]*-[^-\.]*\).*$/\1/'`
if [ -z "$SNMPBINVER" ]; then
  echo "ERROR: the binary \"$UCDSNMPNAME\" RPM is NOT installed!"
  exit 1
fi 

# Make sure Foundation agents RPM is installed
rpm -q cmafdtn >/dev/null 2>&1
if [ $? != 0 ]; then
  echo "ERROR: Foundation Agents RPM (cmafdtn) is NOT installed!"
  exit 1
fi

# Make sure /usr/sbin/snmpd and /opt/compaq/foundation/src/cmaX.c exist
if [ ! -f "/usr/sbin/snmpd" ]; then
  echo "ERROR: /usr/sbin/snmpd does not exist!"
  exit 1
fi 

if [ ! -f "/opt/compaq/foundation/src/cmaX.c" ]; then
  echo "ERROR: /opt/compaq/foundation/src/cmaX.c does not exist!"
  exit 1
fi 

IDINCMAXSRC=""
IDINSNMPDEXE=""
IDINCMAXSRC=`grep 'Compaq .*eXtension .*(cmaX) .*Version' /opt/compaq/foundation/src/cmaX.c | sed 's/^[^"]*"//; s/".*$//'`
IDINSNMPDEXE=`strings /usr/sbin/snmpd | grep 'Compaq .*eXtension .*(cmaX) .*Version'`

if [ "$IDINSNMPDEXE" = "$IDINCMAXSRC" ]; then
  echo
  echo "Compaq Management Agents eXtension (cmaX) in \"/usr/sbin/snmpd\" is Up-To-Date!" 
  echo "There's no need to rebuild /usr/sbin/snmpd! Exiting ..."
  exit 0
fi

if [ ! -z "$SNMPDBLDPRINTNOTES" ]; then
  cat <<EOF

============================== IMPORTANT NOTES ============================
"gcc" (from gcc RPM), UCD SNMP source RPM and Compaq Foundation Agents RPM
(cmafdtn) are required for rebuilding "snmpd" with Compaq Management Agents
eXtension (cmaX).
 
SNMP source RPM ($SNMPSRCRPMFILE) can
be installed from $SNMPSRCRPMCD.
 
This tool supports "cmaX" extension only. If you have multiple SNMP extensions,
follow the instructions for "./configure" utility in source RPM's README file.
 
"ucd-snmp" version "$SNMPBINVER" is currently installed on your system.
===========================================================================

EOF
  echo -n "Press Enter to continue ... "
  read ANS
fi

while true; do
  # Check if UCD SNMP source RPM is installed
  SRCRPMINSTALLED="yes"
  if [ ! -f $SNMPSRCDIR/$SNMPSRCTARFILE ]; then
    SRCRPMINSTALLED="no"
  fi
  for i in $SNMPSRCPATCHFILES; do
    [ -z "$i" ] && continue
    if [ ! -f $SNMPSRCDIR/$i ]; then
      SRCRPMINSTALLED="no"
    fi
  done
  if [ ! -f $SNMPSPECSDIR/$SNMPSPECFILE ]; then
    SRCRPMINSTALLED="no"
  fi

  if [ $SRCRPMINSTALLED = "yes" ]; then
    echo "UCD SNMP source RPM already installed on this system!"
    sleep 2
    break
  fi

  # Install from CD if it's not installed
  echo "WARNING: UCD SNMP Source RPM is NOT installed on this system!"
  while true; do
    while true; do
      echo "Please insert $SNMPSRCRPMCD into CDROM drive!"
      echo -n "Press Enter to continue ..."
      read ANS
      mount /dev/cdrom
      # RH: /mnt/cdrom.  SLES-7: /media/cdrom
      df | egrep "/mnt/cdrom$|/media/cdrom$" >/dev/null 2>&1
      [ $? = 0 ] && break;
      echo "WARNING: Failed to mount $SNMPSRCRPMCD! Aborting ..."
      sleep 1
    done
    # Find SRC RPM
    SNMPSRCRPMNAME=`ls $SNMPSRCRPMFILE 2>/dev/null`
    if [ -z "$SNMPSRCRPMNAME" ]; then
      echo "WARNING: Failed to locate $SNMPSRCRPMFILE! Ejecting CD ..."
      #eject /dev/cdrom >/dev/null 2>&1
      ejectcd 2
      continue;
    fi
    SNMPSRCRPMVER=`basename $SNMPSRCRPMNAME | sed 's/^.*snmp-//; s/\([^-]*-[^-\.]*\).*$/\1/'`
    if [ "$SNMPSRCRPMVER" != "$SNMPBINVER" ]; then
      echo "WARNING: SNMP source RPM version \"$SNMPSRCRPMVER\" is not the same as binary RPM version \"$SNMPBINVER\"! Ejecting CD ..."
      #eject /dev/cdrom >/dev/null 2>&1
      ejectcd 2
      continue
    fi
    break;
  done

  #Install SRC RPM
  echo
  echo -n "Installing $SNMPSRCRPMNAME ..."
  rpm -ivh $SNMPSRCRPMNAME
  if [ $? = 0 ]; then
    echo "Done."
    sleep 2
  else
    echo "Failed!"
    #eject /dev/cdrom >/dev/null 2>&1
    ejectcd 2
    exit 1
  fi
  echo -n "Trying to eject $SNMPSRCRPMCD ..."
  #eject /dev/cdrom >/dev/null 2>&1
  ejectcd
  if [ $? = 0 ]; then
    echo "Done."
  else
    echo "Failed!"
  fi
done  #while

# Correct UCD SNMP source rpm is installed. Prepare the Source ...
echo -n "Preparing the source files and patches ..."
sleep 1
rpm -bp $SNMPSPECSDIR/$SNMPSPECFILE
if [ $? = 0 ]; then
  echo "Done."
else
  echo "Failed!"
  exit 1
fi


SNMPVER=
if [ -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.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 <<EOF





EOF
else
  ./configure --disable-shared --with-mib-modules=cmaX --with-ldflags="-lpthread" --prefix=/usr <<EOF





EOF
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 >/dev/null 2>&1
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
