#!/usr/bin/env perl

#
# start_server.pl - start,stop,restart
#


use strict;
use feature 'say';

use Carp;
use Config::Simple;
use FindBin qw/$Bin/;

use Yote::WebAppServer;
use Yote::SQLiteIO;

use lib "$Bin/../lib";


$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };

my $pidfile = "/usr/local/yote/yote.pid";

my $s = new Yote::WebAppServer;

$SIG{TERM} = sub { 
    $s->shutdown();
    print STDERR "Shutting down due to term
";
    unlink( $pidfile );
    exit;
};



#
# The order of the config is @ARGV, %ENV, ~/.yoterc || ~/.yote/yote.conf || /etc/yote.conf, 
#
my $sqlitefile = "/usr/local/yote/SQLite.yote.db";
unless( -e $sqlitefile ) {
    my $sqlite = new Yote::SQLiteIO( sqlitefile => $sqlitefile );
    $sqlite->init_datastore();
}
my( %config ) = ( port       => 8008, 
		  datastore  => 'Yote::SQLiteIO', 
		  sqlitefile => $sqlitefile );

while( @ARGV ) {
    my $first = shift @ARGV;
    if($first eq '--shutdown') {
        if( -e $pidfile ) {
            my $pid = `cat $pidfile`;
            $pid =~ s/[\n\r]+$//i;
            my $is_pid =  `ps ax | egrep "^\s*$pid\s+" | grep yote_server`;
            if( $is_pid ) {
                print STDERR Data::Dumper->Dump( ["shutting down yote server pid $pid"] );
                `kill $pid`;
            }
            unlink( $pidfile );
        }
        exit;
    }
    elsif( $first eq '--restart' ) {
	#
	# kill any old servers hanging around
	#
	if( -e $pidfile ) {
	    my $res = `cat $pidfile | grep yote_server | xargs kill`;
	    unlink( $pidfile );
	    sleep(2);
	}
	last;
    }
    elsif( $first =~ /^--([^=]+)=(.*)/ ) {
	$config{$1} = $2;
    }
    elsif( $first =~ /^-([^-].*)/ ) {
	$config{$1} = shift @ARGV;
    }
    else {
	say "yote_server <options>
".join( "
	*", ('--shutdown : stops yote server', '--restart : restarts yote server',
						      '--port=<port> : assigns the yote server to run on specified port',
						      '--datastore=<datastore package name> : use data store other than sqlite',
						      '--sqlitefile=<filename> : specify different sqlite file' ) );
	exit 0;
    }
} #while args

#
# Check to make sure no processes are still hanging around.
#
if( -e $pidfile ) {
    open( IN, "<$pidfile" );
    my $pid = <IN>; 
    chomp( $pid );

    my( @haspid ) = `ps -p $pid`;
    my $haspid = grep { $_ =~ /$pid/ } @haspid;
    if( $haspid ) {
	print STDERR "Process $pid still alive. Unable to start yote server
";
	exit 1;
    }
}

#
# Normal start
#
`echo $$ > $pidfile`;

$s->start_server( %config );

