#!/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 -rf -- "$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 -rf -- "$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 -rf -- "$tempDir" start_installed_sas "$installedSas" \ "The Stormux Assistance Service could not be updated." return $? fi fi rm -rf -- "$tempDir" "$installedSas" } run_stormux_assistance_service