#!/bin/sh /etc/rc.common

START=50

USE_PROCD=1
PROG="/usr/sbin/snmptrapd"
#CONFIGFILE="/var/run/snmptrapd.conf"
CONFIGFILE="/etc/snmp/snmptrapd.conf"
PERSISTFILE="/usr/lib/snmp/snmptrapd.conf"
check_types(){
	case $1 in
   0)
      Types=""
      ;;
   1)
      Types="net"
      ;;
   2)
      Types="execute"
      ;;
   3)
      Types="execute,net"
      ;;
   4)
      Types="log"
      ;;
   5)
      Types="log,net"
      ;;
   6)
      Types="log,execute"
      ;;
   7)
      Types="log,execute,net"
      ;;
   *)
     Default condition to be executed
     ;;
esac
}
snmptrapd_add() {
	local cfg="$1"
	local types=0
	local Types
	config_get version "$cfg" version
	#config_get log "$cfg" log
	config_get_bool log_enabled logging log 1
	#echo "log_enabled=$log_enabled"
	if [ "$log_enabled" -eq 1 ];then
		#log_flag=4
		types=$(( $types | 4 ))
	fi
	#config_get execute "$cfg" execute
	config_get_bool execute_enabled notifications_processing execute 1
	#echo "execute_enabled=$execute_enabled"
	if [ "$execute_enabled" -eq 1 ];then
		#execute_flag=2
		types=$(( $types | 2 ))
	fi
	#config_get net "$cfg" net
	config_get_bool net_enabled notifications_processing net 1
	#echo "net_enabled=$net_enabled"
	if [ "$net_enabled" -eq 1 ];then
		#net_flag=1
		types=$(( $types | 1 ))
	fi
	check_types "$types"
	#echo "types=$Types"
	config_get oid "$cfg" oid
	if [ "$version" == "v1" ] || [ "$version" == "v2c" ]; then
		#authCommunity TYPES COMMUNITY [SOURCE [OID | -v VIEW ]]
		config_get community "$cfg" community
		config_get source "$cfg" source
		echo "authCommunity $Types $community $source $oid" >> $CONFIGFILE
	elif [ "$version" == "v3" ]; then
		#authUser TYPES [-s MODEL] USER [LEVEL [OID | -v VIEW ]]
		config_get username "$cfg" username
		config_get type "$cfg" type
		config_get engineid "$cfg" engineid
		config_get security_level "$cfg" security_level
		config_get auth_protocol "$cfg" auth_protocol
		config_get auth_password "$cfg" auth_password
		config_get priv_protocol "$cfg" priv_protocol
		config_get priv_password "$cfg" priv_password
		[ "$type" == "trap" ] && echo "createUser -e $engineid $username $auth_protocol $auth_password $priv_protocol $priv_password" >> $PERSISTFILE
		[ "$type" == "inform" ] && echo "createUser $username $auth_protocol $auth_password $priv_protocol $priv_password" >> $PERSISTFILE
		if [ "$security_level" == "noAuthNoPriv" ]; then
			echo "authUser $Types $username  noauth" >> $CONFIGFILE
		elif [ "$security_level" == "authNoPriv" ]; then
			echo "authUser $Types $username auth" >> $CONFIGFILE
		elif [ "$security_level" == "authPriv" ]; then
			echo "authUser $Types $username priv" >> $CONFIGFILE
		fi
		
	fi
}
snmpd_engineid_add() {
	local cfg="$1"

	config_get id_enabled "$cfg" id_enabled
	[ "$id_enabled" -eq 0 ] && return
	config_get engineid "$cfg" engineid
	[ -n "$engineid" ] && echo "engineID $engineid" >> $CONFIGFILE
	config_get engineidtype "$cfg" engineidtype
	[ "$engineidtype" -ge 1 -a "$engineidtype" -le 3 ] && \
		echo "engineIDType $engineidtype" >> $CONFIGFILE
	config_get engineidnic "$cfg" engineidnic
	[ -n "$engineidnic" ] && echo "engineIDNic $engineidnic" >> $CONFIGFILE
}
add_notification(){
    local section="$1"
    local option="$2"
    local switch="$3"
    local _loctmp
    config_get_bool _loctmp "$section" "$option"
    if [ "$_loctmp" -eq 1 ]; then
        echo "$switch yes" >> $CONFIGFILE
    else
        echo "$switch no" >> $CONFIGFILE
    fi
}
append_parm() {
	local section="$1"
	local option="$2"
	local switch="$3"
	local _loctmp
	config_get _loctmp "$section" "$option"
	[ -z "$_loctmp" ] && return 0
	echo "$switch $_loctmp" >> $CONFIGFILE
}

traphandle() {
	local cfg="$1"
	config_get execute "$cfg" execute
	if [ "$execute" -eq 1 ];then
		config_get execute_oid "$cfg" execute_oid
		config_get program "$cfg" program
		echo "traphandle $execute_oid $program" >> $CONFIGFILE
	fi
}
forward_notifications() {
	local cfg="$1"
	config_get net "$cfg" net
	if [ "$net" -eq 1 ];then
		config_get net_oid "$cfg" net_oid
		config_get destination "$cfg" destination
		echo "forward $net_oid $destination" >> $CONFIGFILE
	fi
}
start_service() {
	[ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE"

	config_load snmptrapd

	config_get_bool snmptrap_enabled general enabled 1
	[ "$snmptrap_enabled" -eq 0 ] && return
	procd_open_instance
	add_notification general ignoreAuthFailure ignoreAuthFailure
	add_notification snmptrapd disableAuthorization disableAuthorization
	config_foreach snmptrapd_add trapd_access_control
	append_parm logging format1 format1
	append_parm logging format2 format2
	config_foreach traphandle notifications_processing
	config_foreach forward_notifications notifications_processing
	config_foreach snmpd_engineid_add engineid


	config_get log_location logging log_location
	if [ "$log_location" == "syslog" ]; then
		procd_set_param command $PROG -Ls 3 -f
	elif [ "$log_location" == "file" ]; then
		config_get file_path logging file_path
		procd_set_param command $PROG -Lf $file_path -f
	fi
	procd_set_param file $CONFIGFILE
	procd_set_param respawn
	procd_close_instance
}
stop_service() {
	[ -f "$CONFIGFILE" ] && rm -f "$CONFIGFILE"
}

service_triggers(){
	local script=$(readlink "$initscript")
	local name=$(basename ${script:-$initscript})
	procd_open_trigger
	procd_add_raw_trigger /etc/init.d/$name reload
	procd_close_trigger

	customaction="restart" procd_add_reload_trigger 'snmptrapd'

}
# service_started() {
# 	procd_set_config_changed firewall
# }
