#!/bin/bash

# -----------------------------------------------------------------
# RPC Project :
# Run RPC_CLUSTA_put_cef_database: put file in the right place in data base
# P. Robert, LPP, 2013-02-15
# -----------------------------------------------------------------

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 file in the right place in data base"
   echo "$appli   require $Narg argument(s)"
   echo " "
   echo "example:"
   echo "$appli C2_CP_STA_DWF_HBR__20040130_164700_20040130_164800.cef   (or cef.gz)"
   echo "$appli C2_CP_FGM_5VPS__20010923_090000_20010923_110000.cef      (or cef.gz)"
   echo "$appli C1_CP_AUX_POSGSE_1M__20010126_103000_20010126_120000.cef (or cef.gz)"
   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

# test d'existence 

if ! test -f $1
   then
       echo "file $1 does not exist"
       echo ""
       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 != cef
   then
   echo " *** only cef can be put in CEF data base"
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 2
fi

# test sur le nom

cara1=`echo $f1 | cut -c1`
cara2=`echo $f1 | cut -c2`
str36=`echo $f1 | cut -c3-6`
exper=`echo $f1 | cut -c7-9`


if (test $cara1 != C ) || (test $str36 != _CP_ ) ; then err1=1 ; else err1=0 ; fi
if (test $cara2 = 1 ) || (test $cara2  = 2 ) || (test $cara2 = 3 ) || (test $cara2 = 4) ; then err2=0 ; else err2=1 ; fi
if (test $str36 != _CP_ ) ; then err3=1 ; else err3=0 ; fi

if (test $err1 != 0) || (test $err2 != 0 ) || (test $err3 != 0 ) 
   then
   echo " file name begin by : $cara1$cara2$str36$exper"
   echo " *** file name must begin as :"
   echo "C1_CP_"
   echo "C2_CP_"
   echo "C3_CP_"
   echo "C4_CP_"
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 3
fi

if (test $exper != STA ) && (test $exper != FGM ) && (test $exper != AUX )
      then
   echo " file name begin by : $cara1$cara2$str36$exper"
   echo " *** file name must begin as:"
   echo "C2_CP_STA"
   echo "C2_CP_FGM"
   echo "C1_CP_AUX"
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 4
fi 

# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# STA

if test $exper = STA
   then
    echo
    echo " -------------------------------------------------------------------"
    echo "$appli : put file in the right place in data base"
    echo "file to move: $file"
    echo "Cluster $cara2 $ext file"
    
    
    level=`echo $f1 | cut -c11-13` 
    if (test $level != DWF ) && (test $level != CWF ) 
       then
       echo "*** bad level $level"
       echo " *** file name must be as :"
       echo "C2_CP_STA_DWF_HBR__20040130_164700_20040130_164800.cef (or cef.gz) or"
       echo "C2_CP_STA_CWF_ISR2__20010923_090000_20010923_110000.cef"
       echo " $appli36 : *** ERROR ! Command aborted." >&2
       exit 5
    fi
    
    if (test $level = DWF )
       then 
          mode=`echo $f1 | cut -c15-17`
          echo $level $mode
          if (test $mode != NBR ) && (test $mode != HBR ) 
             then
             echo "*** bad mode $mode"
             echo " *** file name must be as :"
             echo "C2_CP_STA_DWF_HBR__20040130_164700_20040130_164800.cef (or cef.gz)"
             echo " $appli36 : *** ERROR ! Command aborted." >&2
             exit 6
          fi
          dir=$R_CEF_data_CLU/STA/$level/$mode
       else 
          repere=`echo $f1 | cut -c15-17`
          if (test $repere != GSE ) && (test $repere != ISR ) 
             then
             echo " *** file name must be as :"
             echo "C2_CP_STA_CWF_GSE_20010923_090000_20010923_110000.cef (or cef.gz) or"
             echo "C2_CP_STA_CWF_ISR2__20010923_090000_20010923_110000.cef (or cef.gz)"
             echo " $appli36 : *** ERROR ! Command aborted." >&2
             exit 7
          fi
          if (test $repere = ISR ) ; then repere=ISR2 ; fi
          dir=$R_CEF_data_CLU/STA/$level/$repere   
    fi

fi

# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# FGM

if test $exper = FGM 
   then 
   mode=`echo $f1 | cut -c11-14`
   if (test $mode != 5VPS) && (test $mode != FULL) && (test $mode != SPIN)
      then
      echo " file name begin by : $cara1$cara2$str36$exper"
      echo " *** file name must be as :"
      echo "C2_CP_FGM_FULL__20010923_090000_20010923_110000.cef (or cef.gz)"
      echo " $appli36 : *** ERROR ! Command aborted." >&2
      exit 8
   fi
   dir=$R_CEF_data_CLU/FGM/$mode
fi
   
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# POS

if test $exper = AUX 
   then 
   exper=`echo $f1 | cut -c7-13`
   if (test $exper != AUX_POS )
      then
      echo " file name begin by : $cara1$cara2$str36$exper"
      echo " *** file name must be as :"
      echo "C1_CP_AUX_POSGSE_1M__20010126_103000_20010126_120000.cef (or cef.gz)"
      echo " $appli36 : *** ERROR ! Command aborted." >&2
      exit 9
   fi
   dir=$R_CEF_data_CLU/POS
fi
   
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx     
   
# date extraction

dada=`echo $f1 | sed "s/.*__//"`
yy=`echo $dada | cut -c1-4` 
mm=`echo $dada | cut -c5-6` 

dir=$dir/$yy/$yy'_'$mm

echo "experi=$exper"
echo "target: $dir"

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

echo "mv $file in $dir"

if test -d $dir/$file ; then
   echo "File $file already existing in CEF_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

mv $file $dir

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

