#! /bin/bash

# Configure the cyflex_java applications with common settings

# Create a cui_properties.txt file if needed.

# Where cyflex_java is installed for test cells
INSTALL_BASE=/opt/cyflex_java

# Use this script location as the starting point
TEMP=$(dirname $0)
CYFLEX_JAVA_HOME=$(realpath $TEMP/..)
unset TEMP

# Where everything is relative to the basis
export JAVA_HOME=$CYFLEX_JAVA_HOME/java_jdk
PATH=$CYFLEX_JAVA_HOME/cmds:$JAVA_HOME/bin:$PATH
D3=$CYFLEX_JAVA_HOME/specfilegui.3rdparty
GUI=$CYFLEX_JAVA_HOME/specfilegui

# Test cell install case
TEMP=${CYFLEX_JAVA_HOME:0:16}
if [[ "$TEMP" == "$INSTALL_BASE" ]]; then
   CYFLEX=/cyflex
   SPECS=/specs
   CELL=/cell
   PROP_FILE=/cell/cui_properties.txt
# Development / testing case
else
   CYFLEX=$CYFLEX_JAVA_HOME/specfilegui.src/data/cyflex
   SPECS=$CYFLEX_JAVA_HOME/specfilegui.src/data/specs
   CELL=$CYFLEX_JAVA_HOME/specfilegui.src/data/cell
   PROP_FILE=$CYFLEX_JAVA_HOME/specfilegui.src/data/cell/cui_properties.txt
fi

# If the properties file directory doesn't exist, create it
mkdir -p $(dirname $PROP_FILE)

# If the properties file doesn't exist, create it
if [ ! -e $PROP_FILE ]; then
   touch $PROP_FILE  
   . update_properties $PROP_FILE
else
   # See if the properties file needs to be updated. Search for the
   # expected property key.  If not found, update
   grep -q '^cyflex_java.home' $PROP_FILE
   if (( $? != 0 )); then
      . update_properties $PROP_FILE
   fi
fi
