#!/bin/bash

sys_conf="/etc/date.conf"
boot_conf="/boot/date.conf"
conf="/data/etc/date.conf"

sys_ntp_conf="/etc/ntp.conf"
boot_ntp_conf="/boot/ntp.conf"
ntp_conf="/data/etc/ntp.conf"

if ! [ -f $conf ]; then
    if [ -f $boot_conf ]; then
        cp $boot_conf $conf
    elif [ -f $sys_conf ]; then
        cp $sys_conf $conf
    fi
fi

if ! [ -f $ntp_conf ]; then
    if [ -f $boot_ntp_conf ]; then
        cp $boot_ntp_conf $ntp_conf
    elif [ -f $sys_ntp_conf ]; then
        cp $sys_ntp_conf $ntp_conf
    fi
fi

test -f $conf || exit 0

test -n "$os_version" || source /etc/init.d/base
test -n "$os_debug" || source /etc/init.d/conf

has_net_conn || exit 0
    
date_timeout=10
date_method=http
date_host="google.com"
date_interval="900"

source $conf

kill_after_timeout() {
    sleep $date_timeout
    ps aux | grep httplib | grep -v  grep | tr -s ' ' | cut -d ' ' -f 2 | xargs -r kill
}

set_current_date_http() {
    kill_after_timeout &

    timestamp=$(python -c "import calendar, httplib, email.utils; conn = httplib.HTTPConnection('$date_host'); conn.request('HEAD', '/'); \
            print calendar.timegm(email.utils.parsedate(conn.getresponse().getheader('date')))")
    
    if [ -n "$timestamp" ]; then
        date +%s -s @$timestamp > /dev/null
    else
        return 1
    fi
}

set_current_date_ntp() {
    cat $ntp_conf | grep server | head -n 1 | cut -d ' ' -f 2 | xargs ntpdate -t $date_timeout -s
}

start_http() {
    msg_begin "Setting current date using http"
    set_current_date_http
    test $? == 0 && msg_done "$(date)" || msg_fail
    
    msg_begin "Starting http date updater"
    while true; do
        sleep $date_interval
        set_current_date_http
    done &
    msg_done
}

start_ntp() {
    mkdir -p /var/lib/ntp

    cat $ntp_conf | grep -v iburst > ${ntp_conf}.tmp

    if [ -n "$date_ntp_server" ]; then
        echo "server $date_ntp_server iburst" > $ntp_conf
    else
        cat $sys_ntp_conf | grep iburst > $ntp_conf
    fi

    cat ${ntp_conf}.tmp >> $ntp_conf
    rm ${ntp_conf}.tmp

    msg_begin "Setting current date using ntp"
    set_current_date_ntp
    test $? == 0 && msg_done "$(date)" || msg_fail

    msg_begin "Starting ntpd"
    ntpd -g -c $ntp_conf
    test $? == 0 && msg_done || msg_fail
}

stop_http() {
    msg_begin "Stopping date updater"
    ps | grep S60date | grep -v $$ | grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1 | xargs -r kill
    test $? == 0 && msg_done || msg_fail
}

stop_ntp() {
    msg_begin "Stopping ntpd"
    killall ntpd &>/dev/null
    test $? == 0 && msg_done || msg_fail
}

start() {
    if [ "$date_method" == "http" ]; then
        start_http
    else
        start_ntp
    fi

    echo "system date is $(date '+%Y-%m-%d %H:%M:%S')" > /dev/kmsg
}

stop() {
    if [ "$date_method" == "http" ]; then
        stop_http
    else
        stop_ntp
    fi        
}

case "$1" in
    start)
        start
        ;;

    stop)
        stop
        ;;

    restart)
        stop
        start
        ;;

    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?

