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

export DEBIAN_FRONTEND=noninteractive

log() {
  echo
  echo "========== $* =========="
}

need_root() {
  if [[ "${EUID}" -ne 0 ]]; then
    echo "请用 sudo 运行：sudo bash $0"
    exit 1
  fi
}

install_pkgs() {
  local pkgs=("$@")
  if [[ ${#pkgs[@]} -gt 0 ]]; then
    apt-get install -y "${pkgs[@]}"
  fi
}

need_root

log "更新软件源"
apt-get update

log "安装基础工具和 SSH"
install_pkgs \
  openssh-server \
  nano \
  rsync \
  flex \
  bison \
  texinfo \
  gawk \
  net-tools \
  zsh \
  libncurses-dev \
  libtinfo-dev \
  ncurses-term \
  zstd \
  unrar

log "安装 C/C++ 开发工具链"
install_pkgs \
  cmake \
  build-essential \
  git \
  pkg-config \
  ninja-build \
  gdb \
  valgrind

log "安装 Linux 调试工具"
install_pkgs linux-tools-common linux-tools-generic || true
apt-get install -y "linux-tools-$(uname -r)" || true

log "安装 Qt 基础组件"
install_pkgs \
  qtbase5-dev \
  qtchooser \
  qt5-qmake \
  qtbase5-dev-tools \
  qttools5-dev \
  qttools5-dev-tools \
  qtcreator

log "安装所有可用的 libqt5 *-dev 包"
QT5_DEV_PKGS=$(apt-cache search '^libqt5.*-dev$' | awk '{print $1}' | sort -u || true)
if [[ -n "${QT5_DEV_PKGS}" ]]; then
  # shellcheck disable=SC2086
  apt-get install -y ${QT5_DEV_PKGS}
fi

log "安装 Python 开发环境"
install_pkgs \
  python3 \
  python3-dev \
  python3-pip \
  python3-venv \
  python3-setuptools \
  python3-wheel \
  python3-distutils

log "安装桌面相关依赖"
install_pkgs gsettings-desktop-schemas

log "安装 MinGW 交叉编译工具链"
install_pkgs \
  mingw-w64 \
  gcc-mingw-w64-x86-64 \
  g++-mingw-w64-x86-64 \
  binutils-mingw-w64-x86-64

log "安装 XRDP"
install_pkgs xrdp
adduser xrdp ssl-cert || true
systemctl restart xrdp || true
systemctl enable xrdp || true

log "配置 SSH X11 转发"
install_pkgs \
  xauth \
  x11-apps

SSHD_CFG="/etc/ssh/sshd_config"
cp -a "$SSHD_CFG" "${SSHD_CFG}.bak.$(date +%Y%m%d_%H%M%S)"

if grep -Eq '^[#[:space:]]*X11Forwarding[[:space:]]+' "$SSHD_CFG"; then
  sed -ri 's|^[#[:space:]]*X11Forwarding[[:space:]]+.*|X11Forwarding yes|' "$SSHD_CFG"
else
  echo 'X11Forwarding yes' >> "$SSHD_CFG"
fi

if grep -Eq '^[#[:space:]]*X11DisplayOffset[[:space:]]+' "$SSHD_CFG"; then
  sed -ri 's|^[#[:space:]]*X11DisplayOffset[[:space:]]+.*|X11DisplayOffset 10|' "$SSHD_CFG"
else
  echo 'X11DisplayOffset 10' >> "$SSHD_CFG"
fi

if grep -Eq '^[#[:space:]]*X11UseLocalhost[[:space:]]+' "$SSHD_CFG"; then
  sed -ri 's|^[#[:space:]]*X11UseLocalhost[[:space:]]+.*|X11UseLocalhost yes|' "$SSHD_CFG"
else
  echo 'X11UseLocalhost yes' >> "$SSHD_CFG"
fi

if grep -Eq '^[#[:space:]]*UsePAM[[:space:]]+' "$SSHD_CFG"; then
  sed -ri 's|^[#[:space:]]*UsePAM[[:space:]]+.*|UsePAM yes|' "$SSHD_CFG"
fi

systemctl enable ssh || true
systemctl restart ssh 2>/dev/null || systemctl restart sshd 2>/dev/null || true

log "安装完成"
echo "验证 X11 转发："
echo "  1. 本机先启动 X Server"
echo "  2. ssh -X 用户名@目标IP"
echo "  3. echo \$DISPLAY"
echo "  4. xclock"
echo
echo "备注："
echo "  - SSH 登录用户和实际启动 GUI 程序的用户尽量保持一致"
echo "  - CLion 可设置 DISPLAY=localhost:10.0 和 QT_QPA_PLATFORM=xcb"
echo "  - 你还可以手动执行 ibus-setup、vncpasswd，并编辑 ~/.vnc/xstartup"