#!/bin/bash
##########################################################
#touchrec -- Recursively "touches" files in a directory  #
#Ackn:       Written for TAG (Linux Gazette) :-)         #
#Version:    Version 1.0 (first draft)                   #
#Author:     Created by Thomas Adam                      #
#Date:       Saturday 15 June 2002, 16:58pm BST          #
#Contact:    thomas_adam16@yahoo.com                     #
##########################################################

#Declare Variables
bname=$(basename $0)   #Basename of program (path stripped)
curr_dir=$(pwd)	       #Current dir
dironly=0              #-d off
filesonly=0   	       #-f off
quiet=0       	       #-q off
toplevel=    	       #-l off
redir=$(tty)  	       #verbosity redirection tag
version="$bname: Created by Thomas Adam, Saturday 15 June 2002, 16:58pm BST,
Version 1.0"

#Start Procedures

#Help Procedure
help_user() 
{
echo "
$bname usage: [-s directory path] [-q] [-d] [-f] [-t] [-h] [-v]

where:

-s (optional starting directory, default is 'pwd')
-q (quiet mode -- surpresses verbosity)
-d (only touch directories)
-f (only touch files)
-t (touches the top-level directory, i.e. '.')
-h (prints this message)
-v (version of program)

Issue command \"man \\1 touchrec\" for full documentation
"

exit 0
}

run_default ()
{
  for lists in $(find ${curr_dir} ${toplevel} -depth 2> /dev/null); do
    #If it's a directory....
    [ -d $lists ] && {
      #All directories -- yes
      [ $dironly = 0 ] && {
        #Just the files? -- continue to the next instance of a loop, if so
      	[ $filesonly = 1 ] && {
	  continue
	}
      	echo "touching dir $lists/" >$redir && touch -c $lists
	continue
      } || [ $dironly = 1 ] && {
      	#then we are only checking for directories
	echo "touching dir $lists/" >$redir && touch -c $lists
      }
    #This time check for files...
    } || [ $dironly = 0 ] && {
      [ -f $lists ] && {
      	[ $filesonly = 1 ] && {
	  #Only checking for files....
      	  echo "touching files $lists" >$redir && touch -c $lists
      	  continue
      	} || [ $filesonly = 0 ] && {
	   #As a result of no flags passed at run-time, this executes :-)
           echo "touching files $lists" >$redir && touch -c $lists
      	} 
      }
    }   
  done
}

#Main 

#Check for presense of command-line switches
if [ "$#" = 0 ]; then
  echo "No command-line args given"
  curr_dir=$(pwd)

else

  while getopts ":hqlfvdts: " opts; do
    case $opts in
      d )
      	#Only Check for Directories
	dironly=1
      ;;

      q )
      	#Quiet -- surpresses verbosity to console
      	quiet=1
      	redir="/dev/null"	
      ;;

      f )
      	#Only check for files, no directories
      	filesonly=1
      ;;
      
      t )
      	#Only process the top-level directory "."
      	toplevel="-maxdepth 1"
	#echo $toplevel  #for debugging purposes
	;;
	
      s )
	#Get path as specified
	#If $optarg is blank, print help_user()
	
	[ $OPTARG = "" ] && {
	  echo "No Parameter Given"
	  help_user
	} || curr_dir=${OPTARG}
      
      ;;
      
      h )
      	#Print help message
	help_user
      ;;
      
      v )
      	#Prints the version
	echo $version
      	exit 0
      ;; 
      
      -* | * )
        #Any other options -- ignore
	help_user
      ;;
           
    esac
  done
fi

#Process optional commands...
shift $(($OPTIND - 1))

#Start main procedure -- once all options processed.  
run_default
