#!/usr/bin/perl
#
# a binary countdown clock
#
######################### 
# $Id: binary_countdown.pl 97 2004-01-09 20:16:19Z steve $
#########################
# $Log$
# Revision 1.2  2004/01/09 20:16:19  steve
# added more documentation
#
# Revision 1.1  2004/01/09 20:10:34  steve
# initial revision
#
# Revision 1.2  2003/01/24 17:18:42  steve
# version info bug
#
# Revision 1.1  2003/01/24 17:17:28  steve
# added
#
#########################

use strict;
use Getopt::Std;
use Time::ParseDate;

my ($version) = '$Revision: 97 $' =~ /Revision:\s*(.+)\$/;

our ( $opt_h, $opt_V, $opt_m );
getopts("m:hV");

usage() if $opt_h;
die "$0 version: $version\n" if $opt_V;

#### end init

$| = 1;
my $to = $ARGV[0];
my $from = $ARGV[1];
my $message = $opt_m if $opt_m;

$to = parsedate($to) if $to;
$from = parsedate($from) if $from;

my $diff;

print "\e[2J\e[H";
print "$message\n" if $message;
print "\e7";
do{
    my $val;
    if( $to ){
	if( $from ){
	    $diff = abs($from - $to);
	}else{
	    $diff = abs(time - $to);
	}
    }else{
	$diff = time;
    }
    
    print "\e8";
    print "\e[1K";
    printf "%032b", $diff;
    sleep 1;
}while($diff);

#### helper subs

sub usage(){
    die <<USAGE;
usage: $0 [options] [to_date [from_date]]

Prints, in binary, the number of seconds between two dates. The display refreshes every 
second until the countdown is reached. The program terminates when the countdown reaches 0.

If the from_date is omitted, the present time is used instead. If to_date is omitted, 
binary_countdown simply displays the current Unix time in binary, refreshing every second. 

This uses VT100 escape sequences, so make sure to run it in a terminal.

options:
    -m TEXT     an optional message to prefix the countdown with
    -h          this help
    -V          print version information

Copyright(C) 2004 Steve Pomeroy <steve\@staticfree.info>
Licensed under the GNU GPL. See documentation for complete details.
USAGE

}

__END__

=head1 NAME

binary_countdown - a countdown clock that displays in binary

=head1 SYNOPSIS

B<binary_countdown.pl> S<[ B<-h> ]> S<[ B<-m> I<message> ]> S<[ I<to_date> S<[I<from_date>]> ]>

=head1 DESCRIPTION

C<binary_countdown.pl>
Prints, in binary, the number of seconds between two dates. The display refreshes every 
second until the countdown is reached. The program terminates when the countdown reaches 0.

If the from_date is omitted, the present time is used instead. If to_date is omitted, 
binary_countdown simply displays the current Unix time in binary, refreshing every second. 

=head1 OPTIONS

=over 8

=item B<-h>

This help

=item B<-m> message

an optional text message

=back

=head1 ENVIRONMENT

No environment variables are used. Make sure to run this in a VT100-compliant terminal.

=head1 AUTHOR

Steve Pomeroy <steve@staticfree.info>
http://staticfree.info/

=head1 LICENSE

Copyright © 2004 Steve Pomeroy <steve@staticfree.info>

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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

=head1 SEE ALSO

perl(1)

=head1 BUGS

send them to the author.

=cut
