diff --git a/run.sh b/run.sh old mode 100755 new mode 100644 index 89ff936e..4ed21a92 --- a/run.sh +++ b/run.sh @@ -18,7 +18,7 @@ Where: " } -MODE="SCREEN" +MODE="FOREGROUND" until [[ $# -eq 0 ]]; do case "$1" in @@ -27,6 +27,11 @@ until [[ $# -eq 0 ]]; do shift ;; + -b|--background) + MODE="BACKGROUND" + shift + ;; + -h|--help) printHelp exit 0 @@ -43,27 +48,80 @@ until [[ $# -eq 0 ]]; do shift done +# ----------------------------------------------- +# Functions +# ----------------------------------------------- + +function startHal { + local ARGS=$1 + local CLASSPATH="$(cd "${INSTALL_DIR}" && find . -name "*.jar" -type f -printf '%p:')" + CLASSPATH=${CLASSPATH::-1} + + (set -x && cd "${INSTALL_DIR}" && java -classpath "${CLASSPATH}" se.hal.HalServer ${ARGS}) + local EXIT_CODE=$? + + return ${EXIT_CODE} +} + +function createSymLink { + local file=$1 + + if [[ -f "./${file}" ]]; then + echo "INFO: Creating symlink for: ${file}" + ln -s -f "$(realpath "./${file}")" "${INSTALL_DIR}/${file}" + fi +} + # ----------------------------------------------- # Execute # ----------------------------------------------- +# Prepare execution # gradle returns normally when doing ctr-c so we need to add a # trap where the bash script exits instead of continuing. trap 'exit 130' INT -# Build Hal source -./gradlew build +INSTALL_DIR="." + +if [[ -d "./hal-core/src" ]]; then + # We are operating on source code so build Hal + echo "INFO: Operating in source directory, will build Hal." + INSTALL_DIR="./build/install/Hal" + + ./gradlew --console=rich installDist + EXIT_CODE=$? + + if [[ ${EXIT_CODE} -ne 0 ]]; then + exit 1 + fi + + echo "" + createSymLink "hal.conf" + createSymLink "hal.db" +fi + +# Execute + +echo "" +echo "INFO: Starting Hal in the ${MODE}." if [[ ${MODE} == "FOREGROUND" ]]; then - ./gradlew run -else + EXIT_CODE=200 + + while [[ ${EXIT_CODE} -eq 200 ]]; do + # Restart as long as we have a exit code of 200, this allows the application to restart itself + startHal + EXIT_CODE=$? + done +elif [[ ${MODE} == "BACKGROUND" ]]; then # Kill current session screen -S hal -X kill # Start new session - screen -S hal -L -d -m ./gradlew run + screen -S hal -L -d -m ./run --foreground echo "-------------------------" screen -list +else + echo "ERROR: Unknown mode: ${MODE}" + exit 1 fi - -