#!/bin/bash ######################################################################## # A shell script to install / upgrade SimpleHelp on a Linux system. # # Configure the installation directory below (install=...) or pass it in # as the first argument. This script will always download the latest # public release of SimpleHelp unless --source is specified. # # If an existing installation is detected the server will be overwritten # and the server's configuration will be preserved. # # Usage: # simplehelp-upgrade.sh [INSTALL_DIR] [--source ] # ######################################################################## set -euo pipefail trap 'echo "[ERROR] Script failed at line $LINENO" >&2; exit 1' ERR # Defaults install="/opt/SimpleHelp" source_arg="" # Initialise now=$(date +"%Y%m%d_%H%M%S") initial_dir="$PWD" # Download helper: uses curl if available, else wget download() { # usage: download [--stdout] if command -v curl >/dev/null 2>&1; then if [ "${2:-}" = "--stdout" ]; then curl -fsSL "$1" else curl -fsSLO "$1" fi elif command -v wget >/dev/null 2>&1; then if [ "${2:-}" = "--stdout" ]; then wget -qO- "$1" else wget -q "$1" fi else echo "[ERROR] Neither curl nor wget found on this system." >&2 exit 1 fi } # Parse arguments install_set="0" while [ $# -gt 0 ]; do case "$1" in --source) if [ -n "$2" ]; then source_arg="$2" shift 2 continue else echo "[ERROR] --source requires an argument (e.g. --source value)" >&2 exit 1 fi ;; -h|--help) echo "Usage: $0 [INSTALL_DIR] [--source ]"; exit 0 ;; --*) echo "[ERROR] Unknown option: $1" >&2; exit 1 ;; *) if [ "$install_set" = "0" ]; then install="$1" install_set="1" shift continue else echo "[ERROR] Unexpected argument: $1" >&2 exit 1 fi ;; esac shift done # Determine base download URL base_url="https://simple-help.com/releases" if [ -n "$source_arg" ]; then base_url="https://simple-help.com/releases/previous/$source_arg" fi # Check permissions parentdir="$(dirname "$install")" if [ ! -w "$parentdir" ]; then echo "[ERROR] Insufficient permissions to write installation files in $parentdir" >&2 exit 1 fi if [ -d "$install" ]; then if [ ! -w "$install" ]; then echo "[ERROR] Insufficient permissions to write installation files in $install" >&2 exit 1 fi fi # Query Version.txt to display the version being installed version_txt_url="$base_url/Version.txt" version_output=$(download "$version_txt_url" --stdout 2>/dev/null || true) if [ -z "$version_output" ]; then if [ -z "$source_arg" ]; then echo "[ERROR] No SimpleHelp release was found" else echo "[ERROR] No SimpleHelp release was found for source \"$source_arg\"" fi exit 1 fi version=$(echo "$version_output" | awk -F':' '/^Version[[:space:]]*/ { gsub(/^[ \t]+/, "", $2); print $2; exit }') if [ -n "$version" ]; then echo "--- Installing SimpleHelp Server v$version" else echo "[ERROR] Unable to identify SimpleHelp Server version." exit 1 fi echo "--- Managing SimpleHelp installation in $install" # Stop the server if [ -d "$install" ]; then echo "--- Stopping SimpleHelp..." cd "$install" sh serverstop.sh >/dev/null 2>&1 sleep 10 cd .. echo "--- Backing up the SimpleHelp installation to SimpleHelp_backup_$now" mv "$install" "SimpleHelp_backup_$now" fi uname_out=`uname -m` case "$uname_out" in *aarch64*|*arm64*) echo "--- Using the Linux ARM 64bit Release" sh_binary="SimpleHelp-linux-arm64.tar.gz" sh_url="$base_url/SimpleHelp-linux-arm64.tar.gz" ;; *aarch*|*arm*) echo "--- Using the Linux ARM 32bit Release" sh_binary="SimpleHelp-linux-arm32.tar.gz" sh_url="$base_url/SimpleHelp-linux-arm32.tar.gz" ;; *64*) echo "--- Using the Linux Intel 64bit Release" sh_binary="SimpleHelp-linux-amd64.tar.gz" sh_url="$base_url/SimpleHelp-linux-amd64.tar.gz" ;; *) echo "--- Using the Linux Intel 32bit Release" sh_binary="SimpleHelp-linux.tar.gz" sh_url="$base_url/SimpleHelp-linux.tar.gz" ;; esac rm -f "$sh_binary" echo "--- Downloading ($sh_url)..." download "$sh_url" if [ ! -s "$sh_binary" ]; then echo "[ERROR] Failed to download $sh_binary from $sh_url" exit 1 fi echo "--- Extracting..." tar -xzf "$sh_binary" >/dev/null rm -f "$sh_binary" if [ ! -d "SimpleHelp" ]; then echo "[ERROR] Archive did not extract the expected 'SimpleHelp' directory." exit 1 fi if [ "$install" != "$PWD/SimpleHelp" ]; then echo "--- Moving install to $install" mv SimpleHelp "$install" fi # Copy across the old configuration folder if [ -d "SimpleHelp_backup_$now/configuration" ]; then echo "--- Copying across configuration files" if command -v rsync >/dev/null 2>&1; then rsync -a "SimpleHelp_backup_$now/configuration/" "$install/configuration/" else ( shopt -s dotglob nullglob cp -R "SimpleHelp_backup_$now/configuration/"* "$install/configuration/" 2>/dev/null || true ) fi fi # Start the new server echo "--- Starting SimpleHelp" cd "$install" sh serverstart.sh cd "$initial_dir"