#!/bin/bash

# -----------------------------------------------------------------
# RPC Project :
# Run RPC_purge_cef_database_CLUSTER: purge of  cef files in cef data base for
#                                    CLUSTER
# P. Robert, Jan 2021
# -----------------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : purge of all cef files in cef data base"
   echo "$appli   require $Narg argument(s)"
   echo "$appli   experi"
   echo "Example:"
   echo "$appli "
   echo "$appli STA"
   echo "$appli FGM"
   echo "$appli ALL"
   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`

experi=$1

DB=$R_CEF_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 POS or ALL"
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 3
fi

echo
echo " -------------------------------------------------------------------"
echo "$appli : purge of all $experi cef files in CLUSTER cef data base"
echo "path of cef database: $DB"

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

for expe in $purge
do

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

if test $nbfiles = 0
   then
   echo
   echo "==> There is no  $expe.cef   files in CEF_database_CLUSTER:"

   else
   echo
   echo "==> There is $nbfiles $expe.cef   files in CEF_database_CLUSTER:"
   echo "do you want to remove all these files ? (y/n)"
   read yeye
       if test $yeye = y
          then 
          echo "OK Go!"
          sed "s/^/rm /" toto1_$$.tmp > toto1b_$$.tmp
          sh toto1b_$$.tmp
          echo "Done! files are removed"
       else
       echo "OK, one do nothing"
       fi
fi
rm toto*_$$.tmp

# gz files

find $DB/$expe -name "*.cef.gz"   > toto1_$$.tmp 2> /dev/null
nbfiles=`wc -l toto1_$$.tmp | sed "s/toto1.*//"`

if test $nbfiles = 0
   then
   echo
   echo "==> There is no  $expe.cef.gz   files in CEF_database_CLUSTER:"

   else
   echo
   echo "==> There is $nbfiles $expe.cef.gz   files in CEF_database_CLUSTER:"
   echo "do you want to remove all these files ? (y/n)"
   read yeye
       if test $yeye = y
          then 
          echo "Go!"
          sed "s/^/rm /" toto1_$$.tmp > toto1b_$$.tmp
          sh toto1b_$$.tmp
          echo "Done! files are removed"
       else
       echo "OK, one do nothing"
       fi
fi
rm toto*_$$.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 " -------------------------------------------------------------------"

