#!/usr/bin/perl
#  $Id: pm_reg 23880 2011-09-14 17:58:16Z teetov $
# -----------------------------------------------------------------------------
#  Name:       pm_reg
#  Purpose:    add, update or remove process registation
#  Parameters: see "printUsage" subroutine below
#
#  env:        following variables are required:  APL, APL_USR
# -----------------------------------------------------------------------------
#  Author: A Baranetsky
#  Edited by: A Baranetsky
#  QA by:  Christopher C Gettings
#  Copyright: videoNEXT LLC
# -----------------------------------------------------------------------------

use strict;
use warnings;
use SKM::PM;


sub printUsage {
	print "Usage:\n";
	print "   pm_reg file1.pconf file2.pconf ...         - add processes using pconf files\n";
	print "   pm_reg etc/*.pconf                         - add processes using pconf files\n";
	print "   pm_reg -a param1=value1 param2=value2 ...  - add process\n";
	print "   pm_reg -r procname=value                   - remove process\n";
	print "   pm_reg -u procname=value param=value ...   - update process configuration\n";
	print "   pm_reg -h|--help                           - display this information\n";
	print "\n";
	print "   Process registration properties:\n";
	print "      PROCNAME      - unique process registration id\n";
	print "      DESCRIPTION   - short text description\n";
	print "      STARTUP       - 0|1\n";
	print "      CHECK_TIME    - process execution check interval (sec)\n";
	print "      START_SCRIPT  - start command line\n";
	print "      STOP_SCRIPT   - stop command line\n";
	print "      CHECK_MASK    - mask used to identify process in process list\n";
}


sub readPropertyFile {
	my ($fileName, $dieOnError) = @_;
	my $properties = {};
	
	if (open(PROP_FILE, "<$fileName")) {
		my %mcfg = map{/(^.+?)=(.*)/} grep {/^.+=.*/} <PROP_FILE>;
		$properties = \%mcfg;
		close(PROP_FILE);
	} elsif ($dieOnError) {
		die "unable to open file";
	}

	return $properties;
}



sub readParameters {
	my $properties = {};

	for(my $i = 1; $i <= $#ARGV; $i++) {
		my @params = split(/=/, $ARGV[$i], 2);
		if ($#params < 1) {
			die "Invalid prameter=value pair: $ARGV[$i]";
		} else {
			$properties->{uc($params[0])} = $params[1];
		}
	}

	return $properties;
}


#
# Main
#

if ($#ARGV < 0 || $ARGV[0] eq '-h' || $ARGV[0] eq '--help') {
	printUsage();
	exit;
}


if ($ARGV[0] eq '-a') {
	#
	# Register process
	#
	eval {
		my $pconf = readParameters();
		insertProcess($pconf);
		print "Process $pconf->{'PROCNAME'} registered\n";
	}; if ($@) {
		die "Failed to update process: $@";
	}

} elsif ($ARGV[0] eq '-u') {
	#
	# Update process
	#
	eval {
		my $pconf = readParameters();
		my $updateResult = updateProcess($pconf);
		if ($updateResult == 1) {
			print "Process $pconf->{'PROCNAME'} updated\n";
		} elsif ($updateResult == 0) {
			print "Process $pconf->{'PROCNAME'} is not found\n";
		} else {
			print "Nothing to update\n";
		}
	}; if ($@) {
		die "Failed to update process: $@";
	}


} elsif ($ARGV[0] eq '-r') {
	#
	# Delete process
	#
	eval {
		my $pconf = readParameters();
		if (defined($pconf->{'PROCNAME'})) {
			if (deleteProcess($pconf->{'PROCNAME'})) {
				print "Process $pconf->{'PROCNAME'} deleted\n";
			} else {
				print "Process $pconf->{'PROCNAME'} is not found\n";
			}
		} else {
			print "Property 'procname' is required\n";
		}
	}; if ($@) {
		die "Failed to delete process: $@";
	}

} else {
	# 
	# Handle process file list
	#
	foreach my $pfile (@ARGV) {
		eval {
			my $pconf = readPropertyFile($pfile, 1);
			insertProcess($pconf);
			print "Process $pconf->{'PROCNAME'} ($pfile) registered\n";

		}; if ($@) {
			die "Failed to load file $pfile: $@";
		}
	}
}
