The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/env perl

use strict;
use warnings;
no warnings 'recursion';

use Carp;

use File::Pid;
use Yote::ConfigData;
use Yote::ObjProvider;

my $var_dir  = Yote::ConfigData->config( 'yote_var_dir' );
my $base_dir = Yote::ConfigData->config( 'yote_base_dir' );
my $run_dir  = Yote::ConfigData->config( 'yote_run_dir' );
my $port     = Yote::ConfigData->config( 'port' );

push( @INC, "$base_dir/lib" );

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

my $pidfile = File::Pid->new( { file => "$run_dir/yote.pid" } );

$SIG{TERM} = sub { 
    print STDERR "Shutting down due to term\n";
    $pidfile->remove();
    exit;
};

$SIG{INT} = sub { 
    print STDERR "Shutting down due to int\n";
    $pidfile->remove();
    exit;
};

$SIG{CHLD} = sub { 
    print STDERR "Got CHLD\n";
    #this is important. I may be able to handle the occasional crashing of the web server process right here!
};

$SIG{PIPE} = sub {};

my $sqlitefile = "$var_dir/SQLite.yote.db";
unless( -e $sqlitefile ) {
    print STDERR "No sqlite database found to recycle.";
    exit 0;
}
my( %config ) = ( port       => $port, 
		  datastore  => 'Yote::SQLiteIO', 
		  sqlitefile => $sqlitefile );

while( @ARGV ) {
    my $first = shift @ARGV;
    if( $first eq '--version' || $first eq '-V' ) {
	print "$Yote::VERSION\n";
	exit 0;
    }
    else {
	print  "recycle_objs <options>n".join( "\n\t*", ( '--restart : restarts yote server', ) )."\n";
	exit 0;
    }
} #while args

#
# Check to make sure no processes are still hanging around.
#
if( -e "$run_dir/yote.pid" && $pidfile->running() ) {
    print "The yote server is running. Cannot recycle.\n";
    exit 0;
}

#
# Normal start
#
$pidfile->write();

Yote::ObjProvider::init( \%config );
Yote::ObjProvider::recycle_objects();

$pidfile->remove();

__END__