#!/bin/sh

# this shell script is designed to merely dump the configuration
# information about how the net-snmp package was compiled.  The
# information is particularily useful for applications that need to
# link against the net-snmp libraries and hence must know about any
# other libraries that must be linked in as well.

NSC_BASE_AGENT_LIBS="-lnetsnmpagent -lnetsnmpmibs -lnetsnmphelpers -lnetsnmp"

if test "x$1" = "x"; then
  usage="yes"
else
  exec_prefix=/usr
  prefix=/usr
  case $1 in
    --debug-tokens)
      echo "find NET-SNMP-SOURCE-DIR -name \"*.c\" -print | xargs grep DEBUGMSGT | grep \\\" | cut -f 2 -d\\\" | sort -u"
      ;;
    --indent-options)
      echo "indent -orig -nbc -bap -nut -nfca `(cd NET-SNMP-INCLUDE-DIR; perl -n -e 'print "-T $1 " if (/}\s*(netsnmp_\w+)\s*;/);' */*.h)`"
      ;;
    --configure-options)
      echo " --with-cc=/curly/build/frv-050408/1/frv-linux-gnu/rootfs/build_linux-gnu_frv/frv-050408-1/H-i686-pc-linux-gnulibc2.2/bin/frv-linux-gnu-gcc --with-ar=/curly/build/frv-050408/1/frv-linux-gnu/rootfs/build_linux-gnu_frv/frv-050408-1/H-i686-pc-linux-gnulibc2.2/bin/frv-linux-gnu-ar --target=frv-unknown-linux-gnu --host=frv-unknown-linux-gnu --build=i386-pc-linux-gnu --with-endianness=big --with-persistent-directory=/var/lib/snmp --enable-ucd-snmp-compatibility --enable-shared --with-logfile=none --without-rpm --with-openssl --without-dmalloc --without-efence --without-rsaref --with-sys-contact=root --with-sys-location=Unknown '--with-mib-modules=host smux ucd-snmp/dlmod' --with-defaults --prefix=/usr --sysconfdir=/etc --mandir=/usr/man --infodir=/usr/info --enable-ipv6"
      ;;
    --snmpd-module-list)
      echo  ucd-snmp/dlmod mibII/ipv6 mibII/system_mib mibII/sysORTable mibII/at mibII/interfaces mibII/snmp_mib mibII/tcp mibII/icmp mibII/ip mibII/udp mibII/vacm_vars mibII/setSerialNo ucd-snmp/memory ucd-snmp/vmstat ucd-snmp/proc ucd-snmp/versioninfo ucd-snmp/pass ucd-snmp/pass_persist ucd-snmp/disk ucd-snmp/loadave ucd-snmp/extensible ucd-snmp/errormib ucd-snmp/file ucd-snmp/proxy snmpv3/snmpEngine snmpv3/snmpMPDStats snmpv3/usmStats snmpv3/usmUser notification/snmpNotifyTable notification/snmpNotifyFilterTable notification/snmpNotifyFilterProfileTable target/snmpTargetAddrEntry target/snmpTargetParamsEntry target/target target/target_counters agent/nsTransactionTable agent/nsModuleTable agentx/master agentx/subagent utilities/override host/hr_system host/hr_storage host/hr_device host/hr_other host/hr_proc host/hr_network host/hr_print host/hr_disk host/hr_partition host/hr_filesys host/hr_swrun host/hr_swinst smux/smux mibII/var_route mibII/route_write util_funcs mibII/kernel_linux mibII/tcpTable mibII/ipAddr mibII/udpTable mibII/vacm_context header_complex agentx/protocol agentx/client agentx/master_admin agentx/agentx_config
      ;;
    --base-cflags)
      echo -DINET6  -Os    -Dlinux  -I${prefix}/include
      ;;
    --cflags)
      echo -DINET6  -Os    -Dlinux   -I. -I${prefix}/include
      ;;
    --agent-libs)
      # use this one == --netsnmp-agent-libs + --external-libs
      echo  $NSC_BASE_AGENT_LIBS -ldl -lz -lcrypto -lm  
      ;;
    --libs)
      # use this one == --netsnmp-libs + --external-libs
      echo  -lnetsnmp -lcrypto -lm  
      ;;
    --external-libs)
      echo  -lcrypto -lm  
      ;;
    --external-agent-libs)
      echo  -ldl -lz -lcrypto -lm  
      ;;
    --netsnmp-libs)
      echo -L${exec_prefix}/lib -lnetsnmp
      ;;
    --netsnmp-agent-libs)
      echo -L${exec_prefix}/lib $NSC_BASE_AGENT_LIBS
      ;;
    --version)
      echo 5.0.7
      ;;
    --help)
      usage="yes"
      ;;
    --prefix)
      echo /usr
      ;;
    --exec-prefix)
      echo /usr
      ;;
    --create-snmpv3-user)
      if /bin/ps -e | egrep ' snmpd *$' > /dev/null 2>&1 ; then
         echo "Apparently at least one snmpd demon is already running."
         echo "You must stop them in order to use this command."
         exit 1
      fi

      Aalgorithm="MD5"
      Xalgorithm="DES"
      token=rwuser
      shift
      arg=$1
      shift
      while test "x$done" = "x" -a "x$arg" != "x" ; do
	case $arg in
	    -A)
		Aalgorithm=$1
		shift
		;;
	    -X)
		Xalgorithm=$1
		shift
		;;
	    -a)
		apassphrase=$1
		shift
		;;
	    -x)
		xpassphrase=$1
		shift
		;;
	    -ro)
	        token="rouser"
		;;
 	    -*)
		echo "unknown suboption to --create-snmpv3-user: $arg"
		exit 1
		;;
	    *)
	        done=1
		;;
	esac
	if test "x$done" = "x" ; then
          arg=$1
          shift
	fi
      done
      
      user=$arg
      if test "x$user" = "x" ; then
          prompt=yes
	  echo "Enter a SNMPv3 user name to create: "
	  read user
      fi
      if test "x$user" = "x" ; then
          echo "You must specify a user name"
	  exit 1
      fi

      apassphrase=$1
      if test "x$apassphrase" = "x" ; then
          prompt=yes
	  echo "Enter authentication pass-phrase: "
	  read apassphrase
      else
	  shift
      fi
      if test "x$apassphrase" = "x" ; then
          echo "You must specify an authentication pass-phrase"
	  exit 1
      fi

      if test "x$prompt" = "xyes" -a "x$xpassphrase" = "x" ; then
	  echo "Enter encryption pass-phrase: "
	  echo "  [press return to reuse the authentication pass-phrase]"
	  read xpassphrase
      fi

      outfile="/var/lib/snmp/snmpd.conf"
      line="createUser $user $Aalgorithm \"$apassphrase\" $Xalgorithm $xpassphrase"
      echo "adding the following line to $outfile:"
      echo "  " $line
      echo $line >> $outfile

      outfile="${prefix}/share/snmp/snmpd.conf"
      line="$token $user"
      echo "adding the following line to $outfile:"
      echo "  " $line
      echo $line >> $outfile
      ;;

    --compile-subagent)
      shift
      outname=$1
      shift
      if test $1 = "--norm" ; then
        norm=1
	shift
      fi
      if test $1 = "--cflags" ; then
        shift
	cflags=$1
	echo "setting extra cflags: $cflags"
	shift
      fi
      if test $1 = "--ldflags" ; then
        shift
	ldflags=$1
	echo "setting extra ldflags: $ldflags"
	shift
      fi
      tmpfile=netsnmptmp.$$.c
      if test -f $tmpfile; then
	echo "Ack.  Can't create $tmpfile: already exists"
	exit 1
      fi
      echo "generating the tmporary code file: $tmpfile"
      rm -f $tmp
      cat > $tmpfile <<EOF
/* generated from net-snmp-config */
#include <net-snmp/net-snmp-config.h>
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif /* HAVE_SIGNAL_H */
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
EOF

    # add include files
    while test "$1" != ""; do
      cfiles="$cfiles $1"
      name=`basename $1 | sed 's/\.[co]$/.h/'`
      if test -f $name.h; then
        if grep "init_$name" $name; then
          echo "  #include\"$name\"" >> $tmpfile
        fi
      fi
      shift
    done

      cat >> $tmpfile <<EOF
static int keep_running;

RETSIGTYPE
stop_server(int a) {
    keep_running = 0;
}

int
main (int argc, char **argv) {
  /* print log errors to stderr */
  snmp_enable_stderrlog();
 /* we are a subagent */
  netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);

  /* initialize the agent library */
  init_agent("$outname");

  /* initialize your mib code here */
EOF

    # add init routines
    for i in $cfiles ; do
      name=`basename $i | sed 's/\.[co]$//'`
      echo checking for init_$name in $i
      if grep "init_$name" $i ; then
        echo "  init_${name}();" >> $tmpfile
      fi
      shift
    done

    # finish file
    cat >> $tmpfile <<EOF

  /* ustMain will be used to read ustMain.conf files. */
  init_snmp("$outname");

  /* In case we recevie a request to stop (kill -TERM or kill -INT) */
  keep_running = 1;
#ifdef SIGTERM
  signal(SIGTERM, stop_server);
#endif
#ifdef SIGINT
  signal(SIGINT, stop_server);
#endif

  /* main loop here... */
  while(keep_running) {
    agent_check_and_process(1);
  }

  /* at shutdown time */
  snmp_shutdown("$outname");
  exit(0);
}

EOF
      if test "$?" != 0 -o ! -f "$tmpfile" ; then
        echo "Ack.  Can't create $tmpfile."
	exit 1
      fi
      prefix=/usr
      cmd="/curly/build/frv-050408/1/frv-linux-gnu/rootfs/build_linux-gnu_frv/frv-050408-1/H-i686-pc-linux-gnulibc2.2/bin/frv-linux-gnu-gcc $cflags -DINET6  -Os    -Dlinux  -I. -I${prefix}/include -o $outname $tmpfile $cfiles  -L/usr/lib -lnetsnmpagent -lnetsnmphelpers -lnetsnmpmibs -lnetsnmp -ldl -lz -lcrypto -lm   $ldflags"
      echo "running: $cmd"
      `$cmd`
      if test "x$norm" != "x1" ; then
        echo "removing the tmporary code file: $tmpfile"
        rm -f $tmpfile
      else
        echo "leaving the tmporary code file: $tmpfile"
      fi
      if test -f $outname ; then
        echo "subagent program $outname created"
      fi
      ;;

    *)
      echo "unknown option $1"
      usage="yes"
      ;;
  esac
fi

if test "x$usage" = "xyes"; then
  echo ""
  echo "Usage:"
  echo "  net-snmp-config [--cflags] [--agent-libs] [--libs] [--version]"
  echo "                  ... [see below for complete flag list]"
  echo ""
  echo "    --version         displays the net-snmp version number"
  echo "    --indent-options  displays the indent options from the Coding Style"
  echo "    --debug-tokens    displays a example command line to search to source"
  echo "                      code for a list of available debug tokens"
  echo ""
  echo "  SNMP Setup commands:"
  echo ""
  echo "    --create-snmpv3-user [-ro] [-a authpass] [-x privpass] [-X DES]"
  echo "                         [-A MD5|SHA] [username]"
  echo ""
  echo "  These options produce the various compilation flags needed when"
  echo "  building external SNMP applications:"
  echo ""
  echo "    --base-cflags     lists additional compilation flags needed"
  echo "                      for external applications (excludes -I. and"
  echo "                      extra developer warning flags, if any)"
  echo "    --cflags          lists additional compilation flags needed"
  echo "    --libs            lists libraries needed for building applications"
  echo "    --agent-libs      lists libraries needed for building subagents"
  echo ""
  echo "    --netsnmp-libs        lists netsnmp specific libraries"
  echo "    --external-libs       lists libraries needed by netsnmp libs"
  echo "    --netsnmp-agent-libs  lists netsnmp specific agent libraries"
  echo "    --external-agent-libs lists libraries needed by netsnmp libs"
  echo ""
  echo "  Automatted subagent building (produces an OUTPUTNAME binary file):"
  echo "  [this feature has not been tested very well yet.  use at your risk.]"
  echo ""
  echo "    --compile-subagent OUTPUTNAME [--norm] [--cflags flags]"
  echo "                                  [--ldflags flags] mibmodule1.c [...]]"
  echo ""
  echo "         --norm           leave the generated .c file around to read."
  echo "         --cflags flags   extra cflags to use (e.g. -I...)."
  echo "         --ldflags flags  extra ld flags to use (e.g. -L... -l...)."
  echo ""
  echo "  Details on how the net-nsmp package was compiled:"
  echo ""
  echo "    --configure-options   Display original configure arguments"
  echo "    --snmpd-module-list   Display the modules compiled into the agent"
  echo "    --prefix              Display the installation prefix"
  echo ""
  exit
fi  
