#!/bin/sh

CFG_NAME=snmp

create_config() {
    uci -q get $CFG_NAME
    if [ $? -ne 0 ]; then
        cp -f /barix/config/defaults/$CFG_NAME /barix/config/current/
    fi
}

start() {
    create_config

    SNMP_ENABLED=$(uci -q get snmp.main.enabled)
    if [[ "$SNMP_ENABLED" == "true" || "$SNMP_ENABLED" == "on" ]]; then
        # do not configure if we miss the config template 
        # for compatiblity with previous implementations
        if [ -f /barix/config/templates/$CFG_NAME ] ; then
            rm -f /var/lib/net-snmp/snmpd.conf
            # Prepare an initial UCI config for the application.
            echo "SNMPD: Generating SNMPD UCI config..."
            /lib/config/gen_config.sh $CFG_NAME
            echo "SNMPD: Generating SNMPD UCI config...DONE"
        fi

        /etc/init.d/snmpd start
    fi
}

stop() {
    /etc/init.d/snmpd stop
}


# See how we were called.
case "$1" in
  start)
    start
    ;;
    
  stop)
    stop
    ;;

  restart)
    stop
    start
    ;;

  *)
    echo "Usage: $0 {start|stop}"
    exit 1
esac

exit $?

