#! /bin/tcsh -f

# --------------------------------------------------------------------------
# SELECTION OF DESIRED CONFIGURATION(S)
# --------------------------------------------------------------------------

# set nonomatch
set letters = (a b d e f g h i j k l m n p r s t u v w x y z)

echo "please, choose the set of tagging configurations from:"
echo "[one or more letters (no spaces!), quit=q]:"

if ( "$1" == "" ) then
  set crit = \*.cfg
else
  set crit = $1
endif

# list of configurations found in the current directory
set cn = "0"
set cfgfiles = ($crit)
set cfgletters = ""
foreach i ($cfgfiles)
  @ cn=$cn + 1
  set desc = `grep "#N#" $cfgfiles[$cn] | sed 's|#N#||g' | sed 's|;||g'`
  set ltmp = `grep "#K#" $cfgfiles[$cn] | sed 's|#K#||g' | sed 's|;||g'`
  if ("$desc" == "") set desc = "<no description found>"
  if ("$ltmp" == "") set ltmp = $letters[$cn]
  set cfgletters = ($cfgletters $ltmp)
  echo "$cfgletters[$cn]) $cfgfiles[$cn] : $desc"
end

# inputting selection
if (! ${?batch}) then
  set inputstring = "$<"
else
  echo "selected configuration from batch is: $tagcfg"
  echo ""
  set inputstring = $tagcfg
endif

# default: one configuration - c
if ( "$inputstring" == "" ) set inputstring = "c"

# desired quit
if ( "$inputstring" == "q" ) exit 1

# parsing configurations selection
set cfglist = ""
set cln = "0"
foreach i ($cfgletters)
  @ cln=$cln + 1
  set instr = `echo $inputstring $cfgletters[$cln] | awk '{print index($1,$2)}'`
  if ("$instr" != "0") then
  # adding configuration
    set cfglist = ($cfglist $cfgfiles[$cln])
  endif
end

# final choice
if ("$cfglist" == "") then
  echo "ambiguous input; no configurations selected. exiting."
  echo ""
  exit 1
endif

echo "[ selected configuration(s): $cfglist ]"
echo ""
