#!/bin/bash

#>/tmp/cmdline

if test -e /etc/snmp/snmpd.conf && grep -q '^#LOCKED$' /etc/snmp/snmpd.conf; then
    echo "switchsnmp: /etc/snmp/snmpd.conf is intentionally locked"
    exit
fi

getval () {

    grep "string_${1}" /data/config.lua|sed -e 's/.*= "\(.*\)".*$/\1/' -e 's/\\//g'

}


# Setup SNMP

SNMPLOCATION=$(getval SnmpLocation)
SNMPCONTACT=$(getval SnmpContact)
SNMPROC=$(getval SnmpRoc)
SNMPRWC=$(getval SnmpRwc)
SNMPTRAPSINK=$(getval SnmpTrapsink)

if [ -z "${SNMPROC}" ] ; then
    SNMPROC="public"
fi

if [ -z "${SNMPRWC}" ] ; then
    SNMPRWC="Seife.ist.selten.schamckhaft"
fi

if [ -z "${SNMPLOCATION}" ] ; then
    SNMPLOCATION="OfficeMasterGate"
fi

if [ -z "${SNMPCONTACT}" ] ; then
    SNMPCONTACT="OMG Admin"
fi

if [ -z "${SNMPTRAPSINK}" ] ; then
    SNMPTRAPSINK=127.0.0.1
fi

if grep -q "^export SNMPROC=" /tmp/cmdline;then
    sed -i -e "/^export SNMPROC=/cexport SNMPROC=\"${SNMPROC}\"" /tmp/cmdline
else
    echo "export SNMPROC=\"${SNMPROC}\"" >> /tmp/cmdline
fi

if grep -q "^export SNMPRWC=" /tmp/cmdline;then
    sed -i -e "/^export SNMPRWC=/cexport SNMPRWC=\"${SNMPRWC}\"" /tmp/cmdline
else
    echo "export SNMPRWC=\"${SNMPRWC}\"" >> /tmp/cmdline
fi

if grep -q "^export SNMPLOCATION=" /tmp/cmdline;then
    sed -i -e "/^export SNMPLOCATION=/cexport SNMPLOCATION=\"${SNMPLOCATION}\"" /tmp/cmdline
else
    echo "export SNMPLOCATION=\"${SNMPLOCATION}\"" >> /tmp/cmdline
fi

if grep -q "^export SNMPCONTACT=" /tmp/cmdline;then
sed -i -e "/^export SNMPCONTACT=/cexport SNMPCONTACT=\"${SNMPCONTACT}\"" /tmp/cmdline
else
echo "export SNMPCONTACT=\"${SNMPCONTACT}\"" >> /tmp/cmdline
fi
if grep -q "^export SNMPTRAPSINK=" /tmp/cmdline;then
sed -i -e "/^export SNMPTRAPSINK=/cexport SNMPTRAPSINK=\"${SNMPTRAPSINK}\"" /tmp/cmdline
else
echo "export SNMPTRAPSINK=\"${SNMPTRAPSINK}\"" >> /tmp/cmdline
fi

mkdir -p /etc/snmp
cat > /etc/snmp/snmpd.conf <<EOF

# Generated by switchsnmp
master agentx
iquerySecName _internal
EOF
test ! -z "${SNMPRWC}"  && echo "rwcommunity ${SNMPRWC}" >>/etc/snmp/snmpd.conf
test ! -z "${SNMPRWC}"  && echo "rwcommunity6 ${SNMPRWC}" >>/etc/snmp/snmpd.conf
test ! -z "${SNMPROC}"  && echo "rocommunity ${SNMPROC}" >>/etc/snmp/snmpd.conf
test ! -z "${SNMPROC}"  && echo "rocommunity6 ${SNMPROC}" >>/etc/snmp/snmpd.conf
test ! -z "${SNMPCONTACT}"  && echo "syscontact ${SNMPCONTACT}" >>/etc/snmp/snmpd.conf
test ! -z "${SNMPTRAPSINK}"  && echo "trapsink ${SNMPTRAPSINK}" >>/etc/snmp/snmpd.conf
test ! -z "${SNMPTRAPSINK}"  && echo "trap2sink ${SNMPTRAPSINK}" >>/etc/snmp/snmpd.conf
test ! -z "${SNMPLOCATION}" && echo "syslocation ${SNMPLOCATION}" >>/etc/snmp/snmpd.conf

# if hp-health is installed we need this 
producer=$(dmidecode -s system-manufacturer)
if [  "${producer}" = "HP" -a -e /usr/lib/libcmaX.so ]; then
    echo "dlmod cmaX /usr/lib/libcmaX.so" >>/etc/snmp/snmpd.conf
fi

for i in ${SNMPTRAPSINK};do
echo "trapsink ${i}" >>/etc/snmp/snmpd.conf
echo "trap2sink ${i}" >>/etc/snmp/snmpd.conf
done

/etc/init.d/snmpd restart
exit 0
