#! /bin/bash
# Last edited on 2012-12-08 21:30:34 by stolfilocal

PROG_NAME=${0##*/}
PROG_DESC="find all makefiles under current directory"
PROG_HELP=(
  "${PROG_NAME} [ -notheses ] {DIR}.."
)
PROG_INFO=(
  "\nNAME"
  "\n  ${PROG_NAME} - ${PROG_DESC}."
  "\n"
  "\nSYNOPSIS"
  "\n  ${PROG_HELP[@]}"
  "\n"
  "\nDESCRIPTION"
  "\n  Writes to stdout a list of all makefilesfiles"
  "\n  in the specified directories."
  "\n"
  "\n  Assumes that makefiels are called \"*[Mm]akefile\""
  "\n  or \"*.make\"."
  "\n"
  "\n  Excludes from the list the files \"Deps.make\" created by {extract-ho-deps},"
  "\n  as well as any files that have names"
  "\n  containing \"junk\", \"old\", or \"save\" (delimited"
  "\n  by periods, dashes, or slashes), in any capitalization; and"
  "\n  directory trees whose root is called \"ARCHIVE\","
  "\n  \"GARBAGE\" \"IMPORT-*\", \"PUB/include\", \"pkg\","
  "\n  \"posters/*/[0-9][0-9][-0-9]*[0-9]/\","
  "\n  or \"hand-in\", in that capitalization."
  "\n" \
  "\nOPTIONS"
  "\n  -notheses"
  "\n    Excludes subdirectories called \"theses\", too, in any"
  "\n    capitalization."
  "\n"
  "\nSEE ALSO"
  "\n  find(1)"
  "\nAUTHOR"
  "\n  Created 2006-05-08 by Jorge Stolfi, Unicamp"
)

# ----------------------------------------------------------------------
# INTERNAL OPTIONS

# ----------------------------------------------------------------------
# COMMAND LINE PARSING

# Parse command line switches: 
notheses=( cat )
# bogus=( cat )
while [[ ( $# -ge 1 ) && ( "/$1" =~ /-.* ) ]]; do
  if [[ ( $# -ge 1 ) && ( "/$1" == "/-notheses" ) ]]; then 
    notheses=( egrep -v -i -e "[\\/]theses|teses[\\/]" ); shift;
#   elif [[ ( $# -ge 2 ) && ( "/$1" == "/-bogus" ) ]]; then 
#     extrop=( ${extrop[@]} "-bogus" "$2" ); shift; shift;
   else
    echo "unknown option $1" 1>&2 ;
    echo -e "usage:\n  ${PROG_HELP[@]}" 1>&2 ; exit 1 
  fi
done 

# echo "notheses = ( ${notheses[*]} )" 1>&2

# Get positional parameters
dirs=( \
  ` echo "$@" \
      | tr ' ' '\012' \
      | sed -e 's:^\$:./:' -e 's:[/]*\$:/:' \
      | ${notheses[@]} \
  ` \
)
if [[ ${#dirs[@]} == 0 ]]; then
  dirs=( ./ )
fi

echo "searching directories ${dirs[*]}" 1>&2

# # Check for leftover arguments:
# if [[ $# -ne 0 ]]; then
#   echo 'wrong number of arguments "'"$1"'" ...' 1>&2
#   echo -e "usage:\n  ${PROG_HELP[@]}" 1>&2 ; exit 1 
# fi

# END COMMAND LINE PARSING
# ----------------------------------------------------------------------

# Prefix for temporary file names
tmp="/tmp/$$"

echo "looking for makefiles that are identifiable by name ..." 1>&2
find "${dirs[@]}" \
    -name 'ARCHIVE' -prune -o \
    -name 'GARBAGE' -prune -o \
    -name 'IMPORT-*' -prune -o \
    -name 'hand-in' -prune -o \
    -name 'pkg' -prune -o \
    -name '00-TO-CD' -prune -o \
    -name '00-TARFILES-CONVERTED' -prune -o \
    -wholename '*/projects/imgbank/*/[0-9][0-9][0-9]/*' -prune -o \
    -wholename '*/posters/*/[0-9][0-9][-0-9]*[0-9]/*' -prune -o \
    -wholename '*/stolfi/include' -prune -o \
    -wholename '*/PUB/include' -prune -o \
    -type f \
      \( \
        -name '*[Mm]akefile' -o \
        -name '*.make' \
       \) -print \
  > ${tmp}.chm

echo "sorting file names and removing various trash files ..." 1>&2
cat ${tmp}.chm \
  | egrep -i -v -e '[-\/.](junk|save|old).*[\/.]' \
  | egrep -v -e '[\/]Deps[.]make$' \
  | egrep -v -e '[~]$' \
  | ${notheses[@]} \
  | sed -e 's:^[.][\/]::' \
  | sort \
  | uniq

/bin/rm -f ${tmp}.chm ${tmp}.exs 
