#!/bin/bash

# -------------------------------------------------------
# RPC Project :
# RPC_get_data_GEOGRD : get ULF VTL1 or VTL2 data in RFF database
#                       search also .gz files
# P. Robert, LPP, 2021
# -------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : get GEOGRD/ULF"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   Level year month day"
   echo "    ex: $appli VTL2  1977 07 06"
   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`

level=$1
year=$2
month=$3
day=$4

# Level test
# ---------------

if ( (test $level != VTL1 ) && (test $level != VTL2 ) )
   then 
        echo "$appli : *** Level=$level"
        echo "$appli : *** Level must be VTL1 or VTL2"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 1
fi

sta='???'
if test $year = 1977
   then sta='HUSAFELL'
fi

if test $year = 1980
   then sta='KITDALEN'
fi

if test $year = 1981
   then sta='KITDALEN'
fi

if test $year = 1982
   then sta='SKIBOTN'
fi

sta3=`echo $sta | cut -c1-3`

dir=$R_RFF_data_GEOGRD/ULF/$level/$year/$year"_"$month

echo " -------------------------------------------------------------------"
echo " $appli : get ULF $level from RFF GEOGRD data base "
echo "                            Day choosen : $*"
echo " search files in :"
echo " $dir"
echo ""

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

if test ! -d $dir
   then echo " *** non-existing directory in RFF database, so no files found"
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        exit 4
fi

gz=0

echo "search file $sta3"_"$level"_"$year$month$day.rff ..."
ls $dir/$sta3"_"$level"_"$year$month$day.rff > toto1_$$.tmp 2> /dev/null

if test $? != 0 
   then echo " *** No rff files found ! search for gz files..."
   ls $dir/$sta3"_"$level"_"$year$month$day.rff.gz > toto1_$$.tmp 2> /dev/null
   if test $? != 0 
   then echo " *** No rff.gz files found ! "
        echo
        echo " $appli36 : *** ERROR ! Command aborted." >&2
        rm toto1_$$.tmp
        exit 5
   else gz=1
   fi
fi

if test -f get_data_GEOGRD_$level.out ; then rm get_data_GEOGRD_$level.out ; fi
touch get_data_GEOGRD_$level.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_GEOGRD_$level.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_GEOGRD_$level.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 " -------------------------------------------------------------------"

