#!/usr/bin/perl

=head1 NAME

egg-timer - A super-deluxe egg timer using Net::Peep.

=head1 DESCRIPTION

It is an egg timer.  What more do you want?

=head1 CONFIGURATION

To use this client, include a section like the following the events
block of peep.conf:

  events
  #Event Type  |      Path to Sound File           |    # of sounds to load
  ...
  timer  /usr/local/share/peep/sounds/wetlands/events/hermit-thrush-01.*  1
  end events

=head1 USAGE

  ./egg-timer --help

  ./egg-timer --server=www.foo.org --port=2126 \ 
              --sound=timer --wait=60

  ./egg-timer --wait=300

The --sound and --wait options are specific to this client.

In addition, the following options are common to all Peep clients
(with defaults specific to this client):

  --config=[PATH]       Path to the configuration file to use.
  --debug=[NUMBER]      Enable debugging. (Def:  0)
  --nodaemon            Do not run in daemon mode.  (Default)
  --pidfile=[PATH]      The file to write the pid out to.  (Daemon mode only.)
  --output=[PATH]       The file to log output to. (Def: stderr)
  --noautodiscovery     Disables autodiscovery and enables the server and port options.
                        (Default)
  --server=[HOST]       The host (or IP address) to connect to.  
                        (Def:  localhost)
  --port=[PORT NO]      The port to use. (Def:  2001)
  --help                Prints this documentation.

If you have any problems, try turning on debugging output with
something like --debug=9.

Note that you must have the Peep daemon (peepd) running to hear the
sound.  You can obtain the Peep daemon at 

  http://peep.sourceforge.net

=head1 AUTHOR

Collin Starkweather <collin.starkweather@colorado.edu> Copyright (C) 2001

=head1 SEE ALSO 

perl(1), peepd(1), Net::Peep, Net::Peep::Client, Net::Peep::BC

=cut

# Always use strict :-)
use strict;
use Net::Peep::BC;
use Net::Peep::Client;
use vars qw{ $client $conf %options $wait $sound };

# What follows is a fairly standard formula for any Peep client.  The
# only part you really have to define yourself is a parser method (not
# applicable in this case) and a callback (see loop() below).
# Basically, the meat of this client is a 4-line subroutine.
$client = new Net::Peep::Client;

$client->name('egg-timer');

# The options here are in Getopt::Long style. The %options hash allows
# specify what event is going to be played when the timer goes off
# (with --sound) and how many seconds before the timer goes off (with
# --wait).
$wait = 180;      # default is for a 3-minute egg
$sound = 'timer'; # see CONFIGURATION above
my %options = ( 'wait=s' => \$wait, 'sound=s' => \$sound );

# Initialize the client object.  This will parse the command line
# options.  The default command-line options available to all Peep
# clients include --server and --port.
$client->initialize(%options) || $client->pods();

# The egg timer does not need to specify a parser, so we'll feed it a
# blank code reference
$client->parser( sub { return; } );

# Get the Peep configuration object, which has valuable information
# from the Peep configuration file and the values of any options
# specified on the command-line.
$conf = $client->configure();

# Make sure we default to some reasonable values.  Autodiscovery
# doesn't make sense for the egg timer, so we'll turn it off.  The egg
# timer won't be running as a daemon either.
$conf->setOption('autodiscovery',0) 
    unless $conf->optionExists('autodiscovery') and 
           $conf->getOption('autodiscovery') == 0;

$conf->setOption('server','localhost') 
    unless $conf->optionExists('server') and 
           $conf->getOption('server') ne '';

$conf->setOption('port',2001) 
    unless $conf->optionExists('port') and 
           $conf->getOption('port') ne '';

$conf->setOption('daemon','0') 
    unless $conf->optionExists('daemon') and 
           $conf->getOption('daemon') == 0;

$client->callback( \&loop );

$SIG{'INT'} = $SIG{'TERM'} = sub { $client->shutdown(); };

$client->MainLoop();

sub loop {

    for (my $i=0; $i<$wait; $i++) { print $wait - $i . " seconds to go!\r"; sleep 1; }
    my $broadcast = Net::Peep::BC->new('egg-timer',$conf);
    $broadcast->send('egg-timer', type=>0, sound=>$sound, location=>128, priority=>0, volume=>255);
    print "\n\nDing!\n\n";

} # end sub loop

__END__

