Skip to content

Commit

Permalink
Create execute.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
EricClaeys authored Nov 27, 2024
1 parent d02cecd commit 7c0a31b
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions scripts/utilities/execute.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/bin/bash
# variables.sh
# addMessage.sh
# execute.sh (needs to be 755).

# Execute the specified command.
# Normally the command is built-in, but allow for an arbitrary command.
# Many commands are referenced in addMessage.sh and the command name
# is one of its IDs, e.g., ${AM_...}.

# Allow this script to be executed manually, which requires several variables to be set.
[[ -z ${ALLSKY_HOME} ]] && export ALLSKY_HOME="$( realpath "$( dirname "${BASH_ARGV0}" )/.." )"
ME="$( basename "${BASH_ARGV0}" )"

#shellcheck source-path=.
source "${ALLSKY_HOME}/variables.sh" || exit "${EXIT_ERROR_STOP}"
#shellcheck source-path=scripts
source "${ALLSKY_SCRIPTS}/functions.sh" || exit "${EXIT_ERROR_STOP}"

OK="true"
DO_HELP="false"
while [[ $# -gt 0 ]]; do
ARG="${1}"
case "${ARG,,}" in
--help)
DO_HELP="true"
;;
-*)
echo -e "${RED}Unknown argument '${ARG}' ignoring.${NC}" >&2
OK="false"
;;
*)
break
;;
esac
shift
done

usage_and_exit()
{
local RET=${1}
{
echo
[[ ${RET} -ne 0 ]] && echo -en "${RED}"
echo "Usage: ${ME} [--help] command [arguments...]"
[[ ${RET} -ne 0 ]] && echo -en "${NC}"
} >&2
exit "${RET}"
}

[[ ${DO_HELP} == "true" ]] && usage_and_exit 0
[[ ${OK} == "false" || $# -eq 0 ]] && usage_and_exit 1


function rm_msg()
{
local FILE="$1"

local R RET_CODE

R="$( rm -r "${FILE}" 2>&1 )" # -r in case it's a directory
RET_CODE=$?

if [[ ${RET_CODE} -eq 0 ]]; then
echo "Removed '${FILE}'"
else
echo "Unable to remove '${FILE}': ${R}" >&2
fi
return ${RET_CODE}
}

RET=0
CMD="${1}"
case "${CMD}" in
"AM_RM_PRIOR") # Remove prior version of Allsky
rm_msg "${PRIOR_ALLSKY_DIR}"
RET=$?
rm -f "${OLD_ALLSKY_REMINDER}"

"${ALLSKY_SCRIPTS}/addMessage.sh" --id "${CMD}" --delete
;;

"AM_RM_CHECK") # Remove log from checkAllsky.sh
rm_msg "${CHECK_ALLSKY_LOG}"
RET=$?

"${ALLSKY_SCRIPTS}/addMessage.sh" --id "${CMD}" --delete
;;

"AM_RM_POST") # Remove log from checkAllsky.sh
rm_msg "${POST_INSTALLATION_ACTIONS}"
RET=$?

"${ALLSKY_SCRIPTS}/addMessage.sh" --id "${CMD}" --delete
;;

"AM_NOT_SUPPORTED") # Not supported camera
CT="$2"
shift
"${ALLSKY_UTILITIES}/show_supported_cameras.sh" --${CT}

"${ALLSKY_SCRIPTS}/addMessage.sh" --id "${CMD}" --delete
;;

AM_*)
echo "${ME}: ERROR: Unknown error ID: '${CMD}'." >&2
exit 1
;;

*) # Arbitrary command
eval "${*}"
RET=$?
if [[ ${RET} -eq 0 ]]; then
echo "Executed ${*}"
else
echo "Unable to execute ${*}"
fi
;;
esac

exit ${RET}

0 comments on commit 7c0a31b

Please sign in to comment.