#!/bin/bash

logger -t dhcpd "Refresh dhcpd config request"

/opt/Eye/scripts/print-dhcpd.pl
ret=$?

if [ ${ret} -ne 0 ]; then
    exit
    fi

diff -aqbBfi /etc/dhcp/eye.d /etc/dhcp/eye.d.new >/dev/null
ret=$?

if [ ${ret} -ne 0 ]; then
    logger -t dhcpd "Update dhcpd config"
    rm -f /etc/dhcp/eye.d/*.bak >/dev/null 2>&1
    find /etc/dhcp/eye.d/ -iname "*.conf" -exec rename \.conf \.bak {} \;  >/dev/null 2>&1
    cp -f /etc/dhcp/eye.d.new/*.conf /etc/dhcp/eye.d >/dev/null 2>&1
    sed -i 's/\.new//' /etc/dhcp/eye.d/eye.conf >/dev/null 2>&1
    /sbin/dhcpd -t -cf /etc/dhcp/dhcpd.conf >/dev/null 2>&1
    ret=$?
    if [ ${ret} -eq 0 ]; then
        systemctl restart isc-dhcp-server>/dev/null
	else
        logger -t dhcpd "Config error! Skip restart dhcp."
        rm -f /etc/dhcp/eye.d/*.conf >/dev/null 2>&1
        find /etc/dhcp/eye.d/ -iname "*.bak" -exec rename .bak .conf {} \; >/dev/null 2>&1
	fi
    else
    logger -t dhcpd "Config not changed. Skip restart"
    fi

logger -t dhcpd "done"

exit
