# shellcheck shell=sh disable=SC3043,SC2164

# author:       Li Junhao           l@x-cmd.com    edwinjhlee.github.io
# maintainer:   Li Junhao
# license:      GPLv3

# open -a ApplicationName file
# open a.json --> Using

___x_cmd_open___main(){
    # for i in "$@"; do
    #     case "$i" in
    #         -f|-i|-n|-v)    continue ;;
    #         -*)             continue ;;
    #         # /|/bin)        printf "Abort because trying to remove key file." >&2 || return 1 ;;
    #     esac
    # done

    local p="${1:-$PWD}"

    [ ! -e "$p" ] || {
        [ -z "$___X_CMD_BOX_INNER_HOME" ]           ||  \
        [ "$HOME" = "$___X_CMD_BOX_INNER_HOME" ]    ||  \
        [ "${p#"${___X_CMD_BOX_INNER_HOME}"}" = "$p" ] || p="${HOME}${p#"$___X_CMD_BOX_INNER_HOME"}"
    }
    local wsl=
    read -r wsl </proc/version
    case "$wsl" in
        *WSL2*)  ___x_cmd_open_linux_wsl "$p"
            ;;
        *)      ___x_cmd_cmds xdg-open "$p"  # command not found on wsl
            ;;
    esac
}

___x_cmd_open_linux_wsl(){
    if [ -d "$1" ]; then
        (
            ___x_cmd_cmds cd "$1"    # explorer.exe has a problem, absolute paths only navigate to the Documents directory (this happens with both Unix and Windows paths)
            ___x_cmd_cmds explorer.exe .
        )
    elif [ -f "$1" ]; then
        local p="${1%/*}"
        if [ "$1" = "$p" ]; then
            (
                ___x_cmd_cmds explorer.exe "$1"
            )
        else
            local file="${1##*/}"
            (
                ___x_cmd_cmds cd "$p"
                ___x_cmd_cmds explorer.exe "$file"
            )
        fi
    else
        ___x_cmd browse "$1"
    fi
}



