1#! @PATH_PERL@ -w
2
3die "perl5 needed\n" unless ($] > 5);
4
5use Getopt::Std;
6
7$opt_f = 0;			# 'Hard' failure if 'state' is unknown
8$opt_n = 1000;			# How many tries before we give up? (10 min+)
9$opt_s = 6;			# Seconds to sleep between tries (6s = 10/min)
10$opt_v = 0;			# Be verbose?
11
12getopts('fn:s:v');
13
14$cmd = 'ntpq -c "rv 0 state"';
15
16$| = 1;				# Autoflush output.
17
18print "Waiting for ntpd to synchronize...  " if ($opt_v);
19for ($i = 0; $i < $opt_n; ++$i) {
20    open(Q, $cmd." 2>&1 |") || die "Can't start ntpq: $!";
21    while(<Q>) {
22      if (/^state=4/) {
23	print "\bOK!\n" if ($opt_v);
24	exit 0;
25      }
26
27      if (/request variable was unknown/) {
28	print "\bCan't tell!\nPerhaps you are running an old version of ntpd.\n" if ($opt_v);
29	exit $opt_f;
30      }
31
32      if (/Connection refused/) {
33	print "\bntpd is not running!\n" if ($opt_v);
34	exit 1;
35      }
36    }
37    close(Q);
38    print "\b".substr("*+:.", $i % 4, 1) if ($opt_v);
39    sleep($opt_s);
40}
41print "\bNo!\nntpd did not synchronize.\n" if ($opt_v);
42exit 1;
43