#! /bin/bash

#
# Script name: specfilegui [script_type] [initial_file]
#
# Purpose: Bring up the requested spec file editor.
#
# Notes: If no argument is given, brief usage information is produced.
#
# History: 
#    02/18/20 - Rob Janes - JIRA CCS-1593 - Added dsply_specs
#    01/16/17 - Jen Schuck - CFBUG-1416 - change test_cell_number to cell_name
#    01/27/15 - Rob Janes - Added asam3_specs
#    08/24/11 - Rob Janes - Added cal_table
#    12/08/10 - Rob Janes - Added ra_specs
#    11/06/10 - Rob Janes - Added alias_labels
#    05/22/09 - Rob Janes - Removed supplying default file if not is
#                           supplied on the command line - per Dave
#                           Ruthmansdorfer.
#    05/18/09 - Rob Janes - Added check for default spec file existing.
#    05/13/09 - Rob Janes - Added cuty_specs
#    12/03/08 - Rob Janes - Removed master.  Added gp_test, test_table,
#                           logr_specs
#    06/16/08 - Rob Janes - Updated to invoke generic_java instead of doing everything
#       here.
#    05/11/08 - Rob Janes - Added jgoodies look and feel
#    10/16/07 - Rob Janes - Added ctrl_specs
#    10/05/07 - Rob Janes - Updated to add gp_test.jar for use with
#       CVEDialog.
#    05/25/07 - Rob Janes - Updated to handle command line options.
#       Currently only -dump_labels recognized.
#    ??/??/?? - ? - Updated to default file edited if none specified.
#    08/09/06 - Rob Janes - Added command line argument for spec file
#       to edit at start up.
#    05/05/06 - Rob Janes - Initial release.
#

TYPE_LIST=(alias_labels ao_specs asam3_specs cal_table co_specs ctrl_specs \
   cuty_specs do_specs dsply_specs gen_labels gp_test inpt_specs limit_specs \
   logr_specs pam_specs ra_specs test_table)

# No command line argument produces a list of valid arguments.
if (( $# == 0 ))
then
cat <<eod

Usage: specfilegui  TYPE  FILE

This script should be invoked with 1 or 2 command line arguments, the
type of spec file to be edited optionally followed by the file to edit.

Types which may be specified are:
eod
   for TYPE in ${TYPE_LIST[@]}; do
      echo '   ' $TYPE
   done
   exit 0
fi

# See if a valid spec file type was specified
for TYPE in ${TYPE_LIST[@]}; do
   if [ "$1" = $TYPE ] ; then
      break;
   fi
done
if [ "$1" != $TYPE ]; then
   echo You must enter a valid spec file type.
   echo Try "'specfilegui'" for help.
   exit 1
fi

NAME=$1

# Handle command line options.
declare -i I
OPTIONS=''
FILE=''

I=2
while (( $I <= $# )) ; do
   if [ ${!I} = '-dump_labels' ]; then
      OPTIONS="$OPTIONS -dump_labels"
      I=$I+1
      OPTIONS="$OPTIONS ${!I}"
   else
      FILE=${!I}
   fi
   I=$I+1
done

PACKAGE=com.cybermetrix.cyflex.specfiles
if [ $NAME = 'ctrl_specs' ]; then
   PACKAGE=com.cybermetrix.cyflex.applications
elif [ $NAME = 'cal_table' ]; then
   PACKAGE=com.cybermetrix.cyflex.applications.cal_table
elif [ $NAME = 'cuty_specs' ]; then
   PACKAGE=com.cybermetrix.cyflex.applications
elif [ $NAME = 'asam3_specs' ]; then
   PACKAGE=com.cybermetrix.cyflex.applications
elif [ $NAME = 'alias_labels' ]; then
   PACKAGE=com.cybermetrix.cyflex.applications
elif [ $NAME = 'ra_specs' ]; then
   PACKAGE=com.cybermetrix.cyflex.applications
elif [ $NAME = 'gp_test' ]; then
   PACKAGE=com.cybermetrix.cyflex.gp_test
   NAME=gp_testEditor
   if [ "$FILE" = "$1.`cat /cell/cell_name`" ]; then
      FILE=''
   fi
elif [ $NAME = 'dsply_specs' ]; then
   export LD_LIBRARY_PATH=/cyflex/LIB
   generic_java com.cybermetrix.cyflex.cui.display.DsplySpecs \
      -connection local $FILE
   exit
elif [ $NAME = 'test_table' ]; then
   PACKAGE=com.cybermetrix.cyflex.applications
fi

generic_java $PACKAGE.$NAME $OPTIONS $FILE
