#!/bin/tcsh

#############################################################  
# 							    #
# 							    #
# This script introduces a lot of concepts and constructs   #
# which you might need for Assignment I and other shell     #
# scripts in general. Just observe how the constructs are   #
# structured. Ensure that the file is executable after you  #
# download it. If not, type:				    #
# chmod u+x Ass1Ex.sh 					    #
# 							    #
# To run the script type:				    #
# ./Ass1Ex.sh						    #
# 							    #
# 							    #
#############################################################  
	
# Anything followed by a # is a comment and does not execute

# Infinite loop
# Note: while(1) always evaluates to true
# The program only quits when you choose 4

while(1)

# Directives for usage - Print Menu


if ($argv == 0) then
    echo "Process arguments missing"
    echo ""
else
    echo "SELECT THE FUNCTION YOU WANT TO EXECUTE:"
    echo ""
    echo "1. Hello World!"
    echo "2. Display paths of all the subdirectories of the current working directory"
    echo "3. Count the number of files with a given pattern"
    echo "4: Quit"
    echo ""
endif

# Accept user input

    set choice = $<

    # Note: $< means standard input.
    # choice is the variable to which the input is assigned

    # Note: The following lines provide the structure of the switch construct

    switch ($choice)
	
	# Option 1 gets executed here

	case 1:

	    echo "Hello World"
	    breaksw 

	    # Note: The breaksw statement ensures that switch does not roll over to other options
	    
	# Option 2 gets executed here

	case 2:

	    # Following is an example of assigning the output of a command to a variable
	    # Note: find command looks for the pattern, "*" in this case, in the names
	    # of the current working directory (.)

	    set fileList = `find . -name "*"`
	    
	    # Note: Back-quotes (``) are different from Single-quotes ('')
	    # This in effect stores the names of all files and subdirectories of the
	    # current working directory recursively starting at (.) in the variable
	    # fileList.

	    # The next step is to run through the fileList and check which of these
	    # are directories. (-d $file) does this for you.

	    foreach file ($fileList)
		if(-d $file) then
			echo $file
		endif
	    end
	    
	    breaksw

	    # Can you now write a version to count all the subdirectories of a directory? 


	# Option 3 gets executed here

	case 3:

	    echo -n "Please enter the pattern to search: "

	    # Note: -n option removes newline from echo's output

	    set pattern = $<

	    # Following is a way to use integer variables in csh

	    @ count = 0

	    echo ""
	    echo "Looking for pattern: " $pattern

	    # grep looks for the given pattern in all files of the current working directory
	    # Note: grep is not inherently recursive like find!
	    # Can you make grep recursive? Try the man pages :)

	    set fileList = `grep "$pattern" *`
	
	    foreach file ($fileList)

	    # Note: Be careful while altering the value of an integer
		@ count = $count + 1

	    end

	    echo ""
	    echo "Total matches found " $count

	    breaksw
	   
	# Option 4 gets executed here

	case 4:
	     
	    echo "Quitting Program. Thank you!"
	    exit # Terminates script
	    breaksw
	
	# If the user chooses an option other than the ones from 1 to 4

	default:
	    echo "Invalid arg! Please select only 1, 2, 3 or 4"
	    breaksw

    endsw # end switch

end # end while 

# end script
