#!/bin/bash

# **************************** LICENSE START ***********************************
#
# Copyright 2012 ECMWF and INPE. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************


# ---------------------------------------------------------
# Script to run FLEXTRA from within Metview
# ---------------------------------------------------------

set -x

print_err()
{
	echo ${text_ERR}  $* >> "${f_LOG}"
} 

text_ERR="Script `basename $0` FAILED> "

#Get args

if [ $# -ne 5 ] ; then
    echo "Invalid number of arguments specified for script $0! (" $# " instead of 5)"
    exit 1
fi

d_WORK=$1
f_EXE=$2
type_FLEXTRA=$3
f_OUTPUT=$4
f_LOG=$5

#Define executable
if [ "$f_EXE" = "_UNDEF_" ] ; then
	exe_FLEXTRA=${MV_FLEXTRA_EXE}
else
	exe_FLEXTRA=${f_EXE}
fi


#-------------------------------
# Go to working directory 
#-------------------------------

if [ ! -d $d_WORK ] ; then   
   print_err "No working directory found: " $d_WORK
   exit 1
fi

cd $d_WORK

#-------------------------------
# Checks
#-------------------------------

if [ $type_FLEXTRA != "NORMAL" -a $type_FLEXTRA != "CET" -a $type_FLEXTRA != "FLIGHT" ] ; then   
   print_err "Invalid type specified:" $type_FLEXTRA
   print_err "Type must be: NORMAL, CET or FLIGHT!"
   exit 1
fi

if [ x$exe_FLEXTRA = "x" ] ; then   
   print_err "No FLEXTRA executable is defined. Please define it via env variable MV_FLEXTRA_EXE."
   exit 1
fi

if [ ! -f $exe_FLEXTRA ] ; then   
   print_err "No FLEXTRA executable found: " $exe_FLEXTRA
   exit 1
fi

if [ ! -x $exe_FLEXTRA ] ; then   
   print_err "FLEXTRA executable cannot be run! Permission is missing. " $exe_FLEXTRA
   exit 1
fi

f_PATHNAMES=pathnames
if [ ! -r $f_PATHNAMES ] ; then
   print_err "FLEXTRA pathnames file does not exist or cannot be read!"
   exit 1
fi

echo "Content of pathnames file:"
cat $f_PATHNAMES

#-------------------------------
#Run flextra
#-------------------------------

$exe_FLEXTRA >${f_LOG} 2>&1 
outCode=$?


#-----------------------------------
#  Check log
#-----------------------------------

if [ -f ${f_LOG} ] ; then
  if [ `grep -c -i WARNING $f_LOG` -ne 0 ] ; then
	outCode=255 
  elif [ `grep -c -i ERROR $f_LOG` -ne 0 ] ; then
	outCode=1
  elif [ $outCode -ne 0 ] ; then
	outCode=$outCode
  fi  
fi

#-----------------------------------
#  Concatenate the resulting files
#-----------------------------------

touch $f_OUTPUT
resCnt=0

if [ ${type_FLEXTRA} = "NORMAL" ] ; then

   for f in `ls` ; do
       if [ `echo $f | cut -c 1-3` = "TI_" ] ; then
	   cat $f >> $f_OUTPUT
           resCnt=$(($resCnt+1))
       fi
   done

   for f in `ls` ; do
       if [ `echo $f | cut -c 1-2` = "T_" ] ; then
	   cat $f >> $f_OUTPUT
           resCnt=$(($resCnt+1))
       fi
   done

elif [ ${type_FLEXTRA} = "CET" ] ; then	
	
   for f in `ls` ; do
       if [ `echo $f | cut -c 1-5` = "CETI_" ] ; then
	   cat $f >> $f_OUTPUT
           resCnt=$(($resCnt+1))
       fi
   done

   for f in `ls` ; do
       if [ `echo $f | cut -c 1-4` = "CET_" ] ; then
	   cat $f >> $f_OUTPUT
           resCnt=$(($resCnt+1))
       fi
   done

elif [ ${type_FLEXTRA} = "FLIGHT" ] 	; then
	
   for f in `ls` ; do
       if [ `echo $f | cut -c 1-8` = "FLIGHTI_" ] ; then
	   cat $f >> $f_OUTPUT
           resCnt=$(($resCnt+1))
       fi
   done

   for f in `ls` ; do
       if [ `echo $f | cut -c 1-7` = "FLIGHT_" ] ; then
	   cat $f >> $f_OUTPUT
           resCnt=$(($resCnt+1))
       fi
   done
fi


if [ $resCnt -gt 1 ] ; then
	cat $resCnt  > "multipleRes"
fi	 
	
exit $outCode
