#!/usr/bin/env bash
# Separate Back2Home UOS Desktop 20 x86_64 compatibility-test bootstrap.
set -euo pipefail
[ "$(id -u)" -eq 0 ] || { echo 'Run from your normal desktop terminal with sudo bash.' >&2; exit 1; }
# shellcheck disable=SC1091
. /etc/os-release
if [ "${ID:-}" != uos ] || [ "${VERSION_ID:-}" != 20 ] || [ "$(uname -m)" != x86_64 ]; then
  echo 'This test installer is for UOS Desktop 20 x86_64 only.' >&2; exit 1
fi
if [ -z "${SUDO_USER:-}" ] || [ "$SUDO_USER" = root ]; then
  echo 'Use the normal desktop user, not a root login: curl -fsSL https://back2home.app/install-uos20.sh | sudo bash' >&2; exit 1
fi
for tool in curl sha256sum tar; do command -v "$tool" >/dev/null || { echo "$tool is required." >&2; exit 1; }; done
readonly RELEASE_URL='https://back2home.app/downloads/back2home-linux-0.2.3-uos20-x64.tar.gz'
readonly RELEASE_SHA256='78a13d7182c9cc00ffb379d283aeda0abb8f4e9b8a7673b79ce3254cfe760a21'
stage="$(mktemp -d /tmp/back2home-uos-install.XXXXXXXX)"
cleanup() { case "$stage" in /tmp/back2home-uos-install.*) rm -rf -- "$stage" ;; esac; }
trap cleanup EXIT
curl --fail --silent --show-error --location --proto '=https' --proto-redir '=https' --tlsv1.2 "$RELEASE_URL" -o "$stage/release.tar.gz"
printf '%s  %s\n' "$RELEASE_SHA256" "$stage/release.tar.gz" | sha256sum --check --status || { echo 'Integrity check failed; nothing installed.' >&2; exit 1; }
tar -xzf "$stage/release.tar.gz" --no-same-owner -C "$stage"
bash "$stage/back2home-linux-0.2.3-uos20-x64/install.sh"
