# $Id$
use Config;
use File::Basename qw(basename dirname);
chdir(dirname($0));
($file = basename($0)) =~ s/\.PL$//;
$file =~ s/\.pl$//
        if ($Config{'osname'} eq 'VMS' or
            $Config{'osname'} eq 'OS2');  # "case-forgiving"
open OUT,">$file" or die "Can't create $file: $!";
chmod(0755, $file);
print "Extracting $file (with variable substitutions)\n";

print OUT <<"!GROK!THIS!";
$Config{'startperl'} -w
    eval 'exec perl -S \$0 "\$@"'
        if 0;

!GROK!THIS!

print OUT <<'!NO!SUBS!';

BEGIN {
	# This is to update our search paths relative to script
	require File::Basename;
	my $file = __FILE__;
	my ( $name, $path, $suffix ) = File::Basename::fileparse( $file );
	push @INC, $path . '../lib/';

	# This is to find expat if it's installed with perl
	require Config;	
	my $ldpathname = $Config::Config{'ldlibpthname'};
	my $sep = $Config::Config{'path_sep'};
	EXPAT_LIB_SEARCH: for my $path ( @INC ) {
		if ( -d "$path/ext/lib" ) {
			$ENV{$ldpathname} = "$path/ext/lib" . $sep . $ENV{$ldpathname};
			last EXPAT_LIB_SEARCH;
		}
	}
}

use strict;
use POE qw(Component::XUL Wheel::FollowTail Component::Server::TCP Filter::HTTPD);
use XUL::Node;
use XUL::Node::Application;
use XUL::Node::Application::Regman;
use Getopt::Long;
use Pod::Usage;
use Cipres::Registry::BufferServer;
use Sys::Hostname;

use base 'XUL::Node::Application';

my $ROOT;
for my $path ( @INC ) {
    if ( -d "$path/xul-node" ) {
        $ROOT = "$path/xul-node";
    }
}

my $LOGFILE;
if ( -e $ENV{'HOME'} . '/cipres/tmp/' . hostname() . '_registry.log' ) {
	$LOGFILE = $ENV{'HOME'} . '/cipres/tmp/' . hostname() . '_registry.log';
}
else {
	$LOGFILE = $ENV{'HOME'} . '/cipres/tmp/registry.log';
}

my $PORT     = 8077;
my $HELP     = 0;
my $VERBOSE  = 2;
my $SEEKBACK = 1024;
my $BUFFER   = [];

GetOptions( 
    'port=i'     => \$PORT,
    'seekback=i' => \$SEEKBACK,
    'root=s'     => \$ROOT,
    'logfile=s'  => \$LOGFILE,
    'help'       => \$HELP,
    'verbose+'   => \$VERBOSE,
) or pod2usage(2); 

pod2usage(1) if $HELP;

POE::Session->create(
    'inline_states' => {
        '_start' => sub {
            my ( $kernel, $heap, $session ) = @_[KERNEL, HEAP, SESSION];
            $heap->{'first'} = 0;
            $heap->{'logbuffer'} = \$BUFFER;
            POE::Component::XUL->spawn( {
                'port' => $PORT,
                'root' => $ROOT,
                'apps' => {
                    'Regman' => sub {
                        XUL::Node::Application::Regman->VERBOSE( $VERBOSE );
						XUL::Node::Application::Regman->start;
                    }
                },
            } );
            $heap->{'wheel'} = POE::Wheel::FollowTail->new(
                'Filename'   => $LOGFILE,
                'InputEvent' => 'got_line',
                'ErrorEvent' => 'got_error',
                'SeekBack'   => $SEEKBACK,
            );
            $heap->{'server'} = Cipres::Registry::BufferServer->new(
				'LogPort'     => 8088,
				'Buffer'      => $BUFFER,
            );
        },
        'got_line' => sub {
            my $heap   = $_[HEAP];
            my $buffer = ${ $heap->{'logbuffer'} };
            if ( $heap->{'first'}++ ) {
                print "MESSAGE FROM LOGWATCHER: " . $_[ARG0] . "\n";
                push @{ $buffer }, $_[ARG0];
            }
        },
        'got_error' => sub {
            my $heap = $_[HEAP];   
            my $buffer = ${ $heap->{'logbuffer'} };            
            if ( $heap->{'first'}++ ) {
                print "MESSAGE FROM LOGWATCHER: " . $_[ARG0] . "\n";
                push @{ $buffer }, $_[ARG0];             
            }
        },
    },
);

$poe_kernel->run();

=head1 NAME

regsrv - start XUL-Node HTTP server for CIPRES registry manager

=head1 SYNOPSIS

regsrv [options]

Options:

  --port     Port (default is 8077)
  --root     Document root (default is @INC . '/xul-node')
  --logfile  Registry log file to watch (default is $ENV{HOME} . /registry.log')
  --help     Show this message

=cut

!NO!SUBS!
