#! /bin/csh -f
#
#  support Java wrapping of uread/sglib programs...
#
#  this script echoes the list of "known" uread scripts for a program
#  (NOTE: this could be an empty list)
#
#     uread_script_list <program-name>
#       -- scripts found in $cwd
#     uread_script_list <program-name> read_only
#       -- scripts found in $CODESYSDIR/tmi/<program-name>
#
#  ...or...
#
#     uread_script_list <program-name> all
#       produced output
#       READ_ONLY: <path-to-readonly-scripts> <.ext>
#       <script1>
#       <script2>
#         ...
#       READ_WRITE: <path-to-user-accessible-scripts> <.ext>
#       <script1>
#       <script2>
#
#  the filename extension for scripts is found in 
#  $CODESYSDIR/tmi/<program-name>/default.extension 
#  ... if this file is not found, the script echoes "unknown-program-error"
#  and exits with status code 1
#
#  if the arguments do not have the form shown above the script echoes
#  "syntax-error" and exits with status code 1
#
#  otherwise the script echoes the script filename extension with a leading
#  dot, e.g. ".tmi" or ".ind", followed by a list of 0 or more scripts files
#  found with this extension
#
#  for example:  uread_script_list rplot read_only
#

  @ all_mode = 0
  @ read_only = 0
  @ ier = 0

  if ( $#argv == 0 ) then
    @ ier++

  else if ( $#argv > 2 ) then
    @ ier++

  else if ( $#argv == 2 ) then
    if ( "$argv[2]" == "read_only" ) then
      @ read_only++
    else if ( "$argv[2]" == "all" ) then
      @ all_mode++
    else
      @ ier++
    endif

  endif

  if ( $ier > 0 ) then
    echo "syntax-error"
    exit 1
  endif

  # syntax OK

  set progname = $argv[1]

  set apath = $CODESYSDIR/tmi/$progname
  set epath = $apath/default.extension

  if ( ! -f $epath ) then
    echo "unknown-program-error"
    exit 1
  endif

  set ext = `cat $epath`

  if ( $all_mode == 1 ) then
    # qualifier argument: "all"
    echo "READ_ONLY:  $apath  .$ext"

    set dlist = `ls $apath | grep \.${ext}$ | sed "s#\.$ext##"`
    foreach item ( $dlist )
      echo $item
    end

    echo "READ_WRITE:  $cwd  .$ext"

    # and make sure directory is not empty
    date > $cwd/usl_date.tmp

    set dlist = `ls $cwd | grep \.${ext}$ | sed "s#\.$ext##"`
    foreach item ( $dlist )
      echo $item
    end

  else
    # "read_only" or no qualifier argument...

    if ( $read_only == 1 ) then
      set dpath = $apath
    else
      set dpath = $cwd
      # and make sure directory is not empty
      date > $cwd/usl_date.tmp
    endif

    # get list of script names; 
    set dlist = `ls $dpath | grep \.${ext}$ | sed "s#\.$ext##"`

    # echo the .ext and then the list

    echo ".$ext"
    foreach item ( $dlist )
      echo $item
    end
  endif

  rm -f $cwd/usl_date.tmp
  exit 0


