#!/bin/bash

# ----------------------------------------------------------------------
# RPC Project :
# donne pour le directory et tous les sous-directories la taille totale
# en octets, le nombre de fichiers et le nombre de sous-directories
# et visualise l'arborescence.
# (somme de tous les fichiers, sans la taille reservee pour le directory
#  qui est systeme-dependant).
# P. Robert, Janvier 2014
# revu P.R. Novembre 2009, pour portabilite linux-Solaris
# adaptation du RPC_dir_properties_tree avec l'executable dir_properties_tree.exe
#P.R. Fev 2014
# ----------------------------------------------------------------------

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

if (test $# = 1 ) 
   then
   if (test $1 = -h )
      then echo "$appli : Give directory properties (octets or Mo) for all the tree"
           echo "Usage: $appli"
           echo "       $appli Mo"
           echo "       $appli DIR"
           echo "       $appli DIR Mo"
           exit 1
      fi
fi

Mo=No
nomdir=.

if test $# != 0
   then
   if (test $# = 1 ) && (test $1  = Mo ) 
       then 
           Mo=Mo 
   fi
   
   if (test $# = 1 ) && (test $1 != Mo ) 
      then 
           nomdir=$1
   fi
   
   if (test $# = 2 ) && (test $2 = Mo )
      then 
           Mo=Mo
           nomdir=$1
   fi
fi


gene=`date +%Y%m%d'_'%H%M%S`

echo
echo "---------------------------------"
echo "Directories tree"
echo "---------------------------------"
echo
echo "Starting point:  $nomdir"
echo "Please wait ..."

# remplissage du fichier des infos pour l'executable

if test -f dir_properties_tree.tmp
   then rm dir_properties_tree.tmp
fi
datimc=`date +%F'  '%H':'%M':'%S `
echo $datimc  >dir_properties_tree.tmp
uname -n     >>dir_properties_tree.tmp
uname -sr    >>dir_properties_tree.tmp
echo $nomdir >>dir_properties_tree.tmp
find $nomdir -type d -print |wc -l >>dir_properties_tree.tmp
find $nomdir -type f -print |wc -l >>dir_properties_tree.tmp

if test $Mo = No
   then
      RPC_dir_properties_tree  $nomdir     >>dir_properties_tree.tmp
   else
      RPC_dir_properties_tree  $nomdir $Mo >>dir_properties_tree.tmp
fi

# exploitation du fichier et creation du fichier resultat

if test -f dir_properties_pretty_tree.txt
   then rm dir_properties_pretty_tree.txt
fi

# remove RPC_ from RPC command

appli=`echo $appli | cut -c5-`

# run appli.exe 

$RPC_DIR/bin/$appli.exe       > dir_properties_pretty_tree.txt

rm dir_properties_tree.tmp

cat dir_properties_pretty_tree.txt
echo
echo -----------------------------------------------------
echo copy of results are in dir_properties_pretty_tree.txt
echo -----------------------------------------------------
