#! /bin/sh
#
# Copyright (c) 2003 by Intel Corp.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  This
# file and program are licensed under a BSD style license.  See
# the Copying file included with the OpenHPI distribution for
# full licensing terms.
#
# Authors:
#     Kevin Gao <kevin.gao@intel.com>

usage()
{
    cat <<EOF 
Usage: $(basename $0) [OPTIONs] DIRECTORY

Lists the tests (source/binary) available from the DIRECTORY directory
and down.

  --buildable     List only tests that require building
  --execs         List only tests that are executable
                  If you just want to build a test, but not run it,
                  do not include a main function into the .c file or
                  name it something including the "-buildonly" string.
  --fmake         Find functional makefiles.
  --smake         Find stress makefiles.
  --frun          Find functional run.sh files.
  --srun          Find stress run.sh files.
  --help          Show this help and exit

Filenames need to follow some standarized format for them to be picked
up by this tool. This might change in the future. So far, the ones
picked up are:

NUMBER-NUMBER.c     [requires compilation]
NUMBER-NUMBER.sh    [does not require compilation]
NUMBER-buildonly.c  [requires compilation]
NAME.sh             [does not require compilation]

Note that the [requires compilation] tags will mean that the actual
test name for TEST.c after compiling will be TEST. Currently it does
not support TESTs compiled from many different sources.

EOF
}

buildable="";
execs=""
print_execs=0

# Go through the cmd line options
while true
do
  case "$1" in
      "--buildable")
          buildable="( -name *.c )";
          shift;
          ;;
      "--execs")
          print_execs=1;
          execs="( ( -name *.c -o -name *.sh ) -a ! -name *-buildonly* )";
          shift;
          ;;
      "--fmake")
          find functional/ -type f -maxdepth 2 -mindepth 2 -name "Makefile" -printf "%h\n"
          exit 0;
          ;;
      "--frun")
          find functional/ -type f -maxdepth 2 -mindepth 2 -name "run.sh" -printf "%h\n"
          exit 0;
          ;;
      "--smake")
          find stress/ -type f -maxdepth 2 -mindepth 2 -name "Makefile" -printf "%h\n"
          exit 0;
          ;;
      "--srun")
          find stress/ -type f -maxdepth 2 -mindepth 2 -name "run.sh" -printf "%h\n"
          exit 0;
          ;;
      "--help")
          usage;
          exit 0;
          ;; 
      --*)
          echo "Unknown option $1" 1>&2;
          usage 1>&2;
          exit 1;
          ;;
      *)
          break 2;
          ;;
  esac
done

# Need the DIRECTORY arg ...
if [ -z "$1" ]
then
    echo "Error: no root directory specified" 1>&2
    usage 1>&2;
    exit 1;
fi

# Simple version right now, just locate all:
WHERE="$1"

# Force something .c or .sh
# Avoid .o, backups
# IF --execs, force it has no "-buildonly"
# If --buildable, remove the .sh files
find "$WHERE" -type f \
    \( \
       \( -name "*.c"  \) \
       ! -name \*.o ! -name \*~  \
    \) \
    $buildable $execs \
    | if [ $print_execs -eq 1 ]
        then
            sed 's/\.\(sh\|c\)$/.test/'
        else
            cat
        fi


