#!/usr/local/bin/perl -Tw
# slchoosed - choosing deamon for perl Schedule::Load
# $Id: slchoosed,v 1.10 2001/02/13 14:33:52 wsnyder Exp $
################ Introduction ################
#
# This program is Copyright 2000 by Wilson Snyder.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of either the GNU General Public License or the
# Perl Artistic License, with the exception that it cannot be placed
# on a CD-ROM or similar media for commercial distribution without the
# prior approval of the author.
# 
# 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.
# 
# If you do not have a copy of the GNU General Public License write to
# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
# MA 02139, USA.
######################################################################

require 5.004;
use lib 'blib/lib';	# testing
use English;
use Getopt::Long;
use Pod::Text;
use Schedule::Load::Chooser;

BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }	# Secure path

######################################################################
# configuration

######################################################################
# globals

######################################################################
# main

@Orig_ARGV = @ARGV;

my $Debug = 0;
my %server_params = ();
my $opt_quiet = 1;

$result = &GetOptions (
		       "help"		=> \&usage,
		       "debug"		=> \&debug,
		       "version"	=> \&version,
		       "quiet!"		=> \$opt_quiet,	# Debugging
		       "port=i"		=> sub {shift; $server_params{port} = shift;},
		       "dhost=s"	=> sub {shift; push @{$server_params{dhost}}, shift;},
		       );

if (!$result) { &usage(); }

autoflush STDOUT 1;
autoflush STDERR 1;
if ($opt_quiet && !$Debug) {
    # Silence please (in case user didn't pipe when starting us)
    open(STDOUT, "/dev/null");
    open(STDERR, "/dev/null");
    open(STDIN, "/dev/null");
}

# Loop in case something kills us
while (1) {
    print "Starting server\n" if $Debug;
    unless ($pid = fork) {
        Schedule::Load::Chooser->start (%server_params,
					restart	=> sub {},
					);
	exit(0);
    }
    waitpid($pid,0);
    warn "%Warning: Server aborted\n";
    sleep(1);
    kill 9, $pid;
    sleep(1);
    # Restart this program by re-execing; allows users to restart the deamon
    exec $0, @Orig_ARGV if -r $0;
}

exit (0);

######################################################################

sub usage {
    print '$Id: slchoosed,v 1.10 2001/02/13 14:33:52 wsnyder Exp $ ', "\n";
    $SIG{__WARN__} = sub{};	#pod2text isn't clean.
    pod2text($0);
    exit(1);
}

sub version {
    print "Version: $Schedule::Load::VERSION\n";
    print 'Id: $Id: slchoosed,v 1.10 2001/02/13 14:33:52 wsnyder Exp $ ';
    print "\n";
    exit (1);
}

sub debug {
    $Debug = 1;
    $Schedule::Load::Chooser::Debug = 1;
    $Schedule::Load::Schedule::Debug = 1;
}

######################################################################
__END__

=pod

=head1 NAME

slchoosed - Distributed load chooser for perl Schedule::Load

=head1 SYNOPSIS

B<slchoosed>
[ B<--help> ]
[ B<--port=>I<port> ]
[ B<--dhost=>I<host> ]
[ B<--version> ]

=head1 DESCRIPTION

slchoosed will start a deamon to choose machines for the Schedule::Load
package.

=head1 ARGUMENTS

=over 4

=item --help
Displays this message and program version and exits.

=item --port
Specifies the port number that slchoosed uses.

=item --dhost
Specifies the deamon host name that slchoosed uses.  May be specified
multiple times to specify backup hosts.  Defaults to SLCHOOSED_HOST
environment variable, which contains colon separated host names.  When
slchoosed starts, any hosts listed AFTER the current host are assumed to be
backup hosts, and are sent a reset so that this host may takeover the
choosing task.

=item --version
Displays program version and exits.

=back

=head1 SEE ALSO

C<slreportd>, C<Schedule::Load>, C<Schedule::Load::Chooser>, C<Schedule::Load::Reporter>, 

=head1 DISTRIBUTION

This package is distributed via CPAN.

=head1 AUTHORS

Wilson Snyder <wsnyder@wsnyder.org>

=cut
######################################################################
