#!/bin/sh
# NAME:		pgi_renameProducts
# PURPOSE:      plugin to restrict max. size of science filenames to 64 chars
# AUTHOR:       Reinhard Hanuschik 
# VERSION:      1.0 - 2007-09-13
# PARAMETERS:   none
# INSTRUMENT:	UVES (SCIENCE mode only)
# ================================================================================

# this plugin applies only for science files. It checks for length <= 64 chars since
# safIngest cannot digest filenames longer than that. For UVES SCIENCE files, such long
# filenames can be caused by exceptional OBS_IDs like "-1672260199" sometimes used 
# (incorrectly) by Paranal staff e.g. for observatory runs. In that case, this pgi cuts
# the first char (in this example the "-"). This creates a minor inconsistency with the
# "true" OBS_ID but there are no other options. 
# Note this strategy is highly UVES_specific and may be incorrect for other instruments!
# Example: UV_SFXE_-1672260199_2003-03-22T00:36:29.713_REDU860d2_1x1_10.fits ==>
#           UV_SFXE_1672260199_2003-03-22T00:36:29.713_REDU860d2_1x1_10.fits
# ================================================================================

if [ $MODE != "SCIENCE" ]
then
	exit 0
fi

echo "  calling pgi_renameProducts ..."

for F in `grep "^mv" rn_files | awk '{print $3}'`
do
	CHECK_LENGTH=`echo $F | wc -c`
	if [ $CHECK_LENGTH -gt 65 ]
	then
		OLD_OB=`echo $F | sed "s/_/ /g" | awk '{print $3}'`
		NEW_OB=`echo $OLD_OB | sed "s/^.//"`
		NEW_F=`echo $F | sed "s/$OLD_OB/$NEW_OB/"`
		cat rn_files | sed "s/$F/$NEW_F/" > rn_files1
		mv rn_files1 rn_files
		echo "***INFO: $F shortened to $NEW_F."
	fi
done

echo "  ... pgi_renameProducts finished."

exit
