#!/bin/sh
# $FreeBSD: head/net/kea/files/kea_dhcp4.in 396506 2015-09-09 14:28:47Z hrs $
#
# PROVIDE: kea
# REQUIRE: netif routing
# KEYWORD: shutdown 

#
# Add the following to /etc/rc.conf[.local] to enable one or more ISC Kea services
#
# kea_dhcp4_enable (bool):      Run kea-dhcp4, the DHCPv4 server (or NO)
# kea_dhcp4_conf (str):         Path to the configuration file for kea-dhcp4
# kea_dhcp6_enable (bool):      Run kea-dhcp6, the DHCPv6 server (or NO)
# kea_dhcp6_conf (str):         Path to the configuration file for kea-dhcp6
# kea_dhcp_ddns_enable (bool):  Run kea-dhcp-ddns, the DHCP DDNS server (or NO)
# kea_dhcp_ddns_conf (str):     Path to the configuration file for kea-dhcp-ddns
# kea_ctrl_agent_enable (bool): Run kea-ctrl-agent, the Control Agent (or NO)
# kea_ctrl_agent_conf (str):    Path to the configuration file for kea-ctrl-agent
#

kea_dhcp4_enable=${kea_dhcp4_enable:-"NO"}
kea_dhcp6_enable=${kea_dhcp6_enable:-"NO"}
kea_dhcp_ddns_enable=${kea_dhcp_ddns_enable:-"NO"}
kea_ctrl_agent_enable=${kea_dhcp_ddns_enable:-"NO"}
kea_dhcp4_conf=${kea_dhcp4_conf:-"%%ETCDIR%%/kea-dhcp4.conf"}
kea_dhcp6_conf=${kea_dhcp6_conf:-"%%ETCDIR%%/kea-dhcp6.conf"}
kea_dhcp_ddns_conf=${kea_dhcp_ddns_conf:-"%%ETCDIR%%/kea-dhcp-ddns.conf"}
kea_ctrl_agent_conf=${kea_ctrl_agent_conf:-"%%ETCDIR%%/kea-ctrl-agent.conf"}

. /etc/rc.subr

case $0 in
/etc/rc*)
        # during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
        # so get the name of the script from $_file
        name=$_file
        ;;
*)
        name=$0
        ;;
esac

command="%%PREFIX%%/sbin/kea-${name##*/kea_}"
name="${name##*/}"
kea_config_file=""

case ${name##kea_} in 
  dhcp4)
    kea_config_file=${kea_dhcp4_conf}
    ;;

  dhcp6)
    kea_config_file=${kea_dhcp6_conf}
    ;;

  dhcp_ddns)
    kea_config_file=${kea_dhcp_ddns_conf}
    ;;

  ctrl_agent)
    kea_config_file=${kea_ctrl_agent_conf}
    ;;

  *)
    kea_config_file=""
   ;;

esac

rcvar="${name}_enable"
required_files=${kea_config_file}
extra_commands="reload"
start_precmd="kea_start_precmd"
stop_precmd="kea_cmd_precmd"
reload_precmd="kea_cmd_precmd"
status_precmd="kea_cmd_precmd"

kea_start_precmd()
{
   command_args="-c ${kea_config_file} &"
}

kea_cmd_precmd()
{
   local conf_name=$(basename ${kea_config_file} | cut -f1 -d'.')
   local proc_name=$(basename ${command})

   pidfile="/var/run/${conf_name}.kea-${proc_name}.pid"
}

load_rc_config ${name}
run_rc_command "$1"
