#!/bin/bash

# 帮助信息函数
show_help() {
    cat <<EOF
RunInTerminal - 在独立终端中运行指定命令

使用方式:
  $0 [选项] <命令>

选项:
  -p, --prompt <提示信息>   在命令执行前显示提示文字
  -t, --term-opts <选项>    传递给终端的额外参数
  -w, --wait <策略>         设置等待策略：
                            sleep=N 执行后等待N秒
                            (默认等待用户回车)
  -E, --no-enter-wait       执行后不等待直接关闭终端
  -h, --help                显示本帮助信息

示例:
  # 显示系统信息并等待
  $0 --prompt "系统信息" "neofetch"

  # 执行命令后等待5秒
  $0 --wait sleep=5 "make build"

  # 使用特定终端参数
  $0 --term-opts "--geometry=120x40" "htop"

  # 直接运行不等待
  $0 -E "npm start"
EOF
}

# 查找可用终端程序
find_terminal() {
    local -a preferred=()
    case "${XDG_CURRENT_DESKTOP,,}" in
        *gnome*)         preferred=(ptyxis kgx gnome-terminal) ;;
        *kde*|*plasma*)  preferred=(konsole) ;;
        *xfce*)          preferred=(xfce4-terminal) ;;
        *deepin*|*gxde*) preferred=(deepin-terminal) ;;
    esac

    local terminals=(
        "${preferred[@]}"
        ghostty xfce4-terminal konsole kgx gnome-terminal mate-terminal
        ptyxis lxterminal deepin-terminal terminator qterminal cosmic-term
        alacritty tilix termite xterm kitty terminology sakura
    )

    for term in "${terminals[@]}"; do
        if command -v "$term" >/dev/null; then
            echo "$term"
            return 0
        fi
    done

    echo "ERROR: 未找到可用终端程序" >&2
    return 1
}

# 主函数
main() {
    # 处理帮助参数优先
    for arg in "$@"; do
        if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then
            show_help
            exit 0
        fi
    done

    local term_opts prompt wait_opt cmd term tmpfile quoted_tmpfile quoted_prompt
    term=$(find_terminal) || return 1

    OPTS=$(getopt -o hp:t:w:E --long help,prompt:,term-opts:,wait:,no-enter-wait -n "$0" -- "$@") || exit 1
    eval set -- "$OPTS"

    while true; do
        case "$1" in
            -h|--help)    show_help; exit 0 ;;
            -p|--prompt)  prompt="$2"; shift 2 ;;
            -t|--term-opts) term_opts="$2"; shift 2 ;;
            -w|--wait)    wait_opt="$2"; shift 2 ;;
            -E|--no-enter-wait) wait_opt="--no-enter-wait"; shift ;;
            --) shift; break ;;
            *) echo "参数错误" >&2; exit 1 ;;
        esac
    done
    cmd="$*"
    local -a term_args=()
    if [ -n "$term_opts" ]; then
        read -r -a term_args <<< "$term_opts"
    fi

    # 创建临时脚本
    mkdir -p "$HOME/.cache" || exit 1
    tmpfile=$(mktemp -p "$HOME/.cache" -t runinterminal.XXXXXX) || return 1
    printf -v quoted_tmpfile '%q' "$tmpfile"

    # 构建脚本内容
    {
        echo "#!/bin/bash"
        # The terminal may detach before this process exits. The child must
        # remove its own script, not the parent that starts the terminal.
        printf 'trap %q EXIT\n' "rm -f -- $quoted_tmpfile"
        if [ -n "$prompt" ]; then
            printf -v quoted_prompt '%q' "$prompt"
            printf "printf '\\n\\033[1;32m%%s\\033[0m\\n' %s\n" "$quoted_prompt"
        fi
        printf '%s\n' "$cmd"
        echo '_catos_run_status=$?'
        
        # 等待逻辑处理
        case "$wait_opt" in
            --no-enter-wait) ;;
            sleep=*) echo "sleep ${wait_opt#*=}" ;;
            *) [ "$term" != deepin-terminal ] && echo 'read -p "Press Enter to close terminal..."' ;;
        esac
        
        echo "exit \"\$_catos_run_status\""
    } > "$tmpfile"

    chmod +x "$tmpfile"

    # 执行终端程序
    case "$(basename "$term")" in
        gnome-terminal) "$term" "${term_args[@]}" --wait -- bash "$tmpfile" ;;
        xfce4-terminal) "$term" --disable-server "${term_args[@]}" -x bash "$tmpfile" ;;
        ptyxis|kgx)     "$term" "${term_args[@]}" -- bash "$tmpfile" ;;
        kitty)          "$term" "${term_args[@]}" bash "$tmpfile" ;;
        *)              "$term" "${term_args[@]}" -e bash "$tmpfile" ;;
    esac
    local result=$?
    if (( result != 0 )); then
        rm -f -- "$tmpfile"
    fi
    return "$result"
}

main "$@"
