#!/bin/bash

# ----------------------------------------------------------------------
# RPC Project :
# donne pour le directory et tous les sous-directories la taille totale
# en octets et visualise l'arborescence.
# (somme de tous les fichiers, sans la taille reservee pour le directory
#  qui est systeme-dependant).
# P. Robert, Janvier 2007
# revu P.R. Novembre 2009, pour portabilite linux-Solaris
# adaptation du RPC_dir_size_tree avec l'executable dir_size_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 size (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

if test -d $nomdir
   then
      echo "Starting point:  $nomdir"
   else
      echo "*** Directory $nomdir does not exist"
      echo "*** $appli aborted !"
      exit 2
fi

echo "Please wait ..."

# remplissage du fichier des infos pour l'executable

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

if test $Mo = No
   then
      RPC_dir_size_tree  $nomdir     >>dir_size_tree.tmp
   else
      RPC_dir_size_tree  $nomdir $Mo >>dir_size_tree.tmp
fi

# exploitation du fichier et creation du fichier resultat

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

# remove RPC_ from RPC command

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

# run appli.exe 

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

rm dir_size_tree.tmp

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