#!/bin/bash

# -----------------------------------------------------------------
# RPC Project :
# Run RPC_list_rff_database: list of  rff files in rff data base for
#                            CLUSTER or GEOS
# P. Robert, Nov 2020
# -----------------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : list of all rff files in rff data base"
   echo "$appli   require $Narg argument(s)"
   echo "$appli   mission experiment"
   echo "Example:"
   echo "$appli CLUTER STA"
   echo "$appli CLUTER FGM"
   echo "$appli GEOS   ULF"
   echo "$appli GEOS   POS"
   echo "$appli GEOS   ALL"
   echo "$appli GEOGRD ULF"
   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`

mission=$1
experi=$2


if ( test $mission != GEOS ) && ( test $mission != CLUSTER ) && ( test $mission != GEOGRD )
   then
   echo " *** mission=$mission" 
   echo " *** mission must be only CLUSTER, GEOS or GEOGRD" 
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 2
fi

if test $mission = CLUSTER
   then
   DB=$R_RFF_data_CLU
   ALL="STA FGM POS"
   if (test $experi != STA ) && (test $experi != FGM ) && (test $experi != POS) && (test $experi != ALL)
   then
   echo " *** experi=$experi" 
   echo " *** experi must be only STA FGM or POS"
      echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 3
   fi
fi

if test $mission = GEOS
   then
   DB=$R_RFF_data_GEO
   ALL="ULF MAG POS"
   if (test $experi != ULF ) && (test $experi != MAG ) && (test $experi != POS) && (test $experi != ALL)
   then
   echo " *** experi=$experi" 
   echo " *** experi must be only ULF MAG or POS"
      echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 4
   fi
fi

if test $mission = GEOGRD
   then
   DB=$R_RFF_data_GEOGRD
   if (test $experi != ULF ) 
   then
   echo " *** experi=$experi" 
   echo " *** experi must be only ULF"
      echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 4
   fi
fi

echo
echo " -------------------------------------------------------------------"
echo "$appli : list of all $mission/$experi rff files in rff data base"
echo "path of rff database: $DB"

# -------------------------------
if test $experi = ALL
   then 
   list=$ALL
   else
   list=$experi
fi

for expe in $list
do

find $DB/$expe -name "*.rff"   > toto1_$$.tmp
nbfiles=`wc -l toto1_$$.tmp | sed "s/toto1.*//"`

if test $nbfiles = 0 ; then
   echo
   echo "there is no rff files in RFF_database_$mission"
   rm toto1_$$.tmp
   echo ""
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 0
fi

echo
echo "There is $nbfiles rff $expe files in RFF_database_$mission:"
echo 

sed "s/.*RFF_database/RFF_database/"  toto1_$$.tmp

# gz files

find $DB/$expe -name "*.rff.gz"   > toto1_$$.tmp 2> /dev/null

if test $? = 0 
   then 
     nbfiles=`wc -l toto1_$$.tmp | sed "s/toto1.*//"`
     if test $nbfiles != 0
        then
        echo
        echo "gz files:"
        echo
        sed "s/.*RFF_database/RFF_database/"  toto1_$$.tmp
     fi
fi

rm toto1_$$.tmp

done

# -------------------------------

echo done...
echo ""

# 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 " -------------------------------------------------------------------"

