#!/usr/bin/perl -T -w -I/opt/eprints3/perl_lib 

######################################################################
#
# chkconfig: - 85 15
# description: Start the eprints indexer daemon as the correct user
#
######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 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 2 of the License, or
#  (at your option) any later version.
#  
#  EPrints 2 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 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

use EPrints::SystemSettings;

use strict;

if( $< != 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 @uinfo = getpwnam($EPrints::SystemSettings::conf->{'user'});
my @ginfo = getgrnam($EPrints::SystemSettings::conf->{'group'});
my $uid = $uinfo[2];
my $gid = $ginfo[2];
$( = $) = $gid;
$> = $< = $uid;

my $indexer_cmd = $EPrints::SystemSettings::conf->{'base_path'}.'/bin/indexer';
$| = 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, $opt );
$rv = $rv >> 8;
if( $opt eq 'start' || $opt eq 'stop' ) 
{
	if( $rv == 0 )
	{
		print '                                 [  OK  ]'."\n";
	}
	else
	{
		print '                                 [FAILED]'."\n";
	}
}

exit $rv;
