#----------------------------------------------------------------------------
# /etc/rc.d/rc860.snmp - starting snmp daemon
# Creation:        2006-09-22  gk (Gero Kuhlmann)
# Last Update:     2006-09-22  gk
#----------------------------------------------------------------------------


if [ "$OPT_NETSNMP" = "yes" ]
then
    begin_script netsnmp "starting SNMP daemon ..."

    # Generate SNMP runtime directory
    if [ ! -d /var/run/snmp ]
    then
	mkdir -p /var/run/snmp
    fi

    # Generate SNMP configuration directory
    if [ ! -d /etc/snmp ]
    then
	mkdir -p /etc/snmp
    fi

    # Check if we have predefined configuration files
    for i in snmp.conf snmpd.conf snmptrapd.conf
    do
	if [ -r /etc/$i ]
	then
	    mv /etc/$i /etc/snmp/$i
	fi
    done

    # Check if we have a predefined password files
    for i in snmpd snmptrapd
    do
	if [ -r /etc/$i.var.conf ]
	then
	    cp /etc/$i.var.conf /var/run/snmp/$i.conf
	    rm -f /etc/$i.var.conf
	fi
    done

    # Check if we accept v1/2c community names
    if [ -n "$SNMP_ROUSER" -a -n "$SNMP_ROCOMMUNITY" ]
    then
	if [ -n "$SNMP_ROUSER_PASSWORD" -o \
	        "$SNMP_ROCOMMUNITY" = "$SNMP_ROUSER" ]
	then
	    SNMP_ROCOMMUNITY=""
	fi
    fi
    if [ -n "$SNMP_RWUSER" -a -n "$SNMP_RWCOMMUNITY" ]
    then
	if [ -n "$SNMP_RWUSER_PASSWORD" -o \
	        "$SNMP_RWCOMMUNITY" = "$SNMP_RWUSER" ]
	then
	    SNMP_RWCOMMUNITY=""
	fi
    fi

    # Generate SNMP daemon configuration file
    if [ ! -r /etc/snmp/snmpd.conf ]
    then
	>/etc/snmp/snmpd.conf

	if [ -n "$SNMP_RWUSER" ]
	then
	    if [ -n "$SNMP_RWUSER_PASSWORD" ]
	    then
		SNMP_RWAUTH="auth"
	    else
		SNMP_RWAUTH="noauth"
	    fi
	    echo "rwuser $SNMP_RWUSER $SNMP_RWAUTH" >>/etc/snmp/snmpd.conf
	fi

	if [ -n "$SNMP_ROUSER" ]
	then
	    if [ -n "$SNMP_ROUSER_PASSWORD" ]
	    then
		SNMP_ROAUTH="auth"
	    else
		SNMP_ROAUTH="noauth"
	    fi
	    echo "rouser $SNMP_ROUSER $SNMP_ROAUTH" >>/etc/snmp/snmpd.conf
	fi

	if [ -n "$SNMP_RWCOMMUNITY" ]
	then
	    echo "rwcommunity $SNMP_RWCOMMUNITY" >>/etc/snmp/snmpd.conf
	fi

	if [ -n "$SNMP_ROCOMMUNITY" ]
	then
	    echo "rocommunity $SNMP_ROCOMMUNITY" >>/etc/snmp/snmpd.conf
	fi

	if [ -n "$SNMP_LOCATION" ]
	then
	    echo "sysLocation $SNMP_LOCATION" >>/etc/snmp/snmpd.conf
	fi

	if [ -n "$SNMP_CONTACT" ]
	then
	    echo "sysContact $SNMP_CONTACT" >>/etc/snmp/snmpd.conf
	fi

	if [ -n "$HOSTNAME" ]
	then
	    echo "sysName $HOSTNAME" >>/etc/snmp/snmpd.conf
	fi
    fi

    # Generate SNMP daemon password file
    if [ ! -r /var/run/snmp/snmpd.conf ]
    then
	>/var/run/snmp/snmpd.conf
	if [ -n "$SNMP_RWUSER" -a -n "$SNMP_RWUSER_PASSWORD" ]
	then
	    echo "createUser $SNMP_RWUSER MD5 $SNMP_RWUSER_PASSWORD" >>/var/run/snmp/snmpd.conf
	fi
	if [ -n "$SNMP_ROUSER" -a -n "$SNMP_ROUSER_PASSWORD" ]
	then
	    echo "createUser $SNMP_ROUSER MD5 $SNMP_ROUSER_PASSWORD" >>/var/run/snmp/snmpd.conf
	fi
    fi

    # Generate general configuration file
    if [ ! -r /etc/snmp/snmp.conf ]
    then
	>/etc/snmp/snmp.conf
	echo "persistentDir /var/run/snmp" >>/etc/snmp/snmp.conf
	echo "mibdirs /usr/lib/snmpmibs" >>/etc/snmp/snmp.conf
	if [ -z "$SNMP_RWCOMMUNITY" ]
	then
	    echo "defVersion 3" >>/etc/snmp/snmp.conf
	    echo "defSecurityName $SNMP_RWUSER" >>/etc/snmp/snmp.conf
	else
	    echo "defVersion 2" >>/etc/snmp/snmp.conf
	    echo "defCommunity $SNMP_RWCOMMUNITY" >>/etc/snmp/snmp.conf
	fi
    fi

    # Start all daemon processes
    /usr/sbin/snmpd -c/etc/snmp/snmpd.conf -M/usr/lib/snmpmibs -Lsdaemon -p/var/run/snmpd.pid
    if [ -r /etc/snmp/snmptrapd.conf ]
    then
	/usr/sbin/snmptrapd -c/etc/snmp/snmptrapd.conf -M/usr/lib/snmpmibs -Lsdaemon -p/var/run/snmptrapd.pid
    fi

    end_script
fi

