#!/bin/sh

if [ "x$interface" != "xeth0" ] ; then exit 0 ; fi

if cat /proc/cmdline | grep -q '\bntp=gateway\b' ; then
    if [ -n "$router" ] ; then
        logger -t udhcpc "ntp=gateway used, using \$router=$router as \$ntpsrv"
        server="$router trust"
    else
        logger -t udhcpc "ntp=gateway used but \$router not provided"
    fi
elif [ -n "$ntpsrv" ] ; then
    # Just take the first
    set -- $ntpsrv
    server="$1"
fi

if [ -z "$server" ] ; then
    logger -t udhcpc "No NTP server via DHCP"
    exit 0
fi

sed -i -e "s/^server [^ ]\+/server $server/g" /etc/chrony/chrony.conf

logger -t udhcpc "Restarting chronyd with server $server"
service -q chronyd conditionalrestart
