#!/bin/bash

# 检测系统类型和 CPU 架构
detect_system() {
  OS=$(uname)
  ARCH=$(uname -m)
  SYSTEM=""

  if [ "$OS" = "Darwin" ]; then
    SYSTEM="macOS"
  elif [ "$OS" = "Linux" ]; then
    if [ -f /etc/os-release ]; then
      . /etc/os-release
      if [ "$ID" = "debian" ] || [ "$ID" = "ubuntu" ]; then
        SYSTEM="Debian/Ubuntu"
      elif [ "$ID" = "centos" ] || [ "$ID" = "rhel" ] || [ "$ID" = "fedora" ]; then
        SYSTEM="CentOS/RHEL/Fedora"
      else
        echo "不支持的 Linux 发行版: $ID"
        exit 1
      fi
    else
      echo "无法检测 Linux 发行版"
      exit 1
    fi
  else
    echo "不支持的系统: $OS"
    exit 1
  fi

  if [ "$ARCH" != "x86_64" ] && [ "$ARCH" != "aarch64" ]; then
    echo "不支持的 CPU 架构: $ARCH"
    exit 1
  fi
}

# 下载和安装 socks2trojan
install_socks2trojan() {
  detect_system
  echo "检测到系统: $SYSTEM, 架构: $ARCH"

  if [ "$SYSTEM" = "macOS" ]; then
    if [ "$ARCH" = "x86_64" ]; then
      wget -q https://proxy.v2gh.com/https://github.com/developer-over/socks2trojan/blob/main/socks2trojan-macos-x86_64.tar.gz
      tar -zxvf socks2trojan-macos-x86_64.tar.gz
      rm -f socks2trojan-macos-x86_64.tar.gz
    elif [ "$ARCH" = "aarch64" ]; then
      wget -q https://proxy.v2gh.com/https://github.com/developer-over/socks2trojan/blob/main/socks2trojan-macos-aarch64.tar.gz
      tar -zxvf socks2trojan-macos-aarch64.tar.gz
      rm -f socks2trojan-macos-aarch64.tar.gz
    fi
    chmod +x socks2trojan
    sudo mv socks2trojan /usr/local/bin/
    echo "macOS 安装完成"
  elif [ "$SYSTEM" = "Debian/Ubuntu" ]; then
    if [ "$ARCH" = "x86_64" ]; then
      wget -q https://proxy.v2gh.com/https://github.com/developer-over/socks2trojan/releases/latest/download/socks2trojan_amd64.deb
      sudo apt install -y ./socks2trojan_amd64.deb
      rm -f socks2trojan_amd64.deb
    elif [ "$ARCH" = "aarch64" ]; then
      wget -q https://proxy.v2gh.com/https://github.com/developer-over/socks2trojan/releases/latest/download/socks2trojan_arm64.deb
      sudo apt install -y ./socks2trojan_arm64.deb
      rm -f socks2trojan_arm64.deb
    fi
    echo "Debian/Ubuntu 安装完成"
  elif [ "$SYSTEM" = "CentOS/RHEL/Fedora" ]; then
    if [ "$ARCH" = "x86_64" ]; then
      wget -q https://proxy.v2gh.com/https://github.com/developer-over/socks2trojan/releases/latest/download/socks2trojan.x86_64.rpm
      sudo yum install -y ./socks2trojan.x86_64.rpm
      rm -f socks2trojan.x86_64.rpm
    elif [ "$ARCH" = "aarch64" ]; then
      wget -q https://proxy.v2gh.com/https://github.com/developer-over/socks2trojan/releases/latest/download/socks2trojan.aarch64.rpm
      sudo yum install -y ./socks2trojan.aarch64.rpm
      rm -f socks2trojan.aarch64.rpm
    fi
    echo "CentOS/RHEL/Fedora 安装完成"
  fi
}

# 设置代理
set_proxy() {
  local trojan_url="$1"

  if [ -z "$trojan_url" ]; then
    echo "错误：请提供 Trojan 地址，例如 --trojan 'trojan://...'。"
    exit 1
  fi

  # 安装 socks2trojan（如果未安装）
  if ! command -v socks2trojan &> /dev/null; then
    echo "socks2trojan 未安装，正在安装..."
    install_socks2trojan
  fi

  # 配置 Trojan 地址并启动服务
  socks2trojan set --trojan "$trojan_url"
  socks2trojan start
  echo "代理服务已启动"
}

# 删除代理和软件
remove_proxy() {
  detect_system

  # 停止服务
  if command -v socks2trojan &> /dev/null; then
    socks2trojan stop
    echo "代理服务已停止"
  fi

  # 根据系统类型卸载软件
  if [ "$SYSTEM" = "macOS" ]; then
    sudo rm -f /usr/local/bin/socks2trojan
    echo "macOS 上 socks2trojan 已删除"
  elif [ "$SYSTEM" = "Debian/Ubuntu" ]; then
    sudo apt remove -y socks2trojan
    echo "Debian/Ubuntu 上 socks2trojan 已卸载"
  elif [ "$SYSTEM" = "CentOS/RHEL/Fedora" ]; then
    sudo yum remove -y socks2trojan
    echo "CentOS/RHEL/Fedora 上 socks2trojan 已卸载"
  fi
}

# 主逻辑
case "$1" in
  install)
    install_socks2trojan
    ;;
  set)
    shift
    while [[ "$#" -gt 0 ]]; do
      case $1 in
        --trojan) trojan_url="$2"; shift ;;
        *) echo "未知参数：$1"; exit 1 ;;
      esac
      shift
    done
    set_proxy "$trojan_url"
    ;;
  remove)
    remove_proxy
    ;;
  *)
    echo "用法：$0 {install | set --trojan <trojan_url> | remove}"
    echo "示例："
    echo "  安装软件：$0 install"
    echo "  设置代理：$0 set --trojan 'trojan://ec0c3165-b2d7-428b-a5cf-5a683412cb59@138.128.223.175:443?sni=m.ctrip.com&allowInsecure=1'"
    echo "  删除代理：$0 remove"
    exit 1
    ;;
esac
