3 # Lists all the FCL files whose name matches the specified pattern.
5 # Usage: ListFCL.sh [Pattern]
7 # The pattern is a regex as interpreted by grep ("-e" option).
10 SCRIPTNAME="$(basename "$0")"
12 : ${FORMAT:="%f (in %h)"}
16 Looks for FHICL files in the ART FHICL search directories.
18 Usage: ${SCRIPTNAME} [options] [--] [Pattern]
22 use the specified format; the default format is "%f (in %h)";
23 the placeholder are as documented in find (1) manual
25 print the file name only
33 function isFlagSet() {
35 [[ -n "${!VarName//0}" ]]
38 function STDERR() { echo "$*" >&2 ; }
39 function ERROR() { STDERR "ERROR: $@" ; }
43 STDERR "FATAL ERROR (${Code}): $*"
46 function LASTFATAL() {
48 [[ "$Code" != 0 ]] && FATAL "$Code""$@"
53 if [[ -n "$Pattern" ]]; then
60 function CleanSortKey() { sed -e 's/^[^ ]* \+//' ; }
62 function ApplyFormat() {
66 local FileName="${Data%%/*}"
67 local Path="${Data#*/}"
71 echo "${FileName} (in ${Path})"
74 sed -e "s@\([^\]|\^\)%f@${FileName}@" -e "s@\([^\]|\^\)%h@${Path}@" <<< "$Format"
79 ################################################################################
81 declare -i NoMoreOptions=0
83 declare -i nPatterns=0
84 for (( iParam = 1 ; iParam <= $# ; ++iParam )); do
86 if ! isFlagSet NoMoreOptions && [[ "${Param:0:1}" == '-' ]]; then
88 ( '--help' | '-h' | '-?' ) DoHelp=1 ;;
91 ( '--name' | '-n' ) FORMAT="%f" ;;
92 ( '--path' | '-p' ) FORMAT="%p" ;;
93 ( '--format='* ) FORMAT="${Param#--*=}" ;;
100 echo "Unrecognized script option #${iParam} - '${Param}'"
106 Patterns[nPatterns++]="$Param"
110 if isFlagSet DoHelp ; then
112 # set the exit code (0 for help option, 1 for missing parameters)
118 # - start with paths in FHICL_FILE_PATH
120 # - find in all those directories (but not in their subdirectories),
121 # and in that order, all the FCL files
122 # - print for each its name and the string to be presented to the user as output
123 # - soft them by FCL file name, preserving the relative order of files with the
124 # same name from different directories
125 # - filter them on sort key (file name) by user's request
126 # - remove the sort key (file name) from the output
127 tr ':' "\n" <<< "$FHICL_FILE_PATH" | xargs -I SEARCHPATH find SEARCHPATH -maxdepth 1 -name "*.fcl" -printf "%f ${FORMAT}\n" 2> /dev/null | sort -s -k1,1 -u | Filter "^[^ ]*${Patterns[0]}[^ ]* " | CleanSortKey