#!/bin/sh
#
# $FreeBSD: ports/net-mgmt/net-snmp53/files/snmpd.sh.in,v 1.7 2007/10/27 07:20:34 kuriyama Exp $
#
# PROVIDE: snmpd
# REQUIRE: DAEMON
#
# Add the following line to /etc/rc.conf to enable snmpd:
#
# snmpd_enable="YES"
#

snmpd_enable=${snmpd_enable-"NO"}
snmpd_flags=${snmpd_flags-""}
snmpd_pidfile=${snmpd_pidfile-"/var/run/snmpd.pid"}
snmpd_conffile=${snmpd_conffile-""}
snmpd_flush_cache=${snmpd_flush_cache-"NO"}

. /etc/rc.subr

load_rc_config net_snmpd

if [ ! -z "$net_snmpd_enable" ]; then
    echo "Warning: \$net_snmpd_enable is obsoleted."
    echo "         Use \$snmpd_enable instead."
    snmpd_enable="$net_snmpd_enable"
    [ ! -z "$net_snmpd_flags" ] && snmpd_flags="$net_snmpd_flags"
fi

name=snmpd
rcvar=`set_rcvar`

command=/usr/local/sbin/${name}
pidfile=${snmpd_pidfile}
start_precmd=snmpd_precmd

load_rc_config ${name}

snmpd_precmd () {
    if checkyesno snmpd_flush_cache; then
	rm -vf /var/net-snmp/.snmp-exec-cache
    fi
}

case "${snmpd_flags}" in
*-p\ *)
	echo "Warning: \$snmpd_flags includes -p option." \
		"Please use \$snmpd_pidfile instead."
	;;
*)
	snmpd_flags="-p ${snmpd_pidfile} ${snmpd_flags}"
	;;
esac

for conffile in ${snmpd_conffile}; do
	if [ ! -z ${conffile} -a -f ${conffile} ]; then
	       if [ -z ${snmpd_conffile_set} ]; then
		       snmpd_conffile_set="${conffile}"
	       else
		       snmpd_conffile_set="${snmpd_conffile_set},${conffile}"
	       fi
	else
	       echo "snmpd configuration file $conffile not set or not found."
	       exit 1
	fi
done

case "${snmpd_flags}" in
*-c\ *)
	echo "Warning: \$snmpd_flags includes -c option." \
		"Please use \$snmpd_conffile instead."
	;;
*)
	if [ ! -z "${snmpd_conffile_set}" ]; then
		snmpd_flags="-c ${snmpd_conffile_set} ${snmpd_flags}"
	fi
	;;
esac

run_rc_command "$1"
