# This file is sourced

if [ -n "$TIMEZONE" ] && [ -e "/usr/share/zoneinfo/$TIMEZONE" ]; then
    cp /usr/share/zoneinfo/$TIMEZONE /etc/localtime 2>/dev/null
fi

if [ -n "$TIMESERVER" ]; then
    # Set timeserver to $SERVER if set to autodetect
    if [ "$TIMESERVER" = "auto" ]; then
        TIMESERVER=${SERVER}
    fi

    # Modify /etc/ntp.conf
    if [ -f "/etc/ntp.conf" ]; then
        sed -i -e '/^server/d' /etc/ntp.conf
    fi
    echo "server ${TIMESERVER}" >>/etc/ntp.conf

    # Modify /etc/default/ntpdate
    sed -i -e 's/^NTPSERVERS=".*"/NTPSERVER="'$TIMESERVER'"/' /etc/default/ntpdate
fi
