#!/bin/sh # ScolioSim Helper - installer for macOS and Linux. # # Paste this line in Terminal (it needs no administrator rights and never asks for a password): # curl -fsSL https://scoliosim.com/segment/helper-install.sh | sh # # It installs, for this user only, the small program through which the ScolioSim web app installs, # runs and removes TotalSegmentator on this computer. Everything goes into one folder: # macOS: ~/Library/Application Support/ScolioSim/Helper # Linux: ~/.local/share/scoliosim/helper (or $XDG_DATA_HOME/scoliosim/helper) # What it does: # 1. uv, Astral's Python manager (from GitHub), into that folder; it brings the helper its own Python 3.12. # 2. The helper itself, scoliosim_helper.py and worker.py, from scoliosim.com/segment/helper/. # 3. The link scoliosim-helper://, with which the page starts the helper when it is not running: # on macOS a small "ScolioSim Helper.app" in that folder, on Linux a .desktop file. # 4. uninstall.sh, used by "Uninstall the helper" in ScolioSim (or run it yourself), and starts the helper. # TotalSegmentator itself (a few GB) is installed later, from ScolioSim, when you ask. # Running this again updates the helper and keeps TotalSegmentator. # Nothing of a patient is sent anywhere: the CT is read and segmented on this computer. # Everything runs from main, at the end: a download cut half way runs nothing set -u SOURCE=${SCOLIOSIM_HELPER_SOURCE:-https://scoliosim.com/segment/helper} PORT=41789 case "${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}" in es*) ES=1 ;; *) ES= ;; esac # Colours only in a terminal: the installer app for macOS keeps this text in its log if [ -t 1 ]; then C_SAY=$(printf '\033[36m'); C_WARN=$(printf '\033[33m'); C_FAIL=$(printf '\033[31m'); C_END=$(printf '\033[0m') else C_SAY=; C_WARN=; C_FAIL=; C_END= fi # say "English" "Español" say() { if [ -n "$ES" ]; then printf '%s%s%s\n' "$C_SAY" "$2" "$C_END"; else printf '%s%s%s\n' "$C_SAY" "$1" "$C_END"; fi; } warn() { if [ -n "$ES" ]; then printf '%s%s%s\n' "$C_WARN" "$2" "$C_END"; else printf '%s%s%s\n' "$C_WARN" "$1" "$C_END"; fi; } fail() { if [ -n "$ES" ]; then printf '%s%s%s\n' "$C_FAIL" "$2" "$C_END" >&2; else printf '%s%s%s\n' "$C_FAIL" "$1" "$C_END" >&2; fi exit 1 } # A file of the helper: from the site, or from a folder when testing (SCOLIOSIM_HELPER_SOURCE=/path) fetch() { case "$SOURCE" in http://*|https://*) curl -fsSL --retry 3 --retry-delay 5 "$SOURCE/$1" -o "$2" ;; *) cp "$SOURCE/$1" "$2" ;; esac } LSREGISTER=/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister main() { OS=$(uname -s) ARCH=$(uname -m) case "$OS-$ARCH" in Darwin-arm64|Darwin-x86_64) DIR=${SCOLIOSIM_HELPER_DIR:-"$HOME/Library/Application Support/ScolioSim/Helper"} if [ "$ARCH" = x86_64 ] && [ "$(/usr/sbin/sysctl -n hw.optional.arm64 2>/dev/null)" = 1 ]; then ARCH=arm64 # this Terminal runs under Rosetta; the Mac is Apple silicon fi if [ "$ARCH" = arm64 ]; then TRIPLE=aarch64-apple-darwin; else TRIPLE=x86_64-apple-darwin; fi ;; Linux-x86_64|Linux-aarch64) DIR=${SCOLIOSIM_HELPER_DIR:-"${XDG_DATA_HOME:-$HOME/.local/share}/scoliosim/helper"} # The static (musl) uv runs on any Linux TRIPLE="$ARCH-unknown-linux-musl" ;; *) fail "The ScolioSim helper is for Windows, macOS and Linux (64-bit); this is $OS $ARCH." \ "El asistente de ScolioSim es para Windows, macOS y Linux (64 bits); este equipo es $OS $ARCH." ;; esac command -v curl >/dev/null 2>&1 || fail "curl is needed and was not found." "Hace falta curl y no se ha encontrado." say "ScolioSim: installing the helper for this user, in $DIR" \ "ScolioSim: instalando el asistente para este usuario, en $DIR" # A helper already running (an earlier version) stops, so that the new one starts curl -s -m 3 -X POST "http://127.0.0.1:$PORT/quit" >/dev/null 2>&1 && sleep 1 mkdir -p "$DIR" || fail "Could not create $DIR." "No se pudo crear $DIR." FREE_GB=$(df -Pk "$DIR" | awk 'NR==2 {print int($4/1048576)}') if [ "${FREE_GB:-0}" -lt 15 ]; then warn "Only $FREE_GB GB free: TotalSegmentator, which ScolioSim installs afterwards, needs about 15 GB." \ "Solo quedan $FREE_GB GB libres: TotalSegmentator, que ScolioSim instala después, necesita unos 15 GB." fi # 1. uv, the latest release, into the folder say "Downloading uv (Astral's Python manager)..." "Descargando uv (el gestor de Python de Astral)..." TMP=$(mktemp -d "${TMPDIR:-/tmp}/scoliosim-helper.XXXXXX") || fail "No temporary folder." "No hay carpeta temporal." if curl -fsSL --retry 3 --retry-delay 5 "https://github.com/astral-sh/uv/releases/latest/download/uv-$TRIPLE.tar.gz" -o "$TMP/uv.tar.gz" \ && tar -xzf "$TMP/uv.tar.gz" -C "$TMP" && [ -f "$TMP/uv-$TRIPLE/uv" ]; then cp "$TMP/uv-$TRIPLE/uv" "$DIR/uv" && chmod +x "$DIR/uv" # curl does not quarantine what it downloads, but macOS would not run uv if it were if [ "$OS" = Darwin ]; then xattr -d com.apple.quarantine "$DIR/uv" 2>/dev/null || true; fi elif [ -x "$DIR/uv" ]; then warn "uv could not be downloaded: using the one already installed." "No se pudo descargar uv: se usa el ya instalado." else rm -rf "$TMP" fail "uv could not be downloaded. Check the internet connection and try again." \ "No se pudo descargar uv. Compruebe la conexión a internet e inténtelo de nuevo." fi rm -rf "$TMP" # 2. The helper say "Downloading the helper..." "Descargando el asistente..." for f in scoliosim_helper.py worker.py; do if ! fetch "$f" "$DIR/$f.new" || ! mv "$DIR/$f.new" "$DIR/$f"; then fail "Could not download $f. Check the internet connection and try again." \ "No se pudo descargar $f. Compruebe la conexión a internet e inténtelo de nuevo." fi done say "Preparing the helper's own Python (about 30 MB)..." "Preparando el Python propio del asistente (unos 30 MB)..." # Always uv's own Python, in this folder: never one the computer already has if ! UV_PYTHON_INSTALL_DIR="$DIR/python" UV_CACHE_DIR="$DIR/cache" "$DIR/uv" venv --quiet --python 3.12 --managed-python \ --allow-existing "$DIR/runtime" /dev/null; then "$DIR/start.sh" fi n=0 until curl -s -m 2 "http://127.0.0.1:$PORT/status" >/dev/null 2>&1; do n=$((n + 1)) if [ "$n" -ge 15 ]; then warn "The helper was installed but did not answer yet. In ScolioSim, press 'Already installed? Start it'." \ "El asistente se ha instalado pero aún no responde. En ScolioSim, pulse «¿Ya está instalado? Póngalo en marcha»." return 0 fi sleep 1 done echo say "Done. The ScolioSim helper is installed and running. Go back to ScolioSim in the browser (Chrome or Edge) and press 'Connect to the helper': it guides the rest. You can close this window." \ "Listo. El asistente de ScolioSim está instalado y en marcha. Vuelva a ScolioSim en el navegador (Chrome o Edge) y pulse «Conectar con el asistente»: le guiará en lo demás. Puede cerrar esta ventana." } # The command that starts the helper (both systems), and the uninstaller's common part write_start() { cat > "$DIR/start.sh" <<'EOF' #!/bin/sh # Starts the ScolioSim helper apart from whoever asks (a terminal, the browser through scoliosim-helper://), # and returns at once. If it is already running, the new copy stops by itself. DIR=$(cd "$(dirname "$0")" && pwd) if command -v setsid >/dev/null 2>&1; then setsid "$DIR/runtime/bin/python3" -I "$DIR/scoliosim_helper.py" "$@" /dev/null 2>&1 & else nohup "$DIR/runtime/bin/python3" -I "$DIR/scoliosim_helper.py" "$@" /dev/null 2>&1 & fi EOF chmod +x "$DIR/start.sh" } install_mac() { write_start APPDIR="$DIR/ScolioSim Helper.app" rm -rf "$APPDIR" mkdir -p "$APPDIR/Contents/MacOS" "$APPDIR/Contents/Resources" # The app is the helper itself (exec): macOS keeps it running as long as the helper runs. No Dock icon. cat > "$APPDIR/Contents/MacOS/scoliosim-helper" <<'EOF' #!/bin/sh # Started by macOS when the ScolioSim page opens scoliosim-helper:// DIR=$(cd "$(dirname "$0")/../../.." && pwd) exec "$DIR/runtime/bin/python3" -I "$DIR/scoliosim_helper.py" --protocol /dev/null 2>&1 EOF chmod +x "$APPDIR/Contents/MacOS/scoliosim-helper" VERSION=$(sed -n 's/^VERSION = "\([0-9.]*\)".*/\1/p' "$DIR/scoliosim_helper.py") cat > "$APPDIR/Contents/Info.plist" < CFBundleIdentifiercom.scoliosim.helper CFBundleNameScolioSim Helper CFBundleDisplayNameScolioSim Helper CFBundleExecutablescoliosim-helper CFBundlePackageTypeAPPL CFBundleInfoDictionaryVersion6.0 CFBundleShortVersionString${VERSION:-1} CFBundleVersion${VERSION:-1} LSUIElement LSMinimumSystemVersion11.0 CFBundleURLTypes CFBundleURLNamecom.scoliosim.helper CFBundleURLSchemesscoliosim-helper EOF # Made here, not downloaded: no quarantine, so macOS opens it without asking (nor anything else in the folder) xattr -dr com.apple.quarantine "$DIR" 2>/dev/null || true if [ -x "$LSREGISTER" ]; then "$LSREGISTER" -f "$APPDIR" || warn "The link scoliosim-helper:// could not be registered: start the helper again with this same line." \ "No se pudo registrar el enlace scoliosim-helper://: vuelva a poner en marcha el asistente con esta misma orden." fi cat > "$DIR/uninstall.sh" <<'EOF' #!/bin/sh # Removes the ScolioSim helper from this Mac: the helper, TotalSegmentator, its model weights and its Python. DIR=$(cd "$(dirname "$0")" && pwd) # Only the helper's own folder is removed, never a home folder or the root [ -f "$DIR/scoliosim_helper.py" ] && [ "$DIR" != "$HOME" ] && [ "$DIR" != / ] || { echo "This is not the folder of the ScolioSim helper: nothing was removed."; exit 1; } curl -s -m 3 -X POST http://127.0.0.1:41789/quit >/dev/null 2>&1 sleep 2 /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -u "$DIR/ScolioSim Helper.app" 2>/dev/null rm -rf "$DIR" rmdir "$(dirname "$DIR")" 2>/dev/null echo "The ScolioSim helper has been removed." EOF } install_linux() { write_start APPS="${XDG_DATA_HOME:-$HOME/.local/share}/applications" mkdir -p "$APPS" # Exec quotes the path: the spec wants \ " ` $ escaped inside quotes ESCAPED=$(printf '%s' "$DIR/start.sh" | sed 's/[\\"`$]/\\&/g') cat > "$APPS/scoliosim-helper.desktop" < "$DIR/uninstall.sh" <<'EOF' #!/bin/sh # Removes the ScolioSim helper from this computer: the helper, TotalSegmentator, its model weights and its Python. DIR=$(cd "$(dirname "$0")" && pwd) # Only the helper's own folder is removed, never a home folder or the root [ -f "$DIR/scoliosim_helper.py" ] && [ "$DIR" != "$HOME" ] && [ "$DIR" != / ] || { echo "This is not the folder of the ScolioSim helper: nothing was removed."; exit 1; } APPS="${XDG_DATA_HOME:-$HOME/.local/share}/applications" curl -s -m 3 -X POST http://127.0.0.1:41789/quit >/dev/null 2>&1 sleep 2 rm -f "$APPS/scoliosim-helper.desktop" LIST="${XDG_CONFIG_HOME:-$HOME/.config}/mimeapps.list" if [ -f "$LIST" ]; then grep -v '^x-scheme-handler/scoliosim-helper=' "$LIST" > "$LIST.scoliosim" && mv "$LIST.scoliosim" "$LIST" fi command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$APPS" >/dev/null 2>&1 rm -rf "$DIR" rmdir "$(dirname "$DIR")" 2>/dev/null echo "The ScolioSim helper has been removed." EOF } # scoliosim-helper:// opens the .desktop file: through xdg-mime when there is one, else in mimeapps.list itself register_linux() { command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$1" >/dev/null 2>&1 if command -v xdg-mime >/dev/null 2>&1 && xdg-mime default scoliosim-helper.desktop x-scheme-handler/scoliosim-helper 2>/dev/null; then return 0 fi LIST="${XDG_CONFIG_HOME:-$HOME/.config}/mimeapps.list" mkdir -p "$(dirname "$LIST")" if [ -f "$LIST" ]; then grep -v '^x-scheme-handler/scoliosim-helper=' "$LIST" > "$LIST.scoliosim" || true else : > "$LIST.scoliosim" fi if grep -q '^\[Default Applications\]' "$LIST.scoliosim"; then awk '{ print } /^\[Default Applications\]/ { print "x-scheme-handler/scoliosim-helper=scoliosim-helper.desktop" }' "$LIST.scoliosim" > "$LIST" else { cat "$LIST.scoliosim"; printf '[Default Applications]\nx-scheme-handler/scoliosim-helper=scoliosim-helper.desktop\n'; } > "$LIST" fi rm -f "$LIST.scoliosim" } main "$@"