#!/bin/sh /etc/rc.common
# Copyright (C) 2008 OpenWrt.org
START=50

DEFAULT=/etc/default/snmpd
LIB_D=/var/lib/snmp
LOG_D=/var/log
RUN_D=/var/run
PID_F=$RUN_D/snmpd.pid
RUN_C=$RUN_D/snmpd.conf
DRAY_COF=/etc/config/dray_snmp

snmpd_com2sec_add() {
	local cfg="$1"
	config_get secname "$cfg" secname
	[ -n "$secname" ] || return 0
	config_get source "$cfg" source
	[ -n "$source" ] || return 0
	config_get community "$cfg" community
	[ -n "$community" ] || return 0
	echo "com2sec $secname $source $community" >> $RUN_C
}
snmpd_group_add() {
	local cfg="$1"
	config_get group "$cfg" group
	[ -n "$group" ] || return 0
	config_get version "$cfg" version
	[ -n "$version" ] || return 0
	config_get secname "$cfg" secname
	[ -n "$secname" ] || return 0
	echo "group $group $version $secname" >> $RUN_C
}
snmpd_view_add() {
	local cfg="$1"
	config_get viewname "$cfg" viewname
	[ -n "$viewname" ] || return 0
	config_get type "$cfg" type
	[ -n "$type" ] || return 0
	config_get oid "$cfg" oid
	[ -n "$oid" ] || return 0
	# optional mask
	config_get mask "$cfg" mask
	echo "view $viewname $type $oid $mask" >> $RUN_C
}
snmpd_access_add() {
	local cfg="$1"
	config_get group "$cfg" group
	[ -n "$group" ] || return 0
	config_get context "$cfg" context
	[ -n "$context" ] || return 0
	[ "$context" == "none" ] && context='""'
	config_get version "$cfg" version
	[ -n "$version" ] || return 0
	config_get level "$cfg" level
	[ -n "$level" ] || return 0
	config_get prefix "$cfg" prefix
	[ -n "$prefix" ] || return 0
	config_get read "$cfg" read
	[ -n "$read" ] || return 0
	config_get write "$cfg" write
	[ -n "$write" ] || return 0
	config_get notify "$cfg" notify
	[ -n "$notify" ] || return 0
	echo "access $group $context $version $level $prefix $read $write $notify" >> $RUN_C
}
snmpd_pass_add() {
	local cfg="$1"
	local pass='pass'
	
	config_get miboid "$cfg" miboid
	[ -n "$miboid" ] || return 0
	config_get prog "$cfg" prog
	[ -n "$prog" ] || return 0
	config_get_bool persist "$cfg" persist 0
	[ $persist -ne 0 ] && pass='pass_persist'
	config_get priority "$cfg" priority
	priority=${priority:+-p $priority}
	echo "$pass $priority $miboid $prog" >> $RUN_C
}

snmpd_change_default_port (){
  [ -f $DRAY_COF ] || return 1
  local port=$(uci get dray_snmp.snmp_config.snmp_port)
  if [ "$port" != "161" ]; then
      echo "agentaddress udp:$port" >> $RUN_C
  fi    
}


start() {
	[ -d $LIB_D ] || mkdir -p $LIB_D
	[ -d $LOG_D ] || mkdir -p $LOG_D
	[ -d $RUN_D ] || mkdir -p $RUN_D
	[ -f $RUN_C ] && rm -f $RUN_C
	
	config_load snmpd
	
	config_foreach snmpd_com2sec_add com2sec
	config_foreach snmpd_group_add group
	config_foreach snmpd_view_add view
	config_foreach snmpd_access_add access
	config_foreach snmpd_pass_add pass
	snmpd_change_default_port

	[ -f $DEFAULT ] && . $DEFAULT
	local client_ip=$(uci get dray_snmp.snmp_config.address)
	if [ "$client_ip" != "" ]; then
	  #replace default to an ip address
    sed -i "s/default/$client_ip/g" $RUN_C
    fi
	local enable=$(uci get dray_snmp.snmp_config.enable)
	if [ "$enable" == "1" ]; then
			$DEBUG /usr/sbin/snmpd $OPTIONS
	fi
}
stop() {
	[ -f $PID_F ] && kill $(cat $PID_F)
	[ -f $RUN_C ] && rm -f $RUN_C
}
