#!/bin/sh /etc/rc.common
#
# Opennet Firmware
# 
# Copyright 2010 Rene Ejury <opennet@absorb.it>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 

START=70

change_banner() {
    awk '{print last; last=$0} END{ if(last !~ "no time retrieved") { print last; exit 1 }}' /etc/banner >/tmp/banner &&
        {
            echo " - $(date) - system restart --- " >>/tmp/banner; mv /tmp/banner /etc/banner; sync
        }
}

get_time() {
    no=0;
    while [ -n "$(uci -q get ntpclient.@ntpserver[${no}])" ]; do
        hostname=$(uci -q get ntpclient.@ntpserver[${no}].hostname)
        port=$(uci -q get ntpclient.@ntpserver[${no}].port)
        ping -c 1 $hostname >/dev/null 2>/dev/null && 
            [ -n "$(/usr/sbin/ntpclient -s -c 3 -i 5 -g 1000000 -p $port -h $hostname)" ] &&
            change_banner && return
        : $((no++))
    done;
}

start() {
    for pid in $(pidof ntpclient); do [ $pid == $$ ] || kill $pid; done
	get_time &
    exit 0
}
