#!/bin/sh

# Get time offset from NTP server, then
# add to time-offset.tsv a line of the form:
# "2011-05-14 23:10:02   1305407402   -0.472064"

# Configure the variables below before using this script:

ntp_host="_address_of_your_favorite_ntp_host_"
dir="$HOME/arduino/avr-progs/clock/calibration"

cd $dir

# Warm up the network path
/usr/sbin/ntpdate -q $ntp_host > /dev/null
sleep 1

resp=`/usr/sbin/ntpdate -q $ntp_host | grep ntpdate`

year=`date +%Y`
date=`echo "$resp" | sed -r "s/^(.{6}) (.{8}) .*/\1 $year \2/"`
date=`date -d "$date" +'%F %T   %s'`
offset=`echo "$resp" | sed -r 's/.* offset (.*) sec/\1/'`
data_line="$date   $offset"

echo "$data_line" >> time-ntp.tsv
