#!/bin/bash

help () {
cat <<ENDS
$0 v1.0 -- scan & exec directory recursive
Require: perl
-h  -- this help
-d  -- directory name. default=`pwd`
-f  -- name temp output file. default=/tmp/scan_dir.out
-e  -- program 4 exec file. default='echo ~file~'.
       substring ~file~ must be include in params (require)
-m  -- mask file must be perlregexp. default is any symbol.
Usage: \$ $0 -m '(\..bz2)$'
Copyleft GNU GPL Bootmaker SoftWARe 1999-2004
ENDS
}

SDIR=`pwd`
SFILE="/tmp/scan_dir.out"
SEXEC="echo ~file~"
SMASK="*"

if [ -z "$1" ]; then
  help
  exit
fi
while [ -n "$1" ];
do
  case $1 in
    -h) help; exit ;;
    -f) shift; SFILE=$1 ;;
    -d) shift; SDIR=$1 ;;
    -e) shift; SEXEC=$1 ;;
    -m) shift; SMASK=$1 ;;
    -*) help; exit ;;
  esac
  shift
done

>$SFILE
for i in `find $SDIR -depth -print`; do
  if ! test -d $i; then echo $i >> $SFILE; fi
done

perl -w <<STSCRIPT
open(FH,"$SFILE") or die "Cannot open $SFILE";
\$s=join("",<FH>);
close(FH);
\$s=~ s/\n+/\n/g;
open(FH,">$SFILE.exec");
print FH "#!/bin/sh\n";
foreach \$i (split(/\n/,\$s)) {
 if (\$i =~ /$SMASK/ ) { \$j="$SEXEC";
   \$j=~ s/~file~/\$i/g; print FH "\$j\n";
 }
}
close (FH);
exit;
STSCRIPT

chmod 755 $SFILE.exec
$SFILE.exec
rm -f $SFILE $SFILE.exec

exit


