#! /bin/bash

# This controls the Mach for CyFlex instance
#
# Arguments
#    CONTROL - Required - start, stop, pause, restart, report,
#       statistics_on, statistics_off
#    -test FILE - Optional - run in test mode and not connect to a live
#       CyFlex system.  FILE is a measured values file to use as
#       measured data input.

# Function to find one of the start, stop, pause and restart file names
function find_file () {
   LOCATION=$(grep $1 $MACH_CYFLEX_PROPERTIES | cut -f2 -d\ )
}

# There must be one command line argument.  If there are none, it's
# assumed that help information is being requested.
if (( $# == 0 )); then
   echo Usage:
   echo '   control_mach [ start | stop | pause | restart | report | statistics_on | statistics_off]'
   echo '   The log file is typically located at /data/errors/mach.log.DATE'
   echo
   echo '   General Mach documentation is available at: '
   echo '      file:///usr/local/share/mach/UserDocs/index.html'
   exit 1
fi

# Base for all the installed Mach files
MACH_BASE=/usr/local/share/mach

# Insure Java 17 is the version of Java used to run this application
export JAVA_HOME=$MACH_BASE/java_17
PATH=$JAVA_HOME/bin:$PATH
LD_LIBRARY_PATH=$JAVA_HOME/lib:$LD_LIBRARY_PATH

# Default location for Mach properties
if [[ "$MACH_PROPERTIES" == '' ]]; then
   MACH_PROPERTIES=$MACH_BASE/data/mach_properties.txt
fi

# Default location for cyflex_properties
if [[ "$SPECFILE_PROPERTIES" == '' ]]; then
   SPECFILE_PROPERTIES=/cell/cui_properties.txt
fi

# Default location for cyflex_properties
if [[ "$MACH_CYFLEX_PROPERTIES" == '' ]]; then
   MACH_CYFLEX_PROPERTIES=/specs/mach_cyflex_properties.txt
fi

if [[ $1 == start ]]; then
   # Class path for all the Mach applications.
   export CLASSPATH=''
   for FILE in $MACH_BASE/jars/*; do
      CLASSPATH=${FILE}:${CLASSPATH}
   done
   for FILE in $MACH_BASE/jars/jacorb/*; do
      CLASSPATH=${FILE}:${CLASSPATH}
   done
   for FILE in $MACH_BASE/jars/jaxb/*; do
      CLASSPATH=${FILE}:${CLASSPATH}
   done
   for FILE in $MACH_BASE/jars/nashorn/*; do
      CLASSPATH=${FILE}:${CLASSPATH}
   done
   # Add specfilegui for monitoring use
   CLASSPATH=${CLASSPATH}:/cyflex/specfilegui/specfilegui.jar

   DATE=$(date +"%y%m%d")
   chrt -o 0 java -Dcyflex_properties=$MACH_CYFLEX_PROPERTIES \
        -Dmach_properties=$MACH_PROPERTIES \
        com.cmx.cyflex.prog.mach_for_cyflex \
        >>/data/errors/mach.log.$DATE 2>&1 $2 $3 &

elif [[ $1 == stop ]]; then
   find_file com.cmx.mach.cyflex_stop_file
   touch $LOCATION

elif [[ $1 == pause ]]; then
   find_file com.cmx.mach.cyflex_pause_file
   touch $LOCATION

elif [[ $1 == restart ]]; then
   find_file com.cmx.mach.cyflex_restart_file
   touch $LOCATION

elif [[ $1 == report ]]; then
   find_file com.cmx.mach.cyflex_report_file
   touch $LOCATION

elif [[ $1 == statistics_on ]]; then
echo statistics_on
   find_file com.cmx.mach.cyflex_statistics_on_file
   touch $LOCATION

elif [[ $1 == statistics_off ]]; then
   find_file com.cmx.mach.cyflex_statistics_off_file
   touch $LOCATION

else
   echo Invalid command line argument: $1
   exit 1
fi

exit 0
