#!/bin/sh
# NAME:		pgi_postNFLT
# PURPOSE:      specific plugin to fix the content of the PRO_CATG keyword for NFLT files
# AUTHOR:       Reinhard Hanuschik / ESO June 2004
# VERSION:      1.0 - 
#		1.1 -- using replacekey 
# PARAMETERS:   $1 - AB name
# OUTPUT:       input file, with proper PRO_CATG keyword
# CONFIG FILE:  NONE
# COMMENTS:     workaround for NFLT files
# ================================================================================

TMP_DIR=`echo $TMP_DIR/$$`
mkdir $TMP_DIR

# ================================================================================
# 1. Evaluate AB
# ================================================================================

AB=$1

RECIPE=`grep RECIPE             $DFO_AB_DIR/$AB | awk '{print $2}'`
RAW_TYPE=`grep RAW_TYPE         $DFO_AB_DIR/$AB | awk '{print $2}'`

# ================================================================================
# 2. Execute midas job for all product files
# ================================================================================

grep PRO_CATG $DFO_CONFIG_DIR/config.gipipeline | grep -v "^#" | grep $RECIPE | grep $RAW_TYPE | awk '{print $3}' | sort -u > $TMP_DIR/product_list

if [ ! -s $TMP_DIR/product_list ]
then
	echo "***ERROR: no products defined. Check $DFO_CONFIG_DIR/config.gipipeline!"
	exit
fi

for FILE in `cat $TMP_DIR/product_list`
do
	if [ ! -s $FILE ]
	then
		echo "***ERROR: $FILE expected but not found. Check out!"
		exit
	fi

	chmod u+w $FILE

	PRO_CATG=`grep $FILE 	$DFO_CONFIG_DIR/config.gipipeline  | grep -v "^#" | grep $RECIPE | grep $RAW_TYPE | awk '{print $5}'`

	replacekey -p "HIERARCH ESO PRO CATG" -v $PRO_CATG -c "Pipeline product category" $FILE
done 

rm -r $TMP_DIR
