#!/bin/bash

# generate filename without directory:
FILENAME=$(basename $0)
CAT=$(which cat)

print_help()
{
##########################################################################
$CAT <<EOF

  $0

  author:      Karl Voit, shellscript@Karl-Voit.at

  date:        2003-07-01

  version:     v0.1

  copyright:   GPL

  usage:       $FILENAME

  description: This script checks the structure of directories and files
               below the current directory and reports, if certain files
               and subdirectories exists (or not).

               Of course, this directories and files can be defined in
               whitelists (they should exist) and blacklists (they should
               not exist).

               Names of directories in the starting directory aren't 
               checked (in this version).

               Please note, that reported missing files from non-existent
               subdirectories aren't filtered out (yet).

               Please email me suggestions for improvements and errors!
               I am still learning how to write good shell-scripts *g*

EOF
##########################################################################
}

## 2do:
##      * 
##      * 



########################################################
########################################################
########################################################
##                                                    ##
##                  PERSONALISATION                   ##
##                                                    ##
########################################################
########################################################
########################################################



# subdirectories that SHOULD exist:
SUBDIRECTORIES_WHITELIST=( he he/images he/presentation taplan ta ta/images ta/presentation )

# subdirectories that SHOULD NOT exist:
SUBDIRECTORIES_BLACKLIST=( heuristic ta-plan think-aloud )


# files that SHOULD exist:
FILES_WHITELIST=( he/he.html taplan/taplan.html ta/ta.html )

# files that SHOULD NOT exist:
FILES_BLACKLIST=( he/he.htm taplan/taplan.htm ta/ta.htm )




# change to "done", if you changed all options above to _your_ needs!

CONFIGURED_="notdone"


########################################################




# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #

# --- normally you DON'T have to change anything below this line! --- #

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #




########################################################
########################################################
########################################################
##                                                    ##
##               hardcoded configuration              ##
##                                                    ##
########################################################
########################################################
########################################################


# change to "on" if you want to read the debug-messages (can be much) to trace
# down problems:
export DEBUG="off"


########################################################
########################################################
########################################################
##                                                    ##
##                   F U N C T I O N S                ##
##                                                    ##
########################################################
########################################################
########################################################

## ---------------------------------------------------------

myexit()
{
    doreport debug "function myexit($1) called"

    [ "$1" -lt 1 ] && echo "$FILENAME done."
    [ "$1" -gt 0 ] && echo "$FILENAME aborted with errorcode $1."

#optionally    [ "$1" -gt 0 ] && do_sound_error

    exit $1
}


## ---------------------------------------------------------


## check, if some files needed are not found
testiffound()
{
    doreport debug "function testiffound($1) called"

  if [ -z "$2" ]; then
    doreport debug "The tool \"$1\" is missing because \"$2\" is empty."
    doreport notify "The tool \"$1\" could not be located (missing?)"
    export SOMETHINGMISSING="yes"
  fi
}



## ---------------------------------------------------------

report()
{
## reports the parameter to the stdout

  echo
  echo "==============================================================="
  echo "                                                 $FILENAME"
  echo "$1"
  echo
  echo "==============================================================="
  echo

}



## ---------------------------------------------------------

debugthis()
{
## debugs the script
        #echo $FILENAME: DEBUG: $1
        echo "do nothing" >/dev/null
}



## ---------------------------------------------------------


doreport_internal_writestring()
{
## !! for use withing function "doreport" only !!
## prints out all strings on stdout

## 2do: error-msg written with { echo 1>&2 "text" } (stderr)

        echo
        echo "===$1============================================"
        echo "                                                 $FILENAME"


## FIXXME:
## 2DO: loop instead of this quick-hack!!!

        if [ ! -z "$2" ]; then
	    echo "$2"
        fi
        if [ ! -z "$3" ]; then
	    echo "$3"
        fi
        if [ ! -z "$4" ]; then
	    echo "$4"
        fi
        if [ ! -z "$5" ]; then
	    echo "$5"
        fi
        if [ ! -z "$6" ]; then
	    echo "$6"
        fi
        if [ ! -z "$7" ]; then
	    echo "$7"
        fi
        if [ ! -z "$8" ]; then
	    echo "$8"
        fi

        echo
        echo "===============================================================";
        echo

}



## ---------------------------------------------------------


doreport()
{
## reports the parameter to the stdout

## usage: (shortnote|notify|error|debug) string1 [string2] [string3] [...] [string7]

## NEEDS: doreport_internal_writestring


    case "$1" in

    "shortnote") 
	echo "$FILENAME: $2 $3 $4 $5 $6 $7";;

    "notify") 
	doreport_internal_writestring " notification ==" "$2" "$3" "$4" "$5" "$6" "$7";;

    "whitelist") 
	doreport_internal_writestring " whitelist =====" "$2" "$3" "$4" "$5" "$6" "$7";;

    "blacklist") 
	doreport_internal_writestring " blacklist =====" "$2" "$3" "$4" "$5" "$6" "$7";;

    "error") 
        doreport_internal_writestring " ERROR =========" "$2" "$3" "$4" "$5" "$6" "$7";;

    "debug") 
        ## debugs the script
        if [[ "$DEBUG" = "on" ]]; then echo "$TIMESTAMP $FILENAME: DEBUG: $2 $3 $4 $5 $6 $7"; fi;
        echo "do nothing" >/dev/null;;

    *)
        doreport_internal_writestring " INTERNAL ERROR " "An error occured, while calling function \"doreport\":" "The parameter that was given ($1) has no target/handle." "Aborting.";
        myexit 1;;

    esac
}




## ---------------------------------------------------------

precondition_asserts()
{
## * test for things that are needed
## * check, if caller = root-user
## * check, if everything was configured by the user
## * check, if a parameter is given:

      export SOMETHINGMISSING="no"


      TEST=`which test`
      testiffound test $TEST


    ## check, if any tool was NOT found
    doreport debug "CHECK: any tool missing?"
    if [ "$SOMETHINGMISSING" = "yes" ]; then
      print_help
      doreport error "One or more tool(s), that are needed are missing! (See output above!)" "Please make sure, that you those tools are installed properly and try again." "aborting."
      myexit 2
    fi



      
      ## check, if everything was configured by the user
      if [ ! "$CONFIGURED_" = "done" ]; then
        print_help
	doreport "error" "Please make sure, that you checked/modified all options" "to meet your requirements!" "(see section "PERSONALISATION" in file $0)"
        myexit 3
      fi
      
      
            

    doreport debug "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< all checks OK!"
    export PRECONDITIONS_CHECKED="yes"


}



## ---------------------------------------------------------

postcondition_asserts()
{

## * deletes temporary files
## * ...

##  if [ -z "???filename" ]; then
##    ???
##  fi

echo "donothing" >/dev/null

}



########################################################
########################################################
########################################################
##                                                    ##
##                S C R I P T                         ##
##                                                    ##
########################################################
########################################################
########################################################


## test for important stuff:
precondition_asserts "$1" "$2" "$3" "$4" "$4" "$5" "$6" "$7" "$8" "$9"



check_for_subdirectories()
{
  doreport debug "check_for_directories called using parameter1 as $1."

  INDEX=0
  while [ -n "${SUBDIRECTORIES_WHITELIST[$INDEX]}" ]
  do

    doreport debug "checking for subdirectory \"$1/${SUBDIRECTORIES_WHITELIST[$INDEX]}\""
    if [ ! -d "$1/${SUBDIRECTORIES_WHITELIST[$INDEX]}" ]; then
	doreport whitelist "Directory \"$1/${SUBDIRECTORIES_WHITELIST[$INDEX]}\" could not be found!"
    fi

    ((INDEX += 1))
  done

  INDEX=0
  while [ -n "${SUBDIRECTORIES_BLACKLIST[$INDEX]}" ]
  do

    doreport debug "checking for subdirectory \"$1/${SUBDIRECTORIES_BLACKLIST[$INDEX]}\""
    if [ -d "$1/${SUBDIRECTORIES_BLACKLIST[$INDEX]}" ]; then
	doreport blacklist "Wrong directory \"$1/${SUBDIRECTORIES_BLACKLIST[$INDEX]}\" found."
    fi

    ((INDEX += 1))
  done

  doreport debug "check_for_directories finished.."
}

check_for_files()
{
  doreport debug "check_for_files called."


  INDEX=0
  while [ -n "${FILES_WHITELIST[$INDEX]}" ]
  do

    doreport debug "checking for file \"$1/${FILES_WHITELIST[$INDEX]}\""
    if [ ! -f "$1/${FILES_WHITELIST[$INDEX]}" ]; then
	doreport whitelist "File \"$1/${FILES_WHITELIST[$INDEX]}\" could not be found!"
    fi
    ((INDEX += 1))
  done


  INDEX=0
  while [ -n "${FILES_BLACKLIST[$INDEX]}" ]
  do

    doreport debug "checking for file \"$1/${FILES_BLACKLIST[$INDEX]}\""
    if [ -f "$1/${FILES_BLACKLIST[$INDEX]}" ]; then
	doreport blacklist "Wrong file \"$1/${FILES_BLACKLIST[$INDEX]}\" found."
    fi
    ((INDEX += 1))
  done


  doreport debug "check_for_files finished."
}



loop_direcories()
{
  doreport notify "Searching for ${#FILES_WHITELIST[@]} files and ${#SUBDIRECTORIES_WHITELIST[@]} subdirectories from the whitelists." "Reporting ${#FILES_BLACKLIST[@]} files and ${#SUBDIRECTORIES_BLACKLIST[@]} subdirectories from the blacklists."

  for CURRENT_SUBDIRECTORY in ./*; do
      if [ -d "$CURRENT_SUBDIRECTORY" ]; then
	  doreport shortnote "checking directory $CURRENT_SUBDIRECTORY"
	  check_for_subdirectories $CURRENT_SUBDIRECTORY
	  check_for_files $CURRENT_SUBDIRECTORY
      fi
  
  done
}


## parse parameter 1
case "$1" in

    "help"|"--help"|"?"|"/?")
	doreport debug "Parameter help|--help|?|/? detected"
	print_help;
	myexit 0;;

    *)
	doreport debug "no parameter.";
        loop_direcories;
	myexit 0;;

esac







########################################################


## test for important stuff:
postcondition_asserts

myexit 0


########################################################
##                      E N D                         ##
########################################################

