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;

use YAML::XS;
use File::Spec;
use Sys::Hostname;

use Vulcan::OptConf;

$| ++;

=head1 SYNOPSIS

 $0 [--cache off|on] [--load off|on] [--lock off|on] [--start|--restart]
 $0 --stop

=cut

my ( $option, %seco );
BEGIN { $option = Vulcan::OptConf->load(); %seco = $option->dump( 'seco' ) }

use lib $seco{lib};
use SECO::Conf::Auto;
use SECO::Engine::Search;

die "no ytt!\n" unless -e File::Spec->join( ( getpwuid $< )[7], 'ytt' );

my %o = $option->get( qw( stop start restart load=s lock=s cache=s ) )->dump;
my $conf = SECO::Conf::Auto->new( $seco{conf} );
my $host = Sys::Hostname::hostname;
my $info = $conf->search( $host );

die "no seco config!" unless my ( $tier ) = @{ $info->{$host} };
my $seco = SECO::Engine::Search->new();
my %mode = ( mode => 'qrw' ) if $tier =~ /pw$/;

$seco->flag( lock => $o{lock} ) if $o{lock};
$seco->stop( %mode ) if $o{stop} ||= $o{restart};

if ( $o{start} ||= $o{restart} )
{
    $seco->purge();
    sleep 3 if $o{stop};
    $seco->start( %mode );
}

$seco->cache( $o{cache} ) if $o{cache};
$seco->flag( load => $o{load} ) if $o{load};

YAML::XS::DumpFile \*STDOUT, scalar $seco->status();
exit 0;