#!/usr/bin/env bash
set -e

BASE_URL="https://alist.sian.one/direct/gostc"

OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)

# 只处理 Linux
if [[ "$OS" != "linux" ]]; then
  echo "This installer only supports Linux."
  exit 1
fi

GOOS="linux"

# ========== 工具检测和安装 ==========
check_and_install() {
  local tool=$1
  if ! command -v "$tool" >/dev/null 2>&1; then
    echo "Tool $tool not found, trying to install..."
    if command -v apt-get >/dev/null 2>&1; then
      sudo apt-get update && sudo apt-get install -y "$tool"
    elif command -v yum >/dev/null 2>&1; then
      sudo yum install -y "$tool"
    elif command -v dnf >/dev/null 2>&1; then
      sudo dnf install -y "$tool"
    elif command -v pacman >/dev/null 2>&1; then
      sudo pacman -Sy --noconfirm "$tool"
    else
      echo "No supported package manager found for installing $tool"
      exit 1
    fi
  fi
}

# 检查依赖
for tool in curl tar sudo chown chmod rm mv; do
  check_and_install "$tool"
done

# 映射 ARCH
case "$ARCH" in
  x86_64|amd64)   GOARCH="amd64" ;;
  i386|i686)      GOARCH="386" ;;
  armv5*)         GOARCH="arm_5" ;;
  armv6*)         GOARCH="arm_6" ;;
  armv7*)         GOARCH="arm_7" ;;
  arm64|aarch64)  GOARCH="arm64" ;;
  mips64)         GOARCH="mips64" ;;
  mips64el)       GOARCH="mips64le" ;;
  mips)           GOARCH="mips" ;;
  mipsel)         GOARCH="mipsle" ;;
  riscv64)        GOARCH="riscv64" ;;
  s390x)          GOARCH="s390x" ;;
  *) echo "Unsupported ARCH: $ARCH"; exit 1 ;;
esac

# 映射文件名（默认 amd64_v1）
case "${GOARCH}" in
  amd64)    FILE="gostc_linux_amd64_v1.tar.gz" ;;
  386)      FILE="gostc_linux_386_sse2.tar.gz" ;;
  arm64)    FILE="gostc_linux_arm64_v8.0.tar.gz" ;;
  arm_5)    FILE="gostc_linux_arm_5.tar.gz" ;;
  arm_6)    FILE="gostc_linux_arm_6.tar.gz" ;;
  arm_7)    FILE="gostc_linux_arm_7.tar.gz" ;;
  mips64)   FILE="gostc_linux_mips64_hardfloat.tar.gz" ;;
  mips64le) FILE="gostc_linux_mips64le_hardfloat.tar.gz" ;;
  mips)     FILE="gostc_linux_mips_hardfloat.tar.gz" ;;
  mipsle)   FILE="gostc_linux_mipsle_hardfloat.tar.gz" ;;
  riscv64)  FILE="gostc_linux_riscv64_rva20u64.tar.gz" ;;
  s390x)    FILE="gostc_linux_s390x.tar.gz" ;;
  *) echo "Unsupported Linux ARCH: $GOARCH"; exit 1 ;;
esac

URL="${BASE_URL}/${FILE}"

echo "Detected Linux ARCH: $GOARCH"
echo "Downloading: $URL"

TMPDIR=$(mktemp -d)
cd "$TMPDIR"

curl -fL -o "$FILE" "$URL"

echo "Extracting $FILE ..."
tar -xzf "$FILE"

if [[ ! -f gostc ]]; then
  echo "Error: gostc binary not found in archive!"
  exit 1
fi

echo "Cleaning old gostc in /usr/local/bin ..."
sudo rm -f /usr/local/bin/gostc

echo "Installing gostc to /usr/local/bin/ ..."
sudo mv gostc /usr/local/bin/gostc
sudo chown root:root /usr/local/bin/gostc
sudo chmod 755 /usr/local/bin/gostc

cd /
rm -rf "$TMPDIR"

echo "Installation complete: /usr/local/bin/gostc"

# ========== 运行 gostc 并传递额外参数 ==========
/usr/local/bin/gostc stop || true
/usr/local/bin/gostc uninstall || true
/usr/local/bin/gostc install "$@"
/usr/local/bin/gostc start
