#!/usr/local/bin/perl

use strict;
use v5.8.1;

use warnings;
use Data::Dumper;
use Getopt::Long;
use Pod::Usage;
use UNIVERSAL::require;

my $options;
my $foreground;
my $command = shift || '';

BEGIN {
	$Getopt::Long::ignorecase=0;
	$options = {};
	GetOptions ($options, qw/
		port=i 
		alias=s
		lib|I=s@
		foreground
		version
		debug
		help
		/);
	# $options->{foreground} # Run in the foreground
	unshift(@INC, 
		map {s/~/$ENV{HOME}/;$_} 
		map {split(/:/=>$_)} 
		map {ref $_ ? @{$_} : $_} ($options->{lib})
	) if (exists $options->{lib});
}



$options->{alias} ||= 'POEIKCd';
$options->{port} ||= 47225;
$foreground = exists $options->{foreground} || exists $options->{debug} ;

use POEIKCdaemon;
use POEIKCdaemon::Utility;

if (exists $options->{debug}) {
	no warnings;
	$POEIKCdaemon::DEBUG = $options->{debug};
	for (qw/POEIKCdaemon POEIKCdaemon::Utility/) {
		Class::Inspector->loaded( $_ ) or die;
	}
}

our $VERSION = $POEIKCdaemon::VERSION;

for ($command) {

	### OPTION ###

	exists $options->{version} and do {
		printf "poeikcd version: %s\n", $VERSION;
		last;
	};

	exists $options->{help} and pod2usage(1);

	### COMMAND ###

	/stop|restart/i and do {
		if( Proc::ProcessTable->use ){
			my $proc;
			for my $ps( @{Proc::ProcessTable->new->table} ) {
				if ($ps->{pid} != $$ and $ps->{fname} eq 'poeikcd'){
					$proc++;
				}
			}
		 	/restart/i ? do{$command='start';redo} : last unless $proc;
		}
		use POE::Component::IKC::ClientLite;
		my ($name) = $0 =~ /(\w+)\.\w+/;
		$name .= $$;
		my $ikc = create_ikc_client(
			ip => '127.0.0.1',
			port => $options->{port},
			name => $name,
		);
		$ikc or do{
			if( Proc::ProcessTable->use ){
				for my $ps( @{Proc::ProcessTable->new->table} ) {
					if ($ps->{pid} != $$ and $ps->{fname} eq 'poeikcd'){
						print $ps->{cmndline}," .... already running \n";
					}
				}
			}
			printf "%s\n\n",$POE::Component::IKC::ClientLite::error; 
			exit;
		};
		my $ret = $ikc->post_respond($options->{alias}.'/stop_respond');
		$ikc->error and die($ikc->error);
		my ($loti, $pid, $port) = @{$ret};
		printf "poeikcd is stopped. (PID:%s, Port:%s, %s)\n", 
			$pid, $port, $loti  unless $foreground;
		/restart/i ? do{$command='start';redo} : last;
	};

	/start/i and do {
		use Proc::Daemon;
		printf "poeikcd is Started. (%s)\n",scalar(localtime) unless $foreground;
		Proc::Daemon::Init unless $foreground;
		POEIKCdaemon->daemon(%{$options});
		last;
	};

	### HELP ###
	
	pod2usage(1)

}


__END__

=head1 NAME

poeikcd - POE IKC daemon

=head1 SYNOPSIS

  poeikcd {start|stop|restart} [options]

  eg:
    poeikcd start -p 47225
    poeikcd stop  -p 47225

  Options:

    -p,  --port=#      : Port number to use for connection.

    -a   --alias=s     : Symbolic name, or session alias methods 

    -f   --foreground  : Run in the foreground 
                         STDOUT & STDERR It outputs.
                         eg) poeikcd start -f >> ~/logs.txt 2>1& &

    -Idirectory        : specify @INC/#include directory
         --lib           eg) -I=~/lib:/mylib/ or --lib=~/lib:/mylib/

    -v   --version     : show version number

    -d   --debug       : debug mode. Run in the foreground 
                         STDOUT & STDERR It outputs.

    -h   --help        : show this help screen


=head1 DESCRIPTION

poeikcd (L<POEIKCdaemon>) is daemon of POE::Component::IKC

=head1 AUTHOR

Yuji Suzuki E<lt>yuji.suzuki.perl@gmail.comE<gt>

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 SEE ALSO

L<pikc>

=cut
