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

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=scripts/github/_gh_env.sh
source "$SCRIPT_DIR/_gh_env.sh"

usage() {
  cat <<'EOF'
Usage:
  bash scripts/github/cxgh "使用 gh 更新 issue 并同步 project"
  bash scripts/github/cxgh --help

Environment:
  GH_TOKEN / GITHUB_TOKEN   Required
  CXGH_WORKSPACE_ROOT       Optional workspace root for .gh / .xdg-* directories
EOF
}

if [[ "${1:-}" == "-h" || "${1:-}" == "--help" || "$#" -eq 0 ]]; then
  usage
  exit 0
fi

if ! command -v codex >/dev/null 2>&1; then
  echo "[错误] 未找到 codex 命令" >&2
  exit 1
fi

gh_env_prepare
gh_env_require_token

exec codex \
  -a never \
  -s workspace-write \
  -c 'sandbox_workspace_write.network_access=true' \
  "$@"
