#!/bin/sh
# NAME:		enterCommentsBulk
# PURPOSE:      Enter (raw or product) comments in bulk mode
# AUTHOR:       Reinhard Hanuschik / ESO
# DATE:         March 2010
# VERSIONS:     1.0 
#		1.0.1 -- $COMMENT_EDITOR uses $DFO_EDITOR (2011-04-21)
# PARAMETERS:   -m <mode> -d <date> -r <raw_type> | -h -v
# COMMENTS:	All product or raw files are found for given parameter set, and offered 
#		in text file for comment editing
#		Utility tool, distributed in utilPack
# ===============================================================
TOOL_VERSION=1.0.1

cat > $TMP_DIR/cp_usage <<EOT
USAGE: 	enterCommentsBulk -d <date> -m <mode> -r <raw_type> -c raw|prod 
			--edit comments for all files specified
	raw_type: 	either ALL_FILES, or valid RAW_TYPE as used for certifyProducts
	comment_type: 	raw or prod[ucts]
	mode, date:	as usual
Tool to be used only during a certifyProducts session.
EOT

if [ $# = 0 ]
then
	cat $TMP_DIR/cp_usage
	exit
fi

while getopts d:m:r:c:hv OPTION
do
	case "$OPTION" in
	 v ) echo "$TOOL_VERSION"; exit 0;;
	 h ) cat $TMP_DIR/cp_usage ; exit 0 ;;
	 d ) DATE=$OPTARG ;;
	 m ) MODE=$OPTARG ;;
	 r ) RAW_TYPE=$OPTARG ;;
	 c ) COMMENT_TYPE=$OPTARG ;;
	 ? ) cat $TMP_DIR/cp_usage ; exit 0 ;;
	esac
done

if [ Q$DFO_EDITOR = Q ]
then
	echo "***ERROR: you need to define \$DFO_EDITOR (e.g. vi, vim, emacs) in .dfosrc, then source and start again. Exit."
	exit
fi

COMMENT_EDITOR=`eval "echo $DFO_EDITOR $DFO_LST_DIR/RAWCOM/raw_comment_$DATE.txt"`

if [ Q$RAW_TYPE = Q ]
then
	echo "You have not specified a RAW_TYPE. Do you want to enter a comment for ALL files (Y/N) ? (N)"
	read ENTER_ALL
	if [ Q$ENTER_ALL != QY ]
	then
		echo "Think about it."
		exit 
	else
		RAW_TYPE=ALL_FILES
	fi
fi

if [ Q$MODE = Q ]
then
	echo "***ERROR: you must specify MODE (SCIENCE or CALIB). Exit."
	exit -1
fi

if [ Q$DATE = Q ]
then
	echo "***ERROR: you must specify DATE. Exit."
	exit -1
fi

if [ Q$COMMENT_TYPE = Q ]
then
	echo "***ERROR: you must specify COMMENT_TYPE (raw or prod). Exit."
	exit -1
fi

if [ ! -s $DFO_HDR_DIR/$DATE ]
then
	echo "***ERROR: no headers found for $DATE, can't create comment file."
	exit
fi

if [ ! -s $DFO_MON_DIR/AB_list_${MODE}_$DATE ]
then
	echo "***WARNING: no AB_list found for that date, can't do RAW_TYPE file selection. "
	echo "	  	  Select all raw files instead (Y/N) (N) ?"
	read SELECT_YN
	if [ Q$SELECT_YN != QY ]
	then
		echo "Think about it. Exit. "
		exit
	fi
	RAW_TYPE=ALL_FILES
fi

# check for existing comment file
case $COMMENT_TYPE in
 "raw"  ) COMMENT_FILE="$DFO_LST_DIR/RAWCOM/raw_comment_$DATE.txt"   ;;
 "prod" ) COMMENT_FILE="$DFO_LST_DIR/PRODCOM/prod_comment_$DATE.txt" ;;
esac

if [ $COMMENT_TYPE = prod ]
then
	COMMENT_EDITOR=`echo $COMMENT_EDITOR | sed "s/RAWCOM/PRODCOM/" | sed "s/raw_comm/prod_comm/"`
fi

if [ ! -s $COMMENT_FILE ]
then
	if [ $COMMENT_TYPE = "raw" ]
	then
		echo "#These comments will be entered upon terminating certifyProducts.
#All entries starting with '#', and empty comments, will be skipped.
#MODE	RAW_TYPE ARCFILE                          comment
#===========================================================" > $COMMENT_FILE
	else
		echo "#These comments will be entered upon terminating certifyProducts.
#All entries starting with '#', and empty comments, will be skipped.
#MODE	RUN_ID		OB_ID		DATE       PRODUCT                              Comment
#==============================================================================================" > $COMMENT_FILE
	fi	
fi

rm -f $TMP_DIR/ec_AB_list
if [ $RAW_TYPE != ALL_FILES ]
then
	grep " ${RAW_TYPE} " $DFO_MON_DIR/AB_list_${MODE}_$DATE | awk '{print $1}' > $TMP_DIR/ec_AB_list
else
	if [ -s $DFO_MON_DIR/AB_list_${MODE}_$DATE ]
	then
		cat $DFO_MON_DIR/AB_list_${MODE}_$DATE | awk '{print $1}' > $TMP_DIR/ec_AB_list
	else
		rm -f $TMP_DIR/check_ab 
		grep "^DATE" $DFO_AB_DIR/*ab | grep $DATE | sed "s/:DATE//" > $TMP_DIR/check_ab
		if [ -s $TMP_DIR/check_ab ]
		then
			grep "^DPR_CATG[[:space:]]*${MODE}" `cat $TMP_DIR/check_ab` | sed "s/:DPR_CATG.*//" > $TMP_DIR/ec_AB_list
		else
			echo "No $RAW_TYPE files found for $DATE. Exit."
			exit
		fi
	fi
fi

if [ ! -s $TMP_DIR/ec_AB_list ]
then
	echo "No $RAW_TYPE files found for $DATE. Exit."
	exit 0
fi

# extract raw files from ABs
echo "Finding entries from AB list ... "
if [ $COMMENT_TYPE = raw ]
then
	rm -f $TMP_DIR/ec_filelist
	for AB in `cat $TMP_DIR/ec_AB_list`
	do
		AB1=`basename $AB`
		if [ -s $DFO_AB_DIR/$AB1 ]
		then
			grep "^RAWFILE" $DFO_AB_DIR/$AB1 | awk '{print $2}' >> $TMP_DIR/ec_filelist
		elif [ -s $DFO_LOG_DIR/$DATE/$AB1 ]
		then
			grep "^RAWFILE" $DFO_LOG_DIR/$DATE/$AB1 | awk '{print $2}' >> $TMP_DIR/ec_filelist
		fi
	done
fi

# extract product file parameters from ABs
if [ $COMMENT_TYPE = prod ]
then
	rm -f $TMP_DIR/ec_filelist*
	for AB in `cat $TMP_DIR/ec_AB_list`
	do
		AB1=`basename $AB`
		if [ -s $DFO_AB_DIR/$AB1 ]
		then
			PROD_ROOT_NAME=`grep "^PROD_ROOT_NAME" $DFO_AB_DIR/$AB1 | awk '{print $2}'`
			RUN_ID=`grep "^OBS_PROG_ID" $DFO_AB_DIR/$AB1 | awk '{print $2}' | sed "s/(//" | sed "s/)//"`
			OBS_ID=`grep "^OBS_ID"      $DFO_AB_DIR/$AB1 | awk '{print $2}'`

		elif [ -s $DFO_LOG_DIR/$DATE/$AB1 ]
                then
			PROD_ROOT_NAME=`grep "^PROD_ROOT_NAME" $DFO_LOG_DIR/$DATE/$AB1 | awk '{print $2}'`
			RUN_ID=`grep "^OBS_PROG_ID" $DFO_LOG_DIR/$DATE/$AB1 | awk '{print $2}' | sed "s/(//" | sed "s/)//"`
			OBS_ID=`grep "^OBS_ID"      $DFO_LOG_DIR/$DATE/$AB1 | awk '{print $2}'`
		fi

		if [ $RAW_TYPE = ALL_FILES ]
		then
			EXT=.fits
		else
			EXT=`grep "^CALL_SYNTAX" $DFO_CONFIG_DIR/config.processQC | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $3}'`
		fi

		if [ Q$EXT = Q.fits ]
		then
			echo ${PROD_ROOT_NAME}${EXT} >> $TMP_DIR/ec_filelist
			echo "$MODE	$RUN_ID	$OBS_ID	$DATE ${PROD_ROOT_NAME}${EXT}" >> $TMP_DIR/ec_filelist1
		else
			echo ${PROD_ROOT_NAME}_${EXT} >> $TMP_DIR/ec_filelist
			echo "$MODE	$RUN_ID	$OBS_ID	$DATE ${PROD_ROOT_NAME}_${EXT}" >> $TMP_DIR/ec_filelist1
		fi
	done		
fi

TMPCOMM="$DFO_LST_DIR/tmpcomment"

# add new filenames 
for F in `cat $TMP_DIR/ec_filelist`
do
	F1=`basename $F`
	CHECK_EXIST=`grep " $F1" $COMMENT_FILE | grep "^$MODE" | sed "s/^.*$F1//" | sed "s/  *//"`
	if [ "Q$CHECK_EXIST" = Q ]
	then
		grep -v $F1 $COMMENT_FILE > $TMPCOMM
		if [ $COMMENT_TYPE = raw ]
		then
			echo "$MODE	$RAW_TYPE $F1">> $TMPCOMM
		elif [ $COMMENT_TYPE = prod ]
		then
			grep $F $TMP_DIR/ec_filelist1 >> $TMPCOMM
		fi
		mv $TMPCOMM $COMMENT_FILE
	fi
done

echo "
Now your editor will open, you can enter comments as you wish.
   - add comments in-line with ARCFILE names, no line breaks
   - the list of ARCFILE names has been read from the ABs
   - for better overview you may want to delete the ones for which you have no comments
     (but this is not required, they are auto-skipped when comments are ingested)
   - the comment file may have other entries (don't delete them).
Hit return to open the editor:"
read input
$COMMENT_EDITOR

echo "The comment file $COMMENT_FILE has been edited. Comments will be ingested by certifyProducts."
exit
