#!/bin/bash

# -----------------------------------------------------------------
# RPC Project :
# Run RPC_GEOS_put_rff_database: put RFF file in the right place in data base
# P. Robert, ScientiDev, Dec 2020
# -----------------------------------------------------------------

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 : put RFF file in the right place in data base"
   echo "$appli   require $Narg argument(s)"
   echo "$appli   RFF_file"
   echo " "
   echo "examples:"
   echo "$appli  GEOS1_ULF_VTL2_19770713.rff"
   echo "$appli  GEOS1_MAG_VTL2_19770713.rff "
   echo "$appli  GEOS1_POS_VTL2_19771207.rff "
   echo ""
   echo "$appli  CLU3_STASC_VTL1_NBR_20010110_000000_20010110_020000.rff"
   echo "$appli  CLU3_FGM_VTL2_5VPS_20010110_000000_20010110_020000.rff "
   echo "$appli  CLU3_AUX_VTL2_POS_20010110_000000_20010110_235959.rff "
   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`

file=$1

if ! test -f $file
   then
       echo " *** file $file does not exist"
       echo " $appli36 : *** ERROR ! Command aborted." >&2
       exit 2
fi

ext=`echo $file | sed 's/.*\.//'`

if test $ext = gz 
   then f1=`basename $file .gz`
   echo $f1
   else
   f1=$file
fi

ext=`echo $f1 | sed 's/.*\.//'`
   
if test $ext != rff
   then
   echo " *** only .rff files can be put in RFF data base"
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 3
fi

# info RFF et test pour le chois/path de la base

set `RPC_info_rff $f1 s `

class=$1
mission=$2
satnum=$3
experi=$4
mode=$5
rep=$6
year=$7
month=$8

if test $class != VecTime
   then
   echo " *** rff $file is not a VecTime data file" 
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 4
fi


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

echo
echo " -------------------------------------------------------------------"
echo "$appli : put file in the right place in data base"
echo "file to move: $file"
echo "Mission     : $mission"
echo "Experi/mode : $experi/$mode"
echo "Repere      : $rep"
echo "Year/month  : $year/$month"

# path for each experiment 
# ------------------------

# 1) CLUSTER
#    -------

if test $mission = CLUSTER
   then
      if test $experi = STA 
         then
         if test $rep = SSW6RF 
            then 
                dir=$R_RFF_data_CLU/STA/VTL1/$mode
            else
                dir=$R_RFF_data_CLU/STA/VTL2/$mode/$rep
         fi
      fi

      if test $experi = FGM 
         then
         dir=$R_RFF_data_CLU/FGM/$mode
      fi

      if test $experi = AUX 
         then 
         if test $mode = POS
            then
            dir=$R_RFF_data_CLU/POS
         fi
         if test $mode = VIT
            then
            dir=$R_RFF_data_CLU/VIT
         fi
      fi
fi

# 2) GEOS
#    ----

if test $mission = GEOS
   then
      if test $experi = S300 
         then
         if test $rep = SRV 
            then 
                dir=$R_RFF_data_GEO/ULF/VTL2
            else
                dir=$R_RFF_data_GEO/ULF/VTL1
         fi
      fi
      
      if test $experi = S331 
         then
         dir=$R_RFF_data_GEO/MAG
      fi

      if test $mode = POSITIONS 
         then 
        dir=$R_RFF_data_GEO/POS
      fi
fi

# 3) MOBILE_STATION
#    ----

if test $mission = MOBILE_STATION
   then
      if test $experi = ULF 
         then
         if test $rep = NWV 
            then 
                dir=$R_RFF_data_GEOGRD/ULF/VTL2
            else
                dir=$R_RFF_data_GEOGRD/ULF/VTL1
         fi
      fi
fi

# ------------------------
# date extraction
# ------------------------

dir=$dir/$year/$year'_'$month
echo "target: $dir"

if test ! -d $dir 
             then mkdir -p $dir
fi

if test -d $dir/$file ; then
   echo "File $file already existing in RFF_data_base at:"
   echo $dir
   size=`wc -c $dir/$file`
   echo "size=$size"
   echo "Do you want to erase the old file ? (y/n)"
   read yeye
   if test $yeye != y ; then echo " $appli36: Cancelled" ; exit ; fi
   echo "delete old file and mv $file in $dir"
fi

echo "mv file..."
mv $file $dir
err=$?

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

if test $err != 0
   then
      echo done...
      echo ""
      echo " $appli36 : *** ERROR ! Command aborted." >&2
   else
      echo done...
      echo ""
      echo " $appli36 : NORMAL TERMINATION - time exe= $diff s." >&2
fi

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

