#!/usr/bin/perl
# $Id: cdkwrap,v 1.1 1997/09/28 19:58:10 grimaldo Exp $
#-----------------------------------------------------------------------
#       cdkwrap 
#-----------------------------------------------------------------------
# AUTHOR: D. Emilio Grimaldo T.         grimaldo@panama.iaehv.nl
# DESCRIPTION:
#	A wrapper program for easily adding plug-outs to Chklogs if
#	these plug-outs don't conform to the API. The following 
#	environment variables are passed by Chklogs:
#	        CDKROOT	  The repository were it can be place if archived
#		CDKLOG    Fully qualified name of the log we work on
#		CDKMAILTO Recipient of the mail message
# USAGE: 
#	cdkwrap [options] program [parameters]
#	Options:
#		%ARCHIVE  %ARCH	 Archive log after executing program
#		%TRUNCATE %TRUNC Truncate log after executing program
#
# EXIT VALUE
#	0	No errors
#	1	Syntax error in command line
#	2	Cannot find program that I'm attempting to execute
#	3	Environment variables not defined
#	4	Internal error

# ********* CONFIGURATION SECTION *********

use	lib '/home/grimaldo/devel/Degt';
use	Smtp;
use	strict;

# ********** LOCAL DATA SECTION **********
my $cvsId = '$Revision: 1.1 $';
my $optionArchive = 0;
my $optionTruncate= 0;
my $plugout       = '';
my $params        = '';
my $MAIL;			# The global for the SMTP socket
my @MyHeaders;
my @data;
my ($myself, $clp, $i);
# ********* ********************* *********


sub Usage {
    print "\nUsage: cdkwrap [%ARCH | %TRUNC] program [%L] [params]\n";
    exit 1;
}

sub Abort {
    my ($msg, $ecode) = @_;

    print "$msg\n" if ($msg ne '');
    &CloseConnection(\*MAIL) if ($ecode > 4);
    exit($ecode);
}

&Usage if $#ARGV == -1;
#
# First grab the options meant for us and not for the plugout program.
# %ARCH %ARCHIVE %TRUNC %TRUNCATE are ours only in this context!
#
$clp = shift;
if ($clp eq '%ARCH'  || $clp eq '%ARCHIVE') {
    $optionArchive  = 1;
}
elsif ($clp eq '%TRUNC' || $clp eq '%TRUNCATE') {
    $optionTruncate = 1;
}
else {
    unshift(@ARGV, $clp);
}

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Now process the rest of the command line
#
foreach $i (0 .. $#ARGV) {
    if ($plugout eq "" && !($ARGV[$i] =~ m/^%/)) {
	$plugout = $ARGV[$i];
	next;
    }
    $params .= "$ARGV[$i] ";
}

&Abort('',2) if (-e $plugout || (! -x $plugout));
&Abort('',3) if ($ENV{'CDKROOT'} eq '' || $ENV{'CDKLOG'} || $ENV{'CDKMAILTO'});

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Now mail anything that comes out of the pipe in a single mail message
#
open(PIPE,"$plugout $params 2> /dev/null |") || &Abort('',4);
while (<PIPE>) {
    push @data, $_;
}
close(PIPE);

$myself = $ENV{'USER'} || $ENV{'LOGNAME'} || 'nobody';
push @MyHeaders, "X-Chklogs: $plugout";
&OpenSMTP(\*MAIL, 'localhost');
&Mail(\*MAIL, 				# Socket handle
      $ENV{'CDKMAILTO'}, 		# To:
      $myself, 				# From:
      "CDK wrapper $plugout",		# Subject:
      \@data,				# Content
      \@MyHeaders);			# Extended headers
&CloseConnection(\*MAIL);

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# If necessary process %ARCHIVE or %TRUNCATE
