#!/bin/sh
#
# Copyright (c) Members of the EGEE Collaboration. 2004-2010.
# See http://www.eu-egee.org/partners/ for details on the copyright holders.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

me=$0
mydir=`dirname $0`;
export me mydir


set -- `getopt n $*`
# test whether it is recursive call

NESTED="-v nested=0"
while [ $1 != '--' ]; do case $1 in
	-n) NESTED="-v nested=1"; shift ;;
esac; done
shift

awk -F, $NESTED \
'BEGIN {
	if (nested==0) system("cat $mydir/gen_begin");
	OFS = ",";
	nlines = 0;
}
/^#/ || /^[ 	]*$/ { next; }
{
	source = $2;

	event = $1;

	nline++;
}
# events processing

/-e Transfer/	{ logit();
		  next;}

/-e Accepted/	{ if (checkNOP(7) == 0) logit();
		  next;}

/-e Refused/	{ if (checkNOP(6) == 0) logit(); 
		  next;}

/-e EnQueued/	{ if (checkNOP(6) == 0) logit(); 
		  next;}

/-e DeQueued/	{ if (checkNOP(4) == 0) logit(); 
		  next;}

/-e HelperCall/	{ if (checkNOP(5) == 0) logit(); 
		  next;}

/-e HelperReturn/	{ if (checkNOP(5) == 0) logit(); 
		  next;}

/-e Running/	{ if (checkNOP(3) == 0) logit(); 
		  next;}

/-e Resubmission/	{ if (checkNOP(5) == 0) logit(); 
		  next;}

/-e Done/		{ if (checkNOP(5) == 0) logit(); 
		  next;}

/-e Cancel/	{ if (checkNOP(4) == 0) logit(); 
		  next;}

/-e Abort/	{ if (checkNOP(3) == 0) logit(); 
		  next;}

/-e Clear/	{ if (checkNOP(3) == 0) logit(); 
		  next;}

/-e Purge/	{ if (checkNOP(2) == 0) logit(); 
		  next;}

/-e Match/	{ if (checkNOP(3) == 0) logit(); 
		  next;}

/-e Pending/	{ if (checkNOP(3) == 0) logit(); 
		  next;}

/-e RegJob/	{ if (checkNOP(7) == 0) logit(); 
		  next;}

/-e Chkpt/	{ if (checkNOP(4) == 0) logit(); 
		  next;}

/-e Listener/	{ if (checkNOP(5) == 0) logit(); 
		  next;}

/-e CurDescr/	{ if (checkNOP(3) == 0) logit(); 
		  next;}

/-e UserTag/	{ if (checkNOP(4) == 0) logit(); 
		  next;}

/-e ReallyRunning/	{ if (checkNOP(3) == 0) logit(); 
		  	next;}

/-e CREAMStart/	{ logit(); next;}
/-e CREAMStore/	{ logit(); next;}
/-e CREAMPurge/	{ logit(); next;}
/-e CREAMCall/	{ logit(); next;}
/-e CREAMRunning/	{ logit(); next;}
/-e CREAMReallyRunning/	{ logit(); next;}
/-e CREAMDone/	{ logit(); next;}
/-e CREAMCancel/	{ logit(); next;}
/-e CREAMAbort/	{ logit(); next;}

/-e FileTransferRegister/ {  if (checkNOP(4) == 0) logit();
			next;}
/-e FileTransfer/ { logit(); next;}
/-e Sandbox/ { logit(); next;}

# shell escape (for sequence number branching)

/^!/		{ print substr($0,2,(length($0) - 1)); }

# macro processing - macro name starts and ends with ':'
# in a file gen_MACRONAME.txt is the macro describtion

/^:.*:$/	{system("$me -n $mydir/"substr($1,2,(length($1) - 2))".l" )}


function checkNOP(NOP)
{
	if (NF != NOP) { print nline" : "$2": "NOP" args required ("NF" supplied)"  > "/dev/stderr"; return 1 }
	else return 0;
}

function logit()
{
	split($0,f);

	ff = "";
	for (i=1; i<=NF; i++) ff = ff " " f[i];

	print "send_log_ev" ff;
}
' $1
