#!/bin/bash
set -euo pipefail

DIR="$(cd "$(dirname "$0")" && pwd)"
PYTHON="/usr/bin/python3"
PATCHER="$DIR/scripts/patch_claude_zh_cn.py"

if [ ! -x "$PYTHON" ]; then
  PYTHON="$(command -v python3)"
fi

check_release_update() {
  if [ "${CLAUDE_ZH_SKIP_UPDATE_CHECK:-0}" = "1" ]; then
    return
  fi

  "$PYTHON" - "$DIR/resources/release.json" 2>/dev/null <<'PY'
import json
import re
import sys
import urllib.request

metadata_path = sys.argv[1]
try:
    with open(metadata_path, "r", encoding="utf-8") as f:
        metadata = json.load(f)
    repo = metadata["repo"]
    current = str(metadata["release"])
    req = urllib.request.Request(
        f"https://api.github.com/repos/{repo}/releases/latest",
        headers={
            "Accept": "application/vnd.github+json",
            "User-Agent": "claude-desktop-zh-cn-update-check",
        },
    )
    with urllib.request.urlopen(req, timeout=3) as response:
        latest = str(json.load(response)["tag_name"])

    def version_key(value):
        parts = [int(part) for part in re.findall(r"\d+", value)]
        return parts + [0] * (3 - len(parts))

    if version_key(latest) > version_key(current):
        print(
            f"检测到 GitHub Releases 已发布新版 {latest}，当前脚本包为 {current}。"
            "建议及时更新。本次操作会继续执行。"
        )
except Exception:
    pass
PY
}

check_release_update

echo "Claude Desktop 中文补丁"
echo "目录: $DIR"
echo

ACTION="${CLAUDE_ACTION:-}"
SKIP_ASAR_PATCH="${CLAUDE_SKIP_ASAR_PATCH:-0}"
if [ -z "$ACTION" ]; then
  echo "请选择操作："
  echo "  [1] 安装中文补丁(官方订阅与第三方api均可使用：Cowork 沙箱/工作区可能不可用)"
  echo "  [2] 安装中文补丁(第三方api可用：安全模式，第三方模型需借助ccswitch映射(建议第三方api选此项))"
  echo "  [3] 恢复原样 / 卸载补丁"
  echo "  [4] 禁止自动更新"
  echo "  [5] 允许自动更新"
  echo
  read -rp "请输入选项 [1/2/3/4/5，默认 1]: " action_choice
  case "${action_choice:-1}" in
    2) ACTION="install"; SKIP_ASAR_PATCH="1" ;;
    3) ACTION="restore" ;;
    4) ACTION="disable-updates" ;;
    5) ACTION="enable-updates" ;;
    *) ACTION="install" ;;
  esac
  echo
fi

if [ "$ACTION" = "uninstall" ]; then
  ACTION="restore"
fi

# Language selection
if [ "$ACTION" = "restore" ] || [ "$ACTION" = "disable-updates" ] || [ "$ACTION" = "enable-updates" ]; then
  LANG_CODE=""
elif [ -z "${CLAUDE_LANG:-}" ]; then
  echo "请选择要安装的语言："
  echo "  [1] 简体中文"
  echo "  [2] 繁体中文（中国台湾）"
  echo "  [3] 繁体中文（中国香港）"
  echo
  read -rp "请输入选项 [1/2/3，默认 1]: " choice
  case "${choice:-1}" in
    2) LANG_CODE="zh-TW" ;;
    3) LANG_CODE="zh-HK" ;;
    *) LANG_CODE="zh-CN" ;;
  esac
  echo
else
  LANG_CODE="$CLAUDE_LANG"
fi

SKIP_ASAR_ARG=""
case "$SKIP_ASAR_PATCH" in
  1|true|TRUE|yes|YES|y|Y) SKIP_ASAR_ARG="--skip-asar-patch" ;;
esac

if [ "$ACTION" = "install" ]; then
  echo "选择的语言: $LANG_CODE"
  if [ -n "$SKIP_ASAR_ARG" ]; then
    echo "安全模式: 跳过结构性 app.asar 补丁，仅应用等长菜单汉化补丁"
  fi
  echo
fi

NEEDS_SUDO=1
if [ "$ACTION" = "disable-updates" ] || [ "$ACTION" = "enable-updates" ]; then
  NEEDS_SUDO=0
fi
for arg in "$@"; do
  if [ "$arg" = "--dry-run" ]; then
    NEEDS_SUDO=0
  fi
done

if [ "$(id -u)" -ne 0 ] && [ "$NEEDS_SUDO" -eq 1 ]; then
  echo "需要管理员权限来替换 /Applications/Claude.app。"
  echo "请按提示输入这台 Mac 的登录密码。"
  echo
  if [ "$ACTION" = "restore" ]; then
    sudo "$PYTHON" "$PATCHER" --user-home "$HOME" --restore --launch "$@"
  else
    sudo "$PYTHON" "$PATCHER" --user-home "$HOME" --lang "$LANG_CODE" --launch ${SKIP_ASAR_ARG:+"$SKIP_ASAR_ARG"} "$@"
  fi
  STATUS=$?
  echo
  echo "按回车退出。"
  read -r _
  exit "$STATUS"
fi

USER_HOME="$HOME"
if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then
  USER_HOME="$("$PYTHON" -c 'import pwd, sys; print(pwd.getpwnam(sys.argv[1]).pw_dir)' "$SUDO_USER" 2>/dev/null || true)"
  if [ -z "$USER_HOME" ] || [ ! -d "$USER_HOME" ]; then
    USER_HOME="$(eval echo "~$SUDO_USER")"
  fi
fi

if [ "$ACTION" = "restore" ]; then
  "$PYTHON" "$PATCHER" --user-home "$USER_HOME" --restore --launch "$@"
elif [ "$ACTION" = "disable-updates" ]; then
  "$PYTHON" "$PATCHER" --user-home "$USER_HOME" --set-auto-updates disabled "$@"
elif [ "$ACTION" = "enable-updates" ]; then
  "$PYTHON" "$PATCHER" --user-home "$USER_HOME" --set-auto-updates enabled "$@"
else
  "$PYTHON" "$PATCHER" --user-home "$USER_HOME" --lang "$LANG_CODE" --launch ${SKIP_ASAR_ARG:+"$SKIP_ASAR_ARG"} "$@"
fi

echo
echo "完成。按回车退出。"
read -r _
