#!/bin/bash

# ------------------------------------------------------------
# RPC Project :
# RPC_check_dirname_tree: Check dir name in the tree having blank
# works without argument
# P. Robert, LPP, 2015-03-01
# ------------------------------------------------------------
if( (test $# = 1 ) && (test $1 = -h ) ) ; then hh=1 ; else hh=0 ; fi
Narg=1
if test $# -gt 1  || test $hh = 1
   then
   echo "$appli : Check dir name in the tree having blank in the name"
   echo "$appli   require 0 or 1 argument(s)"
   echo "$appli "
   echo "$appli dir"
   echo ""
   if test $hh = 1 ; then exit 0 ; fi
   echo " $appli36 : *** ERROR ! Command aborted." >&2
   exit 1
fi

echo " -------------------------------------------------------------------"
echo " Check dir name in the tree having blank in the name"
echo " -------------------------------------------------------------------"

if test $# = 1
   then dir=$1
   else dir=.
fi

find $dir -type d | grep " " > toto.tmp
cat toto.tmp

nb=`cat toto.tmp | wc -l`
echo 
echo "There is $nb directories having blank in the name" >&2

if test $nb = 0
   then
       rm toto.tmp
       exit 0
fi

echo
echo "Do you want to replace blank by '_' ? (y/n)" >&2
read yeye
if test $yeye = y
   then 
       echo "ARE YOU SURE ? (y/n)" >&2
       read yeye
       if test $yeye = y
          then
             echo "OK"
             sed "s/ /_/g" toto.tmp > toto2.tmp
             sed -e 's/^/mv "/' -e 's/$/"/' toto.tmp > toto1.tmp
             paste toto1.tmp toto2.tmp > toto3.tmp
             bash toto3.tmp
             echo "done..." >&2
             sed "s/^/ls -dl /" toto2.tmp > toto4.tmp
             bash toto4.tmp
          else
             echo "OK, Bye..." >&2
          fi
   else
     echo "Bye..."        
fi
rm toto*.tmp
echo    
echo " $appli36 : NORMAL TERMINATION - time exe= $diff s." >&2 
echo
echo " -------------------------------------------------------------------"

