From 40c06dc3c6a56f17080836a5c6790a51eaf1c6b9 Mon Sep 17 00:00:00 2001
From: Brandon Clayton <bclayton@usgs.gov>
Date: Thu, 5 Aug 2021 13:20:26 -0600
Subject: [PATCH] simplify script

---
 scripts/docker-entrypoint.sh | 111 +++++++++++++++++++++--------------
 1 file changed, 68 insertions(+), 43 deletions(-)

diff --git a/scripts/docker-entrypoint.sh b/scripts/docker-entrypoint.sh
index 3fbe1f8c4..c4a847f80 100644
--- a/scripts/docker-entrypoint.sh
+++ b/scripts/docker-entrypoint.sh
@@ -1,50 +1,75 @@
 #!/bin/bash
-# shellcheck disable=SC1090
-
-source "$(dirname "${0}")/docker-config.inc.sh";
-exit_status=${?};
-[ "${exit_status}" -eq 0 ] || exit "${exit_status}";
-
-# Get nshmp program to call
-nshmp_program=$(get_nshmp_program "${PROGRAM}");
-exit_status=${?};
-check_exit_status "${exit_status}";
-
-# Get model path to use
-if [ "${MOUNT_MODEL}" = true ]; then
-  nshm_path="model";
-else
-  nshm_path=$(get_model_path "${MODEL}" "${NSHM_VERSION}");
+
+##
+# Run nshmp-haz
+##
+main() {
+  # Get name of sites file
+  sites_file=$(ls /app/sites.*);
+
+  # Run nshmp-haz
+  java "${JAVA_OPTS}" \
+      -cp "/app/nshmp-haz.jar" \
+      "gov.usgs.earthquake.nshmp.${CLASS_NAME}" \
+      "${MODEL_PATH}" \
+      "${sites_file}" \
+      ${RETURN_PERIOD:+ "${RETURN_PERIOD}"} \
+      ${IML:+ "${IML}"} \
+      "${CONFIG_FILE}";
+  exit_status=${?};
+  check_exit_status "${exit_status}";
+
+  # Move results to container volume
+  move_to_output_volume;
   exit_status=${?};
   check_exit_status "${exit_status}";
-fi
 
-# Check site file and get site file path
-site_file=$(check_sites_file);
-exit_status=${?};
-check_exit_status "${exit_status}";
+  exit ${exit_status};
+}
+
+####
+# Check current exit status.
+#
+# @param $1 exit_status {Integer}
+#     Current exit status
+####
+check_exit_status() {
+  local exit_status=${1};
+  [ "${exit_status}" -eq 0 ] || exit "${exit_status}";
+}
+
+####
+# Exit with an error message.
+#
+# @param $1 msg {String}
+#     The message for exit
+# @param $2 exit_status {Integer}
+#     The exit status
+####
+error_exit() {
+  local msg=${1}; shift;
+  local exit_status=${1}
+  echo "Error: ${msg}" >> /dev/stderr;
+  exit "${exit_status}";
+}
+
+####
+# Move artifacts to mounted volume.
+#
+# @status Integer
+#     The status of moving the files.
+####
+move_to_output_volume() {
+  local hazout;
+  hazout=$(jq -r ".output.directory" "${CONFIG_FILE}");
+
+  if [ "${hazout}" == null ]; then
+    hazout="hazout";
+  fi
 
-# Check config file
-[ -f "${CONFIG_FILE}" ] || echo "{}" > "${CONFIG_FILE}";
-jq empty < "${CONFIG_FILE}";
-exit_status=${?};
-check_exit_status "${exit_status}";
+  mv "${hazout}/*" "${OUTPUT_PATH}/.";
+  return ${?};
+}
 
 # Run nshmp-haz
-java -"Xmx${JAVA_XMX}" \
-    -cp "/app/${PROJECT}.jar" \
-    "gov.usgs.earthquake.nshmp.${nshmp_program}" \
-    "${nshm_path}" \
-    "${site_file}" \
-    ${RETURN_PERIOD:+ "${RETURN_PERIOD}"} \
-    ${IML:+ "${IML}"} \
-    "${CONFIG_FILE}";
-exit_status=${?};
-check_exit_status "${exit_status}";
-
-# Move results to container volume
-move_to_output_volume;
-exit_status=${?};
-check_exit_status "${exit_status}";
-
-exit ${exit_status};
+main "$@";
-- 
GitLab