#!/bin/sh
#
#  recurse.sh, a bash script by duus made on Tue Dec 18 17:59:45 EST 2007 
#  lives in: ~/Documents/Software/unixscripts//recurse/recurse.sh 
#  run binedit recurse to edit.  see binedit help for more information
#
_SCRIPT_NAME=recurse             
_SCRIPT_LOCATION=~/bin/             
#             
if [ "$1" = "version" ]                
	then                                 
	echo " recurse by duus"          
	echo " ~/Documents/Software/unixscripts//recurse/recurse.sh "
	echo "this program runs command on this folder and all subfolders"
	exit 1;
fi                                 
# --- normal script here ---  
if [ "$1" = "help" ]
	then
	echo "recurse CMD"
	echo " applies CMD to current folder and all subfolders, recursively"
	exit 1;
fi
_fn=0
function getfolders {
	_fn=`expr ${_fn} + 1`
	echo "${_fn}>>  `pwd`: ${_command}"
	${_command}
	_folders=`ls -fp | perl -i -pe 's/\ /\\\ /g' | perl -i -pe 's/,/\ /g' | grep "/"`
	#echo ${_folders}
	for folder in ${_folders}
		do
		_temp=`echo -n "$folder" | perl -i -pe 's/\///g'`
		if [ -e ${_temp} ]
			then
			cd $_temp
			getfolders
			cd ..
			else
			echo "`pwd`/${_temp} does not exist.  skipping...."
		fi
	done
}

#####

_command=$*
getfolders
# keep going to each subfolder





# -------------------------- 
# 
# end 'version' 
# 
# end recurse, ~/Documents/Software/unixscripts//recurse/recurse.sh
