#!/bin/bash

# -------------------------------------------------------
# RPC Project :
# Run RPC_check_rff of all RFF files of a given directory
# P. Robert, LPP, 2011-08-17
# -------------------------------------------------------

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

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

if test $# != $Narg || test $hh = 1
   then
   echo "$appli : compare recursively two directories"
   echo "$appli   require $Narg argument(s), ex:"
   echo "$appli   /media/robert/robib  /data/robert/robib dir_depth"
   echo "               path must be absolute path"
   echo "               dir_depth: 1 for short, > 1 for long"
   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

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


d1=`dirname $1`
d2=`dirname $2`

if test $3 = L
   then echo " Long option, enlarge screen..."
        echo
        find $1 -type d | sort > toto1.tmp
        find $2 -type d | sort > toto2.tmp
   else echo " Short option.."
        echo
        find $1 -maxdepth $3 -type d | sort > toto1.tmp
        find $2 -maxdepth $3 -type d | sort > toto2.tmp
fi


sed "s/^/RPC_dir_properties /" toto1.tmp > exe1.tmp
sed "s/^/RPC_dir_properties /" toto2.tmp > exe2.tmp

sh exe1.tmp > resu1.tmp
sh exe2.tmp > resu2.tmp

echo '------------------------------------------------' > toto1.tmp
echo '------------------------------------------------' > toto2.tmp

ls -dlgG --time=ctime $1 | cut -d' ' -f4- >> toto1.tmp
ls -dlgG --time=ctime $2 | cut -d' ' -f4- >> toto2.tmp

echo '------------------------------------------------' >> toto1.tmp
echo '------------------------------------------------' >> toto2.tmp
echo '' >> toto1.tmp
echo '' >> toto2.tmp


sed "s%$d1%%" resu1.tmp >> toto1.tmp
sed "s%$d2%%" resu2.tmp >> toto2.tmp

echo '------------------------------------------------' >> toto1.tmp
echo '------------------------------------------------' >> toto2.tmp

# longueur max de ligne

set `wc -l -L toto1.tmp`
lm1=$2
set `wc -l -L toto2.tmp`
lm2=$2


lm=`expr $lm1 + $lm2`
lmt=`expr $lm + 20`

echo ' '
pr -m -t -s"%" --width=$lmt toto1.tmp toto2.tmp | expand  -t 8 | sed "s/       %//"
echo ' '

rm toto1.tmp exe1.tmp toto2.tmp exe2.tmp
