# shellcheck shell=dash

___x_cmd_webtop_open(){
    [ $# -ne 0 ] || set -- --auto
    case "$1" in
        gh|github)  ___x_cmd open "https://github.com/linuxserver/docker-webtop"        ;;
        default)    ___x_cmd open "https://docs.linuxserver.io/images/docker-webtop/"   ;;
        --auto)     ___x_cmd_webtop_container_ps                                        ;;
        -h|--help)  ___x_cmd help -m webtop open                                        ;;
        *)          ___x_cmd_webtop_open___app "$@"                                     ;;
    esac
}

___x_cmd_webtop_open___app(){
    if ___x_cmd os is darwin; then
        ___x_cmd_webtop_open___app_mac "$@"
        return
    fi

    ___x_cmd open "http://localhost:${1#x-webtop-*}"
}

___x_cmd_webtop_open___app_mac(){
    local name="$1"

    case "$name" in
        http*://*)  ___x_cmd open "$name" ;;
        *)
            local title; title="$(___x_cmd_webtop_container_find_title_by_kw "$name")" || return $?
            command open -a "$title" 2>/dev/null || {
                ___x_cmd open "http://localhost:${title%%-*}"
            }
    esac

}
