#!/bin/sh
# $FreeBSD: head/multimedia/webcamd/files/webcamd.in 353901 2014-05-12 22:01:10Z nox $
#
# PROVIDE: webcamd
# REQUIRE: DAEMON LOGIN
# KEYWORD: shutdown
#
# ================================================================
# Webcamd is enabled by adding the following line to /etc/rc.conf:
# ================================================================
#
# webcamd_enable="YES"
#
# ================================================================
# By default webcamd instances are invoked by the devd(8) system
# daemon. You can add additional devices to get probed when the
# webcamd rc-script is started by setting the "webcamd_devices"
# variable:
# ================================================================
#
# webcamd_devices="ugen3.4" # one device
# webcamd_devices="ugen3.4 ugen1.2" # two devices
# webcamd_devices="ugen3.4 ugen1.2 ugen0.2" # three devices
# webcamd_devices="ugen3.4/0 ugen3.4/1" # two interfaces on same device
#
# ================================================================
# Webcamd flags can be set in general using the "webcamd_flags"
# variable or per instance by using "webcamd_ugenX_Y_flags" or
# "webcamd_ugenX_Y_Z_flags" where "ugenX_Y" corresponds to "ugenX.Y"
# and the optional "Z" the interface index:
# ================================================================
#
# webcamd_flags="-m v4l2-dev.hflip=1"
# webcamd_ugen3_4_flags="-v 2"
# webcamd_ugen1_2_flags="-L 127.0.0.1:5100:-1"
# webcamd_ugen0_2_flags="-v 2 -i 1 -m v4l2-dev.hflip=1"
#
# ================================================================
# Additional Webcamd flags:
# ================================================================
#
# webcamd_startup_delay=<seconds>
# webcamd_user=<the user to run webcamd as>
# webcamd_group=<the group to run webcamd as>
#

. /etc/rc.subr

name=webcamd
rcvar=webcamd_enable
load_rc_config $name
dev=${2}
iface=${3-0}

: ${webcamd_enable:=NO}
: ${hald_enable:=NO}
: ${webcamd_user=webcamd}
: ${webcamd_group=webcamd}

command=%%PREFIX%%/sbin/webcamd
command_args="-U ${webcamd_user} -G ${webcamd_group}"

start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"

webcamd_pids()
{
    pids=$(pgrep -d ' ' $name)
    pids=${pids% }
    echo "${pids}"
}

webcamd_start_nodev()
{
    for dev in $webcamd_devices
    do
      webcamd_start_dev $(echo $dev | tr '/' ' ') 0
    done
}

webcamd_start_dev()
{
    echo "Starting ${name} at interface ${2} at ${1}."

    # Compute flag variables
    flagsvar=$(echo \$webcamd_${1}_flags | tr '.' '_')
    ifacevar=$(echo \$webcamd_${1}_${2}_flags | tr '.' '_')

    # Launch an instance of webcamd for this device
    ${command} ${command_args} ${webcamd_flags} -i ${2} \
	$(eval $flagsvar) $(eval $ifacevar) -d ${1} -B

    # Wait a bit before next instance launches
    if [ \! -z "$webcamd_startup_delay" ]; then
	sleep $webcamd_startup_delay
    fi
}

webcamd_start()
{
    if checkyesno hald_enable ; then
	command_args="$command_args -H"
    fi

    if [ -z "$dev" ]; then
	webcamd_start_nodev
    else
	webcamd_start_dev "${dev}" "${iface}"
    fi
}

webcamd_stop()
{
    pids=$(webcamd_pids)
    if [ \! -z "${pids}" ]; then
	echo "Stopping ${name}."
	echo "Waiting for PIDs: ${pids}"
	for signal in TERM INT QUIT KILL HUP
	do
	  kill -s ${signal} ${pids}
	  sleep 1;
	  pids=$(webcamd_pids)
	  [ "${pids}" ] || break;
	done
    else
	echo "${name} is not running."
	return 1
    fi
}

webcamd_status()
{
    pids=$(webcamd_pids)

    if [ "${pids}" ]; then
	echo "${name} is running as pid ${pids}."
    else
	echo "${name} is not running."
	return 1
    fi
}

run_rc_command "$1"
