#!/bin/bash

# -------------------------------------------------------
# RPC Project :
# Run RPC_convert_names of a given directory
# P. Robert, ScientiDev, Dec. 2021
# -------------------------------------------------------

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 : Save recursively a directory in another one"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   /data/robert/robib  /media/robert/robib " 
   echo "               path must be absolute path"
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi

# path must be absolute path

pcar=`echo $1 | cut -c1`

if test $pcar != /
   then echo "source directory $1"
        echo "*** pathname must begin by '/'"
   exit 3
fi

pcar=`echo $2 | cut -c1`

if test $pcar != /
   then echo "target directory $2"
        echo "*** pathname must begin by '/'"
   exit 3
fi

# directories must be existing

if test -d $1
   then echo " "
   else echo
        echo "*** directory $1 does not exist"
        exit 1
fi

if test -d $2
   then echo " "
   else echo
        echo "*** directory $2 does not exist"
        exit 2
fi

# name directory of source and target must be the same

dir_sou=`basename $1`
dir_sav=`basename $2`

if test $dir_sou != $dir_sav
   then echo "file name of target directory must be the same as source"
        echo "Only path must be different, ex:"
        echo "RPC_dir_save /home/robert/toto /data/robert/toto"
   exit 1
fi

# go after check

echo
echo "the target directory $2 will be erased by "
echo "the source directory $1 "
echo "do you want to continue? (y/n)"
read yeye
if test $yeye = y
   then echo "OK, Go..."
   else exit 2
fi


echo " -------------------------------------------------------------------"
echo "$appli $*"
echo " -------------------------------------------------------------------"
echo ' '
 

path_sou=$1 #ex: /data/.../.../dirtoto
path_sav=$2 #ex: /media/robert/Toshiba/dirsave/dirtoto

nam_sou=`dirname $path_sou`
nam_sav=`dirname $path_sav`

#echo "dir_sou=$dir_sou"
#echo "nam_sou=$nam_sou"


if test $nam_sou/$dir_sou != $path_sou
   then 
      echo $nam_sou/$dir_sou
      echo $path_sou
      echo '**** pb !!!!'
      exit 9
fi

echo
echo "---------------------------------------------------------------------"
echo "Mise a jour du dir save $path_sav"
echo "---------------------------------------------------------------------" 
echo

     
#long=1
 long=0

if test -d $path_sav
   then
     # liste des fichiers sou et sav existants
     find $path_sou -type f | sort > liste_sou.txt
     find $path_sav -type f | sort > liste_sav.txt
     
     grep ' ' liste_sou.txt  > toto_sou.tmp
     grep ' ' liste_sav.txt  > toto_sav.tmp
     
     for symb in  \) \( \! \% \' \"  \]  \\^ \&
         do
     grep "$symb" liste_sou.txt  >> toto_sou.tmp
     grep "$symb" liste_sav.txt  >> toto_sav.tmp
     done
   
     nbfaux1=`wc -l toto_sou.tmp | sed "s/toto_sou.tmp//"`
     nbfaux2=`wc -l toto_sav.tmp | sed "s/toto_sav.tmp//"`
     
     if test $nbfaux1 != 0
        then
        echo
          echo "*** $nbfaux1 fichiers de la source ont un nom incompatible avec Linux"
          echo '    please correct using RPC_convert_names'
          echo
          cat toto_sou.tmp
          #rm  toto_sou.tmp
          exit 8
     fi
     
     if test $nbfaux2 != 0
        then
        echo
          echo "*** $nbfaux2 fichiers de la sauvegarde ont un nom incompatible avec Linux"
          echo '    please correct using RPC_convert_names'
          echo
          cat toto_sav.tmp
          #rm  toto_sav.tmp
          exit 7
     fi           
     
     sed "s%$nam_sou%%" liste_sou.txt > toto1.tmp
     sed "s%$nam_sav%%" liste_sav.txt > toto2.tmp
     cat toto1.tmp  > toto3.tmp
     cat toto2.tmp >> toto3.tmp
     sort toto3.tmp > toto4.tmp

# -------------------------------------------------------------------------------------------------
     echo "  I- action sur les fichiers deja presents dans le save"
     uniq -d toto4.tmp > a_voir.tmp
     wc -l a_voir.tmp | sed -e "s/a_voir.tmp/ fichiers deja present dans le save/" -e "s/^/     /"
     echo "     pour ces fichiers, on ne met a jour que ceux qui sont different de la source"
     echo "     sauf si leur date est plus récente"
     sed "s%.*%cmp $nam_sou& $nam_sav&%" a_voir.tmp > exe1.tmp
     sh exe1.tmp > exe2.tmp
 
     if test -s exe2.tmp
        then 
          sed -e "s/^/cp -pruv /" -e "s/sont différents.*//" exe2.tmp > exe3.tmp
          wc -l exe3.tmp | sed -e "s/exe3.tmp/ a recopier/" -e "s/^/     /"
          echo "     recopie..."
          if test $long = 1 ; then cat exe3.tmp ; fi
          sh exe3.tmp
          echo "     done!"
        else
         echo "     tous les fichiers du save deja presents sont identique a la source"
     fi
     echo

# -------------------------------------------------------------------------------------------------
     echo " II- action pour les fichiers qui n'ont pas de version dans le save"
     diff toto1.tmp toto2.tmp | grep "<" | sed -e "s%< %%" -e "s%.*/%mkdir -p $nam_sav& ;; &%" \
                                               -e "s%;;.*% ; cp -pr $nam_sou& $nam_sav&%" -e "s%;; %%g" > a_recop.tmp
     wc -l a_recop.tmp | sed -e "s/a_recop.tmp/ fichiers a recopier/" -e "s/^/     /"
     echo "     recopie..."
     if test $long = 1 ; then cat a_recop.tmp ; fi
     sh a_recop.tmp
     echo "     done!"
     echo

# -------------------------------------------------------------------------------------------------
     echo "III- action pour les anciens fichiers du save non presents dans la source"
     echo "     donc a eliminer"
     echo
     diff toto1.tmp toto2.tmp | grep ">"| sed "s%> %rm $nam_sav%" > a_jeter.tmp
     wc -l a_jeter.tmp | sed -e "s/a_jeter.tmp/ fichiers a jeter/" -e "s/^/     /"
     echo "     removing old files..."
      if test $long = 1 ; then cat a_jeter.tmp ; fi
     sh a_jeter.tmp
     echo "     done!"
     echo
     
   else
   echo "le directory $path_sav n'existe pas, on recopie tout"
   echo "     recopie..."
   cp -pr $path_sou $path_sav
   echo "     done!"
fi
# -------------------------------------------------------------------------------------------------
rm -f a_jeter.tmp  a_recop.tmp  a_voir.tmp  exe1.tmp  exe2.tmp  exe3.tmp  toto1.tmp  toto2.tmp  toto3.tmp  toto4.tmp

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