#!/usr/bin/env bash
# Generated by build-linux-packages.sh for MaQuan Sync Server 0.1.0.
set -Eeuo pipefail
base_url="${MAQUAN_SYNC_DOWNLOAD_BASE:-https://maquan.app/downloads/maquan-sync-server/0.1.0}"
version="0.1.0"
deb_amd64="maquan-sync-server_0.1.0_amd64.deb"; deb_amd64_sha="b240d906dfb31cfc6264a86fcc910f14e6fcffb9683cb2b2dbc7d4c71678b2ac"
deb_arm64="maquan-sync-server_0.1.0_arm64.deb"; deb_arm64_sha="811e4147d412ab7927ec2f8d912578f05eb3bab814cf933e2c50f76073189ad6"
rpm_x86_64="maquan-sync-server-0.1.0-1.x86_64.rpm"; rpm_x86_64_sha="152469707aef05cfb613e6e26a8605797f6c171684566d7e63f1bb879df5d7b5"
rpm_aarch64="maquan-sync-server-0.1.0-1.aarch64.rpm"; rpm_aarch64_sha="9abff89021a9e42f63e7331ffafa91f959195ffa360b228ce487628b35cfc517"
deb_armhf="maquan-sync-server_0.1.0_armhf.deb"; deb_armhf_sha="1fcc9c9c8f241d37c1d2539b6de053674cf2c535380c4778968ecaf44c4bdfe3"
rpm_armv7hl="maquan-sync-server-0.1.0-1.armv7hl.rpm"; rpm_armv7hl_sha="400abfbddaf7c9968313a98a27fd9ee78d6aa94a650543f01438298b9bddfe5d"

mode="${1:-}"
case "$mode" in
  --help|-h) echo "Usage: bash install-maquan-sync-server.sh [--dry-run]"; exit 0 ;;
  --dry-run|'') ;;
  *) echo "Unknown option: $mode" >&2; exit 2 ;;
esac
arch="$(uname -m)"
if command -v apt-get >/dev/null && command -v dpkg >/dev/null; then arch="$(dpkg --print-architecture)"; fi
case "$arch" in
  x86_64|amd64) deb_file="$deb_amd64"; deb_sha="$deb_amd64_sha"; rpm_file="$rpm_x86_64"; rpm_sha="$rpm_x86_64_sha" ;;
  aarch64|arm64) deb_file="$deb_arm64"; deb_sha="$deb_arm64_sha"; rpm_file="$rpm_aarch64"; rpm_sha="$rpm_aarch64_sha" ;;
  armv7l|armv8l|armhf) deb_file="$deb_armhf"; deb_sha="$deb_armhf_sha"; rpm_file="$rpm_armv7hl"; rpm_sha="$rpm_armv7hl_sha" ;;
  *) echo "Unsupported CPU architecture: $(uname -m) (need x86_64, aarch64 or ARMv7 hard-float)" >&2; exit 2 ;;
esac
if command -v apt-get >/dev/null; then file="$deb_file"; expected="$deb_sha"; manager=apt-get
elif command -v dnf >/dev/null; then file="$rpm_file"; expected="$rpm_sha"; manager=dnf
elif command -v yum >/dev/null; then file="$rpm_file"; expected="$rpm_sha"; manager=yum
else echo "Need apt, dnf or yum; OpenWrt is not supported by these packages." >&2; exit 4; fi
if [ "$mode" = --dry-run ]; then printf '%s\n%s\n' "$base_url/$file" "$expected"; exit 0; fi
if [ "$(id -u)" -ne 0 ]; then exec sudo --preserve-env=MAQUAN_SYNC_DOWNLOAD_BASE bash "$0" "$@"; fi
command -v sha256sum >/dev/null || { echo "sha256sum is required" >&2; exit 3; }
test -d /run/systemd/system || { echo "A running systemd system is required." >&2; exit 4; }
work="$(mktemp -d)"; trap 'rm -rf "$work"' EXIT
fetch() { if command -v curl >/dev/null; then curl -fL --retry 3 -o "$2" "$1"; elif command -v wget >/dev/null; then wget -O "$2" "$1"; else echo "curl or wget is required" >&2; exit 3; fi; }
install_file() {
  local name="$1" expected="$2" path="$work/$1"
  echo "Downloading MaQuan Sync Server $version: $name"
  fetch "$base_url/$name" "$path"
  echo "$expected  $path" | sha256sum -c -
  if command -v apt-get >/dev/null; then apt-get update; DEBIAN_FRONTEND=noninteractive apt-get install -y "$path"
  elif command -v dnf >/dev/null; then dnf install -y "$path"
  elif command -v yum >/dev/null; then yum install -y "$path"
  else echo "No supported package manager (apt, dnf or yum)." >&2; exit 4; fi
}
install_file "$file" "$expected"
systemctl enable --now maquan-sync.service
systemctl is-active --quiet maquan-sync.service || { journalctl -u maquan-sync.service -n 30 --no-pager; exit 5; }
systemctl --no-pager --full status maquan-sync.service
echo "Installed. First setup is local only: ssh -L 8788:127.0.0.1:8788 root@your-server, then open http://127.0.0.1:8788"
