DHCP_SBIN=/usr/sbin/dhcpd
DHCP_CONF=/etc/dhcp/dhcpd.conf
DHCP_SERVICE=isc-dhcp-server

# absolute path!
DHCP_INCLUDE="$SRV/var/dhcp"

## Generate DHCP hosts config from hosts hosts using pvl.hosts-dhcp
#
#   update_hosts_dhcpe $out $src
function update_hosts_dhcp {
    local out="$1"
    local src="$2"
    local msg="$out: Generating DHCP hosts: $src"
    local include_cache=$(include_cache_path $src)
    local srcs=($(include_cache $include_cache))

    if apply_check "$out" ${srcs[@]:-}; then
        log_skip "$msg"
    else
        log_apply "$msg"
    
        apply_cmd "$out" $OPT/bin/pvl.hosts-dhcp \
            --hosts-include="$HOSTS_INCLUDE" \
            --hosts-include-trace=$include_cache \
             "$src"
    fi
}

## Generate dhcp confs from source using pvl.dhcp-conf:
function update_dhcp_conf {
    local out="$1"
    local src="$2"
    local msg="$out: Generating DHCP conf: $src"

    if apply_check "$out" "$src"; then
        log_skip "$msg"
    else
        log_apply "$msg"
            
        apply_cmd "$out" $OPT/bin/pvl.dhcp-conf \
            --include-path=$DHCP_INCLUDE \
            "$src"
    fi
}

## Test DHCP configuration for validity using dhcpd -t:
#
#   check_dhcp
#
# Uses the global $DHCPD_CONF, 
function test_dhcp {
    if [ ! -e "$DHCP_SBIN" ]; then
        log_warn "check_dhcp: dhcpd not installed, skipping: $conf"
        return 0
    fi
   
    log_check "Checking DHCP: $DHCP_CONF" 

    test_cmd "$conf" \
        "$DHCP_SBIN" -cf "$DHCP_CONF" -t
}

## Reload dhcp hosts
#
#   reload_dhcp
#
# noop's if we haven't reloaded zones
function reload_dhcp {
    if [ "$UPDATE_RELOAD" = 1 ]; then
        log_force "Reload DHCP"
        
    elif [ "$UPDATE_RELOAD" = 0 ]; then
        log_noop "Skip reload DHCP"

        return
 
    elif [ ! -e "$DHCP_SBIN" ]; then
        warn "Skip missing DHCP"

        return 
   
    elif ! service_status $DHCP_SERVICE; then
        log_skip "dhcpd not running; did not restart"

        return

    else
        log_apply "Reload DHCP"
    fi

    service_restart $DHCP_SERVICE
}
