#!/usr/bin/perl
#  $Id$
# -----------------------------------------------------------------------------
#  Script to allow SKM' "patrol" service remote control from 
#  "conf/cgi-bin/node_proc_ctl" through "sudo"
# -----------------------------------------------------------------------------
#  Author: Andrey Fomenko
#  Modified by: 
#  QA by:  
#  Copyright: videoNEXT Network solutions, Inc.
# -----------------------------------------------------------------------------

use strict;
use warnings;

if(!@ARGV) {
	print 'ERROR: no argument supplied (start|stop|status|restart)';
	exit;
}

my $cmd = lc(shift(@ARGV));
my $STATUSFILE     ="$ENV{APL}/var/wd/status";
my $APL = $ENV{APL} || '/opt/sarch';
my $ctl_script = $^O =~ /darwin/i ? "$APL/sly/Darwin/bin/control" : "/sbin/service patrol";

if(index('-start-stop-status-restart-',$cmd)==-1) {
	print 'ERROR: no valid argument supplied (start|stop|status|restart)';
	exit;
}

if($cmd eq 'status') { # let's read directly from status file
        my $status="unknown";
	if(open STATUS,$STATUSFILE) {
		$status=<STATUS>;
		chomp $status;
		close STATUS;
        }
	print $status; # AT: I do not know why we print without "\n" at the end..
}
else {
	`$ctl_script $cmd 1>/dev/null 2>&1 &`;
	print $?? 'ERROR' : 'OK';
}



