#!/usr/bin/env bash

[ -n "${CONNECTION_UUID}" ] || exit 0

SOURCE=/usr/lib/ublinux/os-config; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null  || exit 0
SOURCE=${SYSCONF}/config; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null
SOURCE=${SYSCONF}/network; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null

[[ ${NTPSERVERS,,} == "dhcp" ]] || exit 0

INTERFACE=$1
ACTION=$2

case $ACTION in
    up)
	[[ -n ${DHCP4_NTP_SERVERS} ]] || exit 0
        if systemctl --quiet is-active systemd-timesyncd &>/dev/null; then
	    /usr/lib/ublinux/rc.preinit.d/21-ntp ${DHCP4_NTP_SERVERS} ${INTERFACE}
    	    systemctl restart systemd-timesyncd
    	fi
    ;;
    down)
        if systemctl --quiet is-active systemd-timesyncd &>/dev/null; then
    	    /usr/lib/ublinux/rc.preinit.d/21-ntp "default" ${INTERFACE}
            systemctl restart systemd-timesyncd
        fi
    ;;
esac

exit 0