From c7693389d9927199bc8320f67aadbbfbb1484a9c Mon Sep 17 00:00:00 2001
From: Erik von Reis <evonreis@caltech.edu>
Date: Mon, 14 Dec 2020 15:57:46 -0800
Subject: [PATCH] rtcds: read in environment before setting flags and change
 flag tests to explictly look for non-empty strings.

The entire block of code for initializing environment variables has been moved before the command line flag checks.
The USERLAND_FLAG and NO_KERNELSPACE_FLAG default values are determined by the environment, but the /etc/advligorts files need to be read in first.

Command line flags are cleared if empty string, or set if not empty.  This wasn't obvious originally since the test for flags was
just [[ $SOME_FLAG ]].

The tests have been changed to [[ -n $SOME_FLAG ]] or in the negative case [[ -z $SOME_FLAG ]].
This doesn't change the behavior but is less ambigous to the eye.
---
 support/bin/rtcds.in | 140 +++++++++++++++++++++++--------------------
 1 file changed, 75 insertions(+), 65 deletions(-)

diff --git a/support/bin/rtcds.in b/support/bin/rtcds.in
index 788091b51..2064f2c8b 100755
--- a/support/bin/rtcds.in
+++ b/support/bin/rtcds.in
@@ -4,16 +4,77 @@ log() {
     echo "$@" >&2
 }
 
+RTS_VERSION=${RTS_VERSION:-__VERSION__}
+if [[ "$RTS_VERSION" =~ ._VERSION_. ]] ; then
+    log "RTS_VERSION variable not set."
+    exit 1
+fi
+
+# this should define all USER_VARS (see below)
+ENV_FILE=${RTS_ENV:-/etc/advligorts/env}
+HOST_ENV_FILE=/etc/advligorts/systemd_env_`hostname`
+set -o allexport
+source "$ENV_FILE" 2>/dev/null || true
+if [[ -e $HOST_ENV_FILE ]] ; then
+    log "found host specific environment file '$HOST_ENV_FILE'"
+    source $HOST_ENV_FILE 2>/dev/null || true
+fi
+set +o allexport
+
+SITE=${SITE^^*}
+site=${SITE,,*}
+IFO=${IFO^^*}
+ifo=${IFO,,*}
+
+CHECK_MODEL_IN_HOST=${CHECK_MODEL_IN_HOST:-false}
+IS_DOLPHIN_NODE=${IS_DOLPHIN_NODE:-false}
+DAQ_STREAMING=${DAQ_STREAMING:-false}
+
+RCG_SRC=${RCG_SRC:-/usr/share/advligorts/src}
+RCG_BUILD_ROOT=${RCG_BUILD_ROOT:-/var/cache/advligorts}
+RCG_BUILDD=${RCG_BUILDD:-$RCG_BUILD_ROOT/rcg-$RTS_VERSION}
+# FIXME: the RCG hard-codes /opt/rtcds as the root
+RCG_TARGET=/opt/rtcds/${site}/${ifo}
+
+# search paths for C source code
+CDS_SRC=${CDS_SRC:-$RCG_LIB_PATH}
+CDS_IFO_SRC=${CDS_IFO_SRC:-$CDS_SRC}
+
+# add RCG module source to lib path
+# FIXME: rename RCG_MOD_PATH
+RCG_LIB_PATH=$RCG_LIB_PATH:${RCG_SRC}/src/epics/simLink/:${RCG_SRC}/src/epics/simLink/lib
+
+#options from environment variables
+ALLOW_MODEL_ENABLE=${ALLOW_MODEL_ENABLE:-true}
+
+USER_VARS=(SITE IFO RCG_LIB_PATH)
+LIST_VARS=(RTS_VERSION ${USER_VARS[@]} RCG_SRC RCG_BUILD_ROOT RCG_BUILDD RCG_TARGET)
+EXPORT_VARS=(${USER_VARS[@]} site ifo CDS_SRC CDS_IFO_SRC)
+
+# set the umask to ensure that all files and directories are made
+# group writable
+umask 0002
+
 USE_KERNEL_MODELS=${USE_KERNEL_MODELS:-false}
 
+# for all flags, empty string means cleared.  Any other string means set.
 if $USE_KERNEL_MODELS ; then
     log "Build kernel-mode models by default"
+    USERLAND_FLAG=
+    NOKERNELSPACE_FLAG=
 else
     log "Build user mode models by default"
     USERLAND_FLAG=0
     NOKERNELSPACE_FLAG=0
 fi
 
+ALL_FLAG=
+FOLLOW_FLAG=
+INFO_FLAG=
+VERSION_FLAG=
+HELP_FLAG=
+FORCE_START_FLAG=
+
 # capture all command line flags as variables
 params=""
 ccmd=""
@@ -72,57 +133,6 @@ done
 
 eval set -- "$params"
 
-RTS_VERSION=${RTS_VERSION:-__VERSION__}
-if [[ "$RTS_VERSION" =~ ._VERSION_. ]] ; then
-    log "RTS_VERSION variable not set."
-    exit 1
-fi
-
-# this should define all USER_VARS (see below)
-ENV_FILE=${RTS_ENV:-/etc/advligorts/env}
-HOST_ENV_FILE=/etc/advligorts/systemd_env_`hostname`
-set -o allexport
-source "$ENV_FILE" 2>/dev/null || true
-if [[ -e $HOST_ENV_FILE ]] ; then
-    log "found host specific environment file '$HOST_ENV_FILE'"
-    source $HOST_ENV_FILE 2>/dev/null || true
-fi
-set +o allexport
-
-SITE=${SITE^^*}
-site=${SITE,,*}
-IFO=${IFO^^*}
-ifo=${IFO,,*}
-
-CHECK_MODEL_IN_HOST=${CHECK_MODEL_IN_HOST:-false}
-IS_DOLPHIN_NODE=${IS_DOLPHIN_NODE:-false}
-DAQ_STREAMING=${DAQ_STREAMING:-false}
-
-RCG_SRC=${RCG_SRC:-/usr/share/advligorts/src}
-RCG_BUILD_ROOT=${RCG_BUILD_ROOT:-/var/cache/advligorts}
-RCG_BUILDD=${RCG_BUILDD:-$RCG_BUILD_ROOT/rcg-$RTS_VERSION}
-# FIXME: the RCG hard-codes /opt/rtcds as the root
-RCG_TARGET=/opt/rtcds/${site}/${ifo}
-
-# search paths for C source code
-CDS_SRC=${CDS_SRC:-$RCG_LIB_PATH}
-CDS_IFO_SRC=${CDS_IFO_SRC:-$CDS_SRC}
-
-# add RCG module source to lib path
-# FIXME: rename RCG_MOD_PATH
-RCG_LIB_PATH=$RCG_LIB_PATH:${RCG_SRC}/src/epics/simLink/:${RCG_SRC}/src/epics/simLink/lib
-
-#options from environment variables
-ALLOW_MODEL_ENABLE=${ALLOW_MODEL_ENABLE:-true}
-
-USER_VARS=(SITE IFO RCG_LIB_PATH)
-LIST_VARS=(RTS_VERSION ${USER_VARS[@]} RCG_SRC RCG_BUILD_ROOT RCG_BUILDD RCG_TARGET)
-EXPORT_VARS=(${USER_VARS[@]} site ifo CDS_SRC CDS_IFO_SRC)
-
-# set the umask to ensure that all files and directories are made
-# group writable
-umask 0002
-
 ##################################################
 
 contains() {
@@ -216,7 +226,7 @@ list_host_sys() {
 # returns 0 if $1 names a system that's designated to run
 #  on this host
 check_sys_in_host() {
-    if $CHECK_MODEL_IN_HOST && ! [[ $FORCE_START_FLAG ]] ; then
+    if $CHECK_MODEL_IN_HOST && [[ -z $FORCE_START_FLAG ]] ; then
   	local systems=`list_host_sys`
   	contains $1 $systems
     fi
@@ -265,10 +275,10 @@ check_host_sys_inactive() {
 
 setup_make_args() {
     make_args=''
-    if [[ $USERLAND_FLAG ]] ; then
+    if [[ -n $USERLAND_FLAG ]] ; then
         make_args='RCG_BUILD_USP=YES'
     fi
-    if [[ $NOKERNELSPACE_FLAG ]] ; then
+    if [[ -n $NOKERNELSPACE_FLAG ]] ; then
         make_args="$make_args RCG_BUILD_NO_KOBJ=YES"
     fi
 }
@@ -550,12 +560,12 @@ Available commands:
 "
 }
 
-if [[ $HELP_FLAG ]]; then
+if [[ -n $HELP_FLAG ]]; then
     usage
     exit 0
 fi
 
-if [[ $VERSION_FLAG ]] ; then
+if [[ -n $VERSION_FLAG ]] ; then
     echo $RTS_VERSION
     exit 0
 fi
@@ -573,7 +583,7 @@ fi
 
 case $cmd in
     'build'|'make')
-	if [[ $ALL_FLAG ]] ; then
+	if [[ -n $ALL_FLAG ]] ; then
 	    check_env
 	    prep_buildd
 	    build_world
@@ -587,7 +597,7 @@ case $cmd in
 	fi
 	;;
     'install')
-	if [[ $ALL_FLAG ]] ; then
+	if [[ -n $ALL_FLAG ]] ; then
 	    check_env
 	    prep_target
 	    install_world
@@ -601,7 +611,7 @@ case $cmd in
 	fi
 	;;
     'start')
-	if [[ $ALL_FLAG ]] ; then
+	if [[ -n $ALL_FLAG ]] ; then
 	    start_sys $(list_host_sys)
 	elif [[ $1 ]] ; then
 	    check_host_sys_inactive $@
@@ -612,7 +622,7 @@ case $cmd in
 	fi
 	;;
     'restart')
-	if [[ $ALL_FLAG ]] ; then
+	if [[ -n $ALL_FLAG ]] ; then
 	    # we do this in reverse so the IOP is stopped last
 	    stop_sys $(list_host_sys | tac)
 	    start_sys $(list_host_sys)
@@ -626,7 +636,7 @@ case $cmd in
 	fi
 	;;
     'stop'|'kill')
-	if [[ $ALL_FLAG ]] ; then
+	if [[ -n $ALL_FLAG ]] ; then
 	    # we do this in reverse so the IOP is stopped last
 	    stop_sys $(list_host_sys | tac)
 	elif [[ $1 ]] ; then
@@ -643,7 +653,7 @@ case $cmd in
 	    log "ALLOW_MODEL_ENABLE environment variable is set to false."
 	    exit 2
 	fi
-	if [[ $ALL_FLAG ]] ; then
+	if [[ -n $ALL_FLAG ]] ; then
 	    # we do this in reverse so the IOP is stopped last
 	    ${cmd}_sys $(list_host_sys | tac)
 	elif [[ $1 ]] ; then
@@ -654,7 +664,7 @@ case $cmd in
 	fi
 	;;
     'status')
-	if [ -z "$1" ] || [[ $ALL_FLAG ]] ; then
+	if [ -z "$1" ] || [[ -n $ALL_FLAG ]] ; then
 	    global_status
 	else
 	    sys=$1
@@ -663,7 +673,7 @@ case $cmd in
     ;;
     'log')
 	args=
-	if [[ $FOLLOW_FLAG ]] ; then
+	if [[ -n $FOLLOW_FLAG ]] ; then
 	    args="-f"
 	fi
 	if [ -z "$1" ] ; then
@@ -675,7 +685,7 @@ case $cmd in
 	;;
     'blog')
 	format=full
-	if [[ $INFO_FLAG ]] ; then
+	if [[ -n $INFO_FLAG ]] ; then
 	    format=info
 	fi
 	if [ -z "$1" ] ; then
-- 
GitLab