#!/usr/bin/env bash
set -euo pipefail
[ "$(id -u)" -eq 0 ] || { echo "请使用 sudo 运行"; exit 1; }
. /etc/arch-hotspot-release
URL="$UPDATE_BASE/$CHANNEL/current.json"
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
curl -fsSL "$URL" -o "$TMP/current.json"
FILE=$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(d["file"])' "$TMP/current.json")
NEWVER=$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(d["version"])' "$TMP/current.json")
if [ "$NEWVER" = "$VERSION" ]; then echo "已是最新版本 $VERSION"; exit 0; fi
curl -fsSL "$UPDATE_BASE/$CHANNEL/$FILE" -o "$TMP/update.tar.gz"
tar -xzf "$TMP/update.tar.gz" -C "$TMP"
PKG=$(find "$TMP" -mindepth 1 -maxdepth 1 -type d -name 'arch-hotspot-*' | head -1)
bash "$PKG/install.sh"
