#!/usr/bin/perl -T -w

### BEGIN INIT INFO
# Provides:          epindexer
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: EPrints indexer
# Description:       Start the EPrints indexer daemon as the correct user.
### END INIT INFO

my $user = "eprints";
my $group = "eprints";
my $prefix = "/usr/share/eprints";

######################################################################
#
# chkconfig: - 85 15
# description: Start the eprints indexer daemon as the correct user
#
######################################################################
#
#
######################################################################

use Config::General qw( ParseConfig );
use English;
use POSIX;

use strict;

if( $REAL_USER_ID != 0 )
{
	print "This script is intended to start the eprints indexer as a service.\nIt should only be run as root.\nExiting.\n";
	exit 1;
}
if( !defined $ARGV[0] || $ARGV[0] !~ m/^(start|stop|status)$/ )
{
	print "Usage: $0 {start|stop|status}\n";
	exit 1;
}
my $opt = $1;

# These are not going via Platform, but this is a UNIX script, so meh.
my $uid = (getpwnam($user))[2];
my $gid = (getgrnam($group))[2];
my @sgroups = ($gid); # gid + supplementary groups
while(my( undef, undef, $gid, $members ) = getgrent())
{
	push @sgroups, $gid if $members =~ /\b$user\b/;
}

$REAL_GROUP_ID = $EFFECTIVE_GROUP_ID = "@sgroups";
$REAL_USER_ID = $EFFECTIVE_USER_ID = $uid;

# Read the configuration file
my $confname = "$prefix/cfg/epindexer.conf";
my %conf;
if ( -f $confname )
{
	%conf = ParseConfig( $confname );
}

# Configure verbosity
my $noise = 0;
if( defined $conf{'verbose'} and $conf{'verbose'} =~ m/^(\d+)$/ )
{
	$noise = $1;
}

# Set up the environment
if ( exists $conf{'Environment'} )
{
	foreach my $k ( keys %{$conf{'Environment'}} )
	{
		# regex capture groups don't inherit the source's taint
		$conf{'Environment'}{$k} =~ m/(.*)/;
		$ENV{$k} = $1;
		if ( $noise > 0 )
		{
			print $k . '=' . $ENV{$k} . "\n";
		}
	}
}

# Build the command line for the indexer
my @indexer_cmd = ( "$prefix/bin/indexer" );
# logfile must be non-empty string
if ( defined $conf{'logfile'} and $conf{'logfile'} =~ m/(.+)/ )
{
	push @indexer_cmd, '--logfile', $1;
}
# loglevel is a number from 0 to 6
if ( defined $conf{'loglevel'} and $conf{'loglevel'} =~ m/^([0-6])$/ )
{
	push @indexer_cmd, '--loglevel', $1;
}
# rollcount is a number
if ( defined $conf{'rollcount'} and $conf{'rollcount'} =~ m/^(\d+)$/ )
{
	push @indexer_cmd, '--rollcount', $1;
}
# respawn is a number (of seconds)
if ( defined $conf{'respawn'} and $conf{'respawn'} =~ m/(\d+)/ )
{
	push @indexer_cmd, '--respawn', $1;
}

push @indexer_cmd, $opt;
if ( $noise > 0 )
{
	print 'Executing ' . join( ' ', @indexer_cmd ) . "\n";
}

# Go!
$| = 0;
if( $opt eq 'start' )
{
	print 'Starting EPrints Indexer: ';
}
if( $opt eq 'stop' )
{
	print 'Stopping EPrints Indexer: ';
}
delete $ENV{'PATH'};
my $rv = system( @indexer_cmd );
$rv = $rv >> 8;
if( $opt eq 'start' || $opt eq 'stop' )
{
	if( $rv == 0 )
	{
		print '                                 [  OK  ]'."\n";
	}
	else
	{
		print '                                 [FAILED]'."\n";
	}
}

exit $rv;

=head1 COPYRIGHT

=for COPYRIGHT BEGIN

Copyright 2000-2011 University of Southampton.

=for COPYRIGHT END

=for LICENSE BEGIN

This file is part of EPrints L<http://www.eprints.org/>.

EPrints is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

EPrints is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
License for more details.

You should have received a copy of the GNU General Public License
along with EPrints.  If not, see L<http://www.gnu.org/licenses/>.

=for LICENSE END

