#!/sbin/openrc-run

depend() {
	need net
	after firewall
}

command=/usr/bin/sfcapd
pidfile=/var/run/${SVCNAME}/${SVCNAME}.pid
: ${exec_user:=$UID}
: ${exec_group:=$GID}
: ${exec_dir:=$BASEDIR}

start_pre() {
	checkpath --directory --owner "${exec_user:-nobody}:${exec_group:-nobody}" "${exec_dir:-/var/log/nfcapd}"
	checkpath --directory --owner "${exec_user:-nobody}:${exec_group:-nobody}" "${pidfile/$SVCNAME.pid}"
}

start() {
	ebegin "Starting ${SVCNAME}"
	start_pre()
	
	[ -n "${SOURCE}" ] && SOURCE=`echo -n "${SOURCE}" | sed 's/ / -n /g'`
	[ "${IPV4}"       == "yes" ] && command_args="${command_args} -4"
	[ "${IPV6}"       == "yes" ] && command_args="${command_args} -6"
	[ "${ALIGN}"      == "yes" ] && command_args="${command_args} -w"
	[ "${AUTOEXPIRE}" == "yes" ] && command_args="${command_args} -e"
	[ "${COMPRESS}"   == "yes" ] && command_args="${command_args} -z"
	for optname in p:PORT b:BINDHOST j:MULTICASTGROUP i:IFACE R:REPEAT \
		I:IDENT l:BASEDIR n:SOURCE s:SAMPLERATE S:SUBDIR \
		T:EXTENSIONS t:INTERVAL u:UID g:GID \
		B:BUFFLEN ; do
	    opt="${optname/:*}"	optvar="${optname/*:}"
	    eval optvalue="\$$optvar"
	    [ -n "$optvalue" ] && command_args="${command_args} -${opt} ${optvalue}"
	done

	# Handle remote command as a special case
	if [ -n "${ROTATECMD}" ]; then
		start-stop-daemon --start -b --exec $command \
			--pidfile $pidfile \
			-- ${command_args} -x "${ROTATECMD}" -P $pidfile
	else
		start-stop-daemon --start -b --exec $command \
			--pidfile $pidfile \
			-- ${command_args} -P $pidfile
	fi

	eend $?
}
