#!/usr/bin/env bash
set -euo pipefail

APP_DIR="/opt/vmqos"
DOWNLOAD_URL="https://pan.halocloud.net/?f=/vmqos/vmqos.zip"
TMP_ZIP="/tmp/vmqos_update.zip"
TMP_DIR="/tmp/vmqos_update"

if [[ $EUID -ne 0 ]]; then
  echo "请用 root 运行"
  exit 1
fi

echo "===> 下载更新包"
curl -fL "$DOWNLOAD_URL" -o "$TMP_ZIP"

echo "===> 输入解压密码"
read -r -s -p "ZIP password: " ZIP_PW
echo

if [[ -z "$ZIP_PW" ]]; then
  echo "密码不能为空"
  exit 1
fi

rm -rf "$TMP_DIR"
mkdir -p "$TMP_DIR"

echo "===> 解压更新包"
unzip -o -P "$ZIP_PW" "$TMP_ZIP" -d "$TMP_DIR"

echo "===> 覆盖更新（保留 config.py）"

rsync -av \
  --exclude="config.py" \
  --exclude=".venv" \
  --exclude="__pycache__" \
  "$TMP_DIR"/ "$APP_DIR"/

echo "===> 重启 vmqos"
systemctl restart vmqos

echo "===> 更新完成"
systemctl status vmqos --no-pager