diff --git a/.includes/sas.sh b/.includes/sas.sh new file mode 100644 index 0000000..3f61d17 --- /dev/null +++ b/.includes/sas.sh @@ -0,0 +1,90 @@ +#!/usr/bin/env bash + +validate_sas_script() { + local scriptPath="$1" + + [[ -s "$scriptPath" ]] && python3 -c \ + 'import ast, pathlib, sys; ast.parse(pathlib.Path(sys.argv[1]).read_text(encoding="utf-8"))' \ + "$scriptPath" 2> /dev/null +} + +start_installed_sas() { + local installedSas="$1" + local warning="$2" + + if [[ -x "$installedSas" ]] && validate_sas_script "$installedSas"; then + msgbox "$warning Starting the installed version instead." + "$installedSas" + return $? + fi + + msgbox "$warning No working copy of the Stormux Assistance Service is installed." + return 1 +} + +run_stormux_assistance_service() { + local installedSas="/usr/local/bin/sas" + local repositoryUrl="https://git.stormux.org/storm/sas" + local tempDir + local repositoryDir="" + local latestSas + local stagedSas="/usr/local/bin/.sas.new.$$" + local attempt + + if ! command -v ii &> /dev/null; then + infobox "Installing the ii IRC client required by the Stormux Assistance Service..." + install_package ii + if ! command -v ii &> /dev/null; then + msgbox "The ii IRC client could not be installed. The Stormux Assistance Service cannot start." + return 1 + fi + fi + + tempDir=$(mktemp -d /tmp/configure-stormux-sas.XXXXXX) || { + start_installed_sas "$installedSas" \ + "A temporary directory could not be created to check for an update." + return $? + } + + infobox "Checking for the latest Stormux Assistance Service..." + for attempt in 1 2 3; do + if GIT_TERMINAL_PROMPT=0 timeout 30s git clone --quiet --depth 1 \ + "$repositoryUrl" "$tempDir/repository-$attempt"; then + repositoryDir="$tempDir/repository-$attempt" + break + fi + done + + if [[ -z "$repositoryDir" ]]; then + rm -r -- "$tempDir" + start_installed_sas "$installedSas" \ + "The latest Stormux Assistance Service could not be downloaded." + return $? + fi + + latestSas="$repositoryDir/sas.py" + if ! validate_sas_script "$latestSas"; then + rm -r -- "$tempDir" + start_installed_sas "$installedSas" \ + "The downloaded Stormux Assistance Service failed validation." + return $? + fi + + if [[ ! -x "$installedSas" ]] || ! cmp -s "$latestSas" "$installedSas"; then + # sudoFlags is initialized by configure-stormux.sh before this file is sourced. + # shellcheck disable=SC2154 + if ! sudo "${sudoFlags[@]}" install -m 0755 "$latestSas" "$stagedSas" || + ! sudo "${sudoFlags[@]}" mv -f "$stagedSas" "$installedSas"; then + sudo "${sudoFlags[@]}" rm -f "$stagedSas" + rm -r -- "$tempDir" + start_installed_sas "$installedSas" \ + "The Stormux Assistance Service could not be updated." + return $? + fi + fi + + rm -r -- "$tempDir" + "$installedSas" +} + +run_stormux_assistance_service diff --git a/configure-stormux.sh b/configure-stormux.sh index 21683a8..421cd62 100755 --- a/configure-stormux.sh +++ b/configure-stormux.sh @@ -108,6 +108,9 @@ while [[ "$choice" != "Exit" ]]; do "Get help on IRC") source .includes/irc.sh ;; + "Stormux Assistance Service") + source .includes/sas.sh + ;; "Update configure-stormux") sudo "${sudoFlags[@]}" git pull exit $? @@ -141,6 +144,7 @@ while [[ "$choice" != "Exit" ]]; do ) options+=("Set timezone" "Get help on IRC" + "Stormux Assistance Service" "Update configure-stormux" "Update EEPROM" "Update RHVoice dictionaries"