#!/usr/bin/perl
#
# Copyright (C) 2001 Caldera International, Inc.  All Rights Reserved.
#
# servicelistall
#
# list all 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); # the usage message
        exit(1);
}

sub IsValidService {
        my($opt) = @_;
        foreach $member (@services) {
                if ($opt eq $member) {
                        return 1;
                }
        }
        return 0;
}

#
# main
#
msgi18ninit(28); # set 28 is msgservicelistall

parse_conf noauth;

msgi18naction(); # initialize i18n action list

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

@services = sort(keys(%msgservices));

$all = 0;
$dopid = 0;
$dostatus = 0;

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

foreach (@ARGV) {
	if (/^--/) {
		chomp($_);
		@optarg = split('=', $_, 2);
                $attr   = $optarg[0];
                $attr   =~ s/^--//;
		$val    = $optarg[1];
		if ($val) {
			Usage;
		}
		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;
	}
}

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

if ($UNITEDLINUX && ($dopid || $dostatus)) {
	foreach $service (@services) {
		# Check UID and don't ask for password if root
		if ($> == 0) {
			$ret = system("$msgservices{$service}->{'rcommand'} status > /dev/null");
		} else {
			$ret = system("$msgbin/msgroot --service=$msgservices{$service}->{'service'} --action=status > /dev/null");
		}
		if ($ret == 0) {
			$msgservices{$service}->{'status'} = "UP";
			my @cmd = split(/\s/, $msgservices{$service}->{"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);
			$msgservices{$service}->{'pid'} = $ret;
		}
	}
} elsif ($dopid || $dostatus) {
	foreach $service (@services) {
		open(PS, "ps -C $msgservices{$service}->{'comm'} -o pid,command |") ||
			die msgi18nmsg(3, "$@"); # Cannot fork ps: $@

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

foreach $service (@services) {
	if ( $service eq  "docview" && !docview_installed() ) { next; }
	foreach $attr (@attrlist) { 
		if ($attr eq "action") {
			foreach $act (@{$msgservices{"$service"}->{"action"}}) {
				print "$attr:$act\n";
				print "i18naction:$msgi18naction{$act}\n";
			}
		} else {
			print "$attr:$msgservices{$service}->{$attr}\n";
		}
	}
	print "\n";
}

exit(0);
