#!/bin/bash

# -------------------------------------------------------
# RPC Project :
# RPC_get_data_CLUSTA_VTL1 : get VTL1 data in RFF database
#                            search also .gz files
# P. Robert, LPP, 2014-11-14, rev Nov 2020
# -------------------------------------------------------

appli=`basename $0`
appli36=`echo "$appli                                 " | cut -c1-36`
Narg=5

if( (test $# = 1 ) && (test $1 = -h ) ) ; then hh=1 ; else hh=0 ; fi

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : get CLUSTER/STA VTL1"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   SatNum year month day BitRate"
   echo "    ex: $appli 3 2001 09 23 NBR"
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi

datim1=`date +%F'  '%H':'%M':'%S `
julsec1=`date +%s`

SatNum=$1
year=$2
month=$3
day=$4
BitRate=$5

# Spacecraft test
# ---------------

if ( (test $SatNum -lt 1 ) || (test $SatNum -gt 4 ) )
   then 
        echo "$appli : S/C # must be >=1 and =< 4"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 2
fi

# BitRate test
# ------------

if ( (test $BitRate != NBR) && (test $BitRate != HBR) )
   then 
        echo "$appli : Bitrate must be only NBR or HBR"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 3
fi

dir=$R_RFF_data_CLU/STA/VTL1/$BitRate/$year/$year"_"$month

echo " -------------------------------------------------------------------"
echo " RPC_get_data_CLUSTA_VTL1 : get VTL1 from RFF CLUSTER data base "
echo "                            Day choosen : $*"
echo " search files in :"
echo " $dir"
echo ""

# cp VTL1 file from data base to current directory
# ------------------------------------------------

if test ! -d $dir
   then echo " *** non-existing directory in RFF database, so no files found"
        echo " => you have to download DWF cef file by 'RPC_download_data_CLUSTA_DWF_oneday_t1t2'"
        echo "    then use 'RPC_CLUSTA_DWF_cef_to_rff' "
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 4
fi

gz=0

ls $dir/CLU$SatNum*STA*VTL1*$year$month$day*.rff > toto1_$$.tmp 2> /dev/null

if test $? != 0 
   then echo " *** No rff files found ! search for gz files..."
   ls $dir/CLU$SatNum*STA*VTL1*$year$month$day*.rff.gz > toto1_$$.tmp 2> /dev/null
   if test $? != 0 
   then echo " *** No rff.gz files found ! "
        echo
        echo " =>  you have to download cef file by RPC_CLUSTA_download_data_CWF_oneday_t1t2"
        echo "     then use 'RPC_CLUSTA_CWF_cef_to_rff' "
        echo
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        rm toto1_$$.tmp
        exit 5
   else gz=1
   fi
fi

if test -f get_data_CLUSTA_VTL1.out ; then rm get_data_CLUSTA_VTL1.out ; fi
touch get_data_CLUSTA_VTL1.out           
   
# temporary exec shell files   
sed "s/^/basename /" toto1_$$.tmp > toto2_$$.tmp
sed -e "s/basename/cp/" -e "s/$/ ./" toto2_$$.tmp > toto3_$$.tmp
sh toto2_$$.tmp | sed "s/^/ls -l /" > toto4_$$.tmp

echo " files found: " 
sh toto2_$$.tmp | sed "s/^/ /"

echo
echo " copy in current directory"
echo " Please wait..."
echo
sh toto3_$$.tmp

echo " files copied:"
sh toto4_$$.tmp 2> /dev/null

if test $? = 0
   then echo " file copied in current directory"
        if test $gz = 1 ; then echo " you have to gunzip file by 'gunzip -c file_name.gz > ./file_name'" ; fi
        sh toto2_$$.tmp >> get_data_CLUSTA_VTL1.out
   else echo " error on file copy"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        rm toto1_$$.tmp toto2_$$.tmp toto3_$$.tmp toto4_$$.tmp
        exit 6
fi 

# change last access date
sed "s/^/touch -a /" toto1_$$.tmp > toto5_$$.tmp 
sh toto5_$$.tmp 2> /dev/null

rm toto*_$$.tmp 

echo "name of copied files are in get_data_CLUSTA_VTL1.out"
   
# end of command
# --------------

datim2=`date +%F'  '%H':'%M':'%S `
julsec2=`date +%s`
diff=`expr $julsec2 - $julsec1`
diffm=`echo "scale=2; $diff/60." | bc `

echo ""
echo " Starting time $appli : $datim1"
echo " Ending   time $appli : $datim2"
echo " Duration      $appli : $diff sec.  ($diffm mn.)"

echo
echo " $appli36 : NORMAL TERMINATION - time exe= $diff s." >&2

echo " -------------------------------------------------------------------"

