#!/usr/bin/perl
#
# Copyright (C) 2001 Caldera International, Inc.  All Rights Reserved.
#
# servicelist
#
# list msg services to monitor/control
#	- postfix
#	- slapd
#	- cyrus
#	- docview
#

$DEBUG	= 0;				# must be first statement

BEGIN {
	use File::Basename();
	$newdir = File::Basename::dirname($0);
	unshift (@INC, "$newdir");
}

use 		msglib;

sub Usage {
	print STDERR msgi18nmsg(1); # usage message
        exit(1);
}

sub IsValidService {
	my ($service) = @_;

	if (defined($msgservices{"$service"})) {
		return(1);
	}

	return 0;
}

#
# main
#

parse_conf noauth;

msgi18ninit(27); # set 27 is msgservicelist

msgi18naction(); # initialize i18n action list

@attributes = qw(
	service
	description
	pid
	command
	action
	status
);

$argc = @ARGV;
if ($argc < 1) {
	Usage();
}

$all = 0;
$dopid = 0;
$dostatus = 0;
foreach (@ARGV) {
	if (/^--/) {
		chomp($_);
		@optarg = split('=', $_, 2);
                $attr    = $optarg[0];
                $attr    =~ s/^--//;
		$val = $optarg[1];
		if ($val) {
			if ($attr eq "service") {  
				$service = $val;
				if (!IsValidService($service)) {
					print STDERR msgi18nmsg(3, $service); # is not a valid service"
					Usage;
				}
			} else {
				Usage;
			}
		} else {
			if ($attr eq "all") {
				$all = 1;
			} elsif ($attr eq "service") {
				push(@attrlist, $attr);
			} elsif ($attr eq "description") {
				push(@attrlist, $attr);
			} elsif ($attr eq "pid") {
				$dopid = 1;
				push (@attrlist, $attr);
			} elsif ($attr eq "command") {
				push(@attrlist, $attr);
			} elsif ($attr eq "action") {
				push(@attrlist, $attr);
			} elsif ($attr eq "status") {
				$dostatus = 1;
				push (@attrlist, $attr);
			} else {
				Usage;
			}
		}
	} else {
		Usage;
	}
}

if ($service eq "") {
	print STDERR msgi18nmsg(4); # --service argument is missing
	Usage();
}

$numattrs = @attrlist;
if ($all || $numattrs == 0) {
	@attrlist = @attributes;
	$dopid = 1;
	$dostatus = 1;
}

foreach $key (keys(%{$msgservices{$service}})) {
	$values{$key} = $msgservices{$service}{$key};
}

if ($UNITEDLINUX && ($dopid || $dostatus)) {
	# Check UID and don't ask for password if root
	if ($> == 0) {
		$ret = system("$values{'rcommand'} status > /dev/null");
	} else {
		$ret = system("$msgbin/msgroot --service=$values{'service'} --action=status > /dev/null");
	}

	if ($ret == 0) {
		$values{"status"} = "UP";
		my @cmd = split(/\s/, $values{"command"});

		# Check UID and don't ask for password if root
		if ($> == 0) {
			$ret = `/sbin/pidofproc $cmd[0]`;
		} else {
			$ret = `$msgbin/msgroot --pidof=$cmd[0]`;
		}
		chomp($ret);
		$values{"pid"} = $ret;
	}
} elsif ($dopid || $dostatus) {
	open(PS, "ps -C $values{'comm'} -o pid,command |") ||
		die msgi18nmsg(5, "$@"); # Cannot fork ps.

	my $header = <PS>; # get rid of the first line (header)
	while (<PS>) {
		$psout = Trim(chomp($_));
		if ($psout =~ /$values{command}/) {
			$pid = $psout;
			$pid =~ s/ .+$//;
			$values{"pid"} = "$pid";
			$values{"status"} = "UP";
			last;
		}
	}

	close(PS);
}

foreach $attr (@attrlist) { 
	if ($attr eq "action") {
		foreach $act (@{$values{"action"}}) {
			print "$attr:$act\n";
			print "i18naction:$msgi18naction{$act}\n";
		}
	} else {
		print "$attr:$values{$attr}\n";
	}
}

exit(0);
