#!/bin/ksh

# dexec_browser
# A multi browser launcher
#
# 1. choose input source
#       (filtered-, full history, clipboad, manual entry)
# 2. choose how to handle the input
#       (can suggest a matching default)
# 3. choose which browser to use
#       (can suggest a matching default)

. ${HOME}/.bin/_config

HISTFILE="${HOME}/.browser_history"
touch "${HISTFILE}"

# show history file
# EXPECTS:  $HISTFILE $DMENU_CMD
# PROVIDES: $INPUT
read_input() {
    local S=$( { echo "paste_from_clipboard"; tail -r ${HISTFILE}; } \
                 | awk '!seen[$0]++' \
                 |  ${DMENU_CMD} -p "Bookmarks" -l 20)

    case "${S}" in
        paste_from_clipboard) S=$(xclip -o | head -n 1); ;;
    esac

    [ -z "${S}" ] && exit 0

    INPUT="${S}"
}

# Decide how to open the thing
# EXPECTS:  $INPUT $DMENU_CMD
# PROVIDES: $URI
choose_wrapper() {

local SE="OPEN URL
Amazon
Amazon Order ID
CPAN
Crates.io
DuckDuckGo Web Search
DuckDuckGo Image Search
Startpage Web Search
GLIB Documentation
Github
LibSOUP Documentation
Marc Info MessageID
Marc Info OpenBSD Misc List
Marc Info OpenBSD Ports CVS
Marc Info OpenBSD Ports List
Marc Info OpenBSD Tech List
PDF Viewer
SearX Web Search
SearX Image Search
Torrent Leech
Wikipedia DE
Wikipedia EN
InternetMovieDataBase (IMDB)
OpenStreetMap (OSM)
Grep.app
Google Maps
Youtube
Zalando"

    local C="$(printf '%s' "${INPUT}" \
                    | head -n 1)"
    case "${C}" in
        *::*) DEFAULT="CPAN (default)"; ;;
        http*) DEFAULT="OPEN (default)"; ;;
        192.168.*) DEFAULT="OPEN (default)"; C="http://${C}"; ;;
        10.20.*) DEFAULT="OPEN (default)"; C="http://${C}"; ;;
        ???-???????-???????) DEFAULT="Amazon Order ID (default)"; ;;
        gopher*) DEFAULT="OPEN (default)"; ;;
        g_*) DEFAULT="GLIB Documentation (default)"; ;;
        [Ss]oup_*) DEFAULT="LibSOUP Documentation (default)"; ;;
        www.*) DEFAULT="OPEN (default)"; C="http://${C}"; ;;
        \<*@*\>) DEFAULT="Marc Info Message ID (default)"; ;;
        *.com|*.de|*.net|*.org) DEFAULT="OPEN (default)"; C="http://${C}"; ;;
        *.at|*.ch|*.social) DEFAULT="OPEN (default)"; C="http://${C}" ;;
        *.io|*.sh|*.pw|*.party) DEFAULT="OPEN (default)"; C="http://${C}"; ;;
        *.coffee|*.me|*.cloud) DEFAULT="OPEN (default)"; C="http://${C}"; ;;
        *.[a-zA-Z]*/*) DEFAULT="OPEN (default)"; C="http://${C}"; ;;
        *) DEFAULT="DuckDuckGo Web Search (default)"; ;;
    esac

    local S="$(printf "%s\n%s" "${DEFAULT}" "${SE}" \
                    | ${DMENU_CMD} -p "Search Where?" -l 20)"
    C=$(echo "$C" | sed 's/ /%20/g')
    case "${S}" in
        OPEN*)      URI="${C}"; ;;
        Amazon)     URI="https://www.amazon.de/s?k=${C}&rh=p_n_enc-merchantbin%3AA3JWKAKR8XB7XF"; ;;
        Amazon*O*)  URI="https://www.amazon.de/gp/your-account/order-details/ref=ppx_yo_dt_b_order_details_o00?orderID=${C}"; ;;
        CPAN*)      URI="https://metacpan.org/search?q=${C}"; ;;
        Crate*)     URI="https://crates.io/search?q=${C}"; ;;
        Grep*)      URI="https://grep.app/search?q=${C}"; ;;
        GLIB*)      URI="https://docs.gtk.org/glib/flags.UriFlags.html?q=${C}"; ;;
        LibSOUP*)   URI="https://libsoup.org/libsoup-3.0/index.html?q=${C}"; ;;
        Github)     URI="https://github.com/search?q=${C}"; ;;
        D*Web*)     URI="https://html.duckduckgo.com/html?q=${C}"; ;;
        Startpage*) URI="https://eu.startpage.com/do/search?prfe=00d4bb7f075582be9304aaad813ac6b4dc4bd733a098955c35c0586a99eaddf123ac81f1301f1e4f06fd014082141134ec5e0c5441250cf4c05557bed28fcfbced8de0007d01a0254332962533&query=${C}"; ;;
        D*Ima*)     URI="https://duckduckgo.com/?q=${C}&iax=images&ia=images"; ;;
        S*Web*)     URI="https://searx.bar/search?q=${C}&category_general=on"; ;;
        S*Ima*)     URI="https://searx.bar/search?q=${C}&category_images=on"; ;;
        Tor*Lee*)   URI="https://www.torrentleech.org/torrents/browse/index/query/${C}"; ;;
        W*DE*)      URI="https://de.m.wikipedia.org/wiki/Spezial:Suche/${C}"; ;;
        W*EN*)      URI="https://en.m.wikipedia.org/wiki/Spezial:Search/${C}"; ;;
        *IMDB*)     URI="https://www.imdb.com/find?q=${C}"; ;;
        O*S*M*)     URI="https://www.openstreetmap.org/search?query=${C}"; ;;
        G*Maps)     URI="https://www.google.com/maps/place/${C}"; ;;
        You*be)     URI="https://m.youtube.com/results?sp=mAEA&search_query=${C}" ;;
        M*I*ID*)    URI="https://marc.info/?i=$(printf '%s' "${C}" | tr -d '<>')"; ;;
        M*I*P*List) URI="https://marc.info/?l=openbsd-ports&s=${C}&q=b"; ;;
        M*I*T*List) URI="https://marc.info/?l=openbsd-tech&s=${C}&q=b"; ;;
        M*I*M*List) URI="https://marc.info/?l=openbsd-misc&s=${C}&q=b"; ;;
        M*I*P*CVS)  URI="https://marc.info/?l=openbsd-ports-cvs&s=${C}&q=b"; ;;
        Zalando)    URI="https://www.zalando.de/herren/?q=${C}"; ;;
        *) exit 0 ;;
    esac
}

# Which browser shall we use?
# EXPECTS:  $URI $DMENU_CMD
# PROVIDES: $BROWSER
choose_browser() {

    case "${URI}" in
        gopher://*)          DEFAULT="Lagrange (default)"; ;;
        gemini://*)          DEFAULT="Lagrange (default)"; ;;
        *console.hetzner.cloud*) DEFAULT="Firefox (default)"; ;;
        *youtube.com/watch*) DEFAULT="Mpv (default)"; ;;
        *media.ccc.de/v/*)   DEFAULT="Mpv (default)"; ;;
        *.pdf|*.cb|*.ps)     DEFAULT="Zathura (default)"; ;;
        *.mp4|*.m4v|*.mkv)   DEFAULT="Mpv (default)"; ;;
        *) DEFAULT="$BROWSER (default)"; ;;
    esac

    local S="${DEFAULT}\nLuakit\nVimb\nFirefox\nChrome\nQutebrowser\nWyeb\nNetsurf\nTor-Browser\nDillo\nSurf\nMpv"
    case $(echo "${S}" | ${DMENU_CMD} -p "Browser") in
        [Nn]etsurf*)  BROWSER=netsurf-gtk3 ;;
        [Vv]imb*)     BROWSER="vimb --no-maximize" ;;
        [Dd]illo*)    BROWSER=dillo; ;;
        [Qq]uteb*r*)  BROWSER="qutebrowser -R" ;;
        [Ss]urf*)     BROWSER=surf ;;
        [Oo]tter*r*)  BROWSER=otter-browser ;;
        [Ll]uakit*)   BROWSER="luakit -Un" ;;
        [Cc]hrome*)   BROWSER=chrome ;;
        [Ff]irefox*)  BROWSER=firefox ;;
        [Tt]or-B*r*)  BROWSER=tor-browser ;;
        [Zz]athura*)  BROWSER=zathura ;;
        [Ll]agrange*) BROWSER=lagrange ;;
        [Ww]yeb*)     BROWSER=wyeb ;;
        [Mm]pv*)      BROWSER="mpv" ;;
        *) exit 0;;
    esac
}

save_history() {
    printf '%s\n' "${INPUT}" | sed 's/^ *//g;s/ *$//g' >> "${HISTFILE}"
}

# main program starts here.
read_input
choose_wrapper
# choose_browser
save_history
$BROWSER "$(printf '%s' "${URI}" | sed 's/ /%20/g')" &
