#!/usr/bin/perl
#
# htpdate time poller version 0.9.1
# Copyright (C) 2004 Eddy Vervest
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# http://www.gnu.org/copyleft/gpl.html

# Proxy setting are read from environment 
# e.g. in bash for setting environment variables:
#
# export HTTP_PROXY='http://wwwproxy.xs4all.nl:8080'
#
# or set the proxy value here
#
# $ENV{HTTP_PROXY} = 'http://wwwproxy.xs4all.nl:8080';
#
# If proxy authentication is required, specify your userid and password
# in the 'init' subroutine below

# Specify SSL ciphers for HTTPS access (optional)
#
# $ENV{HTTPS_VERSION} = 3;
# $ENV{CRYPT_SSLEAY_CIPHER} = 'DHE-RSA-AES256-SHA:DES-CBC3-SHA';

use LWP::UserAgent;
use Getopt::Std;

sub init () {

	getopts('dhqx') || usage;			# specify valid switches
	usage() if $opt_h;
	usage() unless $ARGV[0];

	$version	='0.9.1';				# version number
	$minadjust	=1;						# minimum time step in seconds
	$maxadjust	=1800;					# maximum time step in seconds
	$datecommand='date -s';				# "date" command to set time

	$userid='';							# userid for proxy servers
	$password='';						# password for proxy server

	if ( $ARGV[0] =~ /^http/i ) {
		$server=$ARGV[0];
	} else {
		$server = "http://".$ARGV[0];
	}

}

sub usage() {

	print STDERR <<USAGE;

htpdate version $version
Usage: $0 [-dhqx] <URL>

	-d	debug
	-h	show this help
	-q	quiet
	-x	do not set the time (only show)

	e.g. $0 -x http://www.microsoft.com

USAGE

	exit;

}


init();

$ua = LWP::UserAgent->new;
$ua->agent("htpdate/$version");
$ua->timeout(5);
$ua->env_proxy;
$ua->max_redirect(1);					# do not follow redirects

# prepare the HTTP request header
$req = HTTP::Request->new(HEAD => $server );

# set proxy authentication, if applicable
$req->proxy_authorization_basic ( $userid, $password ) if ( $userid );

# force a clean time stamp from the webserver (not a cached one)
$req->header(Pragma => "no-cache", 'Cache-control' => "Max-age=0");

# send the request to the webserver
print $req->as_string if $opt_d;

# receive the request
$res = $ua->request($req);

# checking on "$res->is_success" will fail in case of redirects,
# so I use "$res->date" in stead
if ( $res->date ) {
	# $res->as_string output is formatted by perl!
	print $res->as_string if $opt_d;

	# calculate the difference between local and remote time
	$diff = $res->date - time();

	print "$server => $diff seconds\n" unless $opt_q;
	if ( abs($diff) > $maxadjust ) {
		print STDERR "Refuses clock setting ($diff sec)\n" unless $opt_q;
	} elsif ( abs($diff) <= $minadjust ) {
		print "Time not changed ($diff sec)\n" unless $opt_q;
	} else {
		$newtime = scalar localtime(time + $diff);
		if ( ! $opt_q ) {
			print "Setting time...\n";
			print `$datecommand \"$newtime\"`;
		}
	}
} else {
	print STDERR "No suitable server found\n" unless $opt_q;
}
