#!/bin/sh

engine_names="duckduckgo
searx0
searx1
searx2
searx3
searx4
startpage
qwant
stackoverflow
github
gitlab
mdn
devhints
gentoo_wiki
arch_wiki
alpine_wiki
wikipedia
open_street_map
google_images
urban_dictionary
thesaurus
vocabulary
wiktionary_en
twitter
youtube
odysee
archive.org
alternativeto"

menu1="{{ launcher }} {{ dmenu_flag }} -p Search"
query=$(printf "" | ${menu1} | urlfilter)

if [ -z "${query}" ]; then
    echo "Query is blank."
    exit 1
fi

if echo "${query}" | grep -qE '^https?://.*$'; then
    {{ default_browser }} "${query}"
else
    menu2="{{ launcher }} {{ dmenu_flag }} -p Engine"

    engine=$(echo "${engine_names}" |  ${menu2})
    if [ -n "${engine}" ]; then
        case ${engine} in
            duckduckgo) engine_url='https://html.duckduckgo.com/html?q=' ;;
            searx0) engine_url='https://search.disroot.org/?category_general=on&q=' ;;
            searx1) engine_url='https://searx.info/?category_general=on&q=' ;;
            searx2) engine_url='https://searx.fmac.xyz/?category_general=on&q=' ;;
            searx3) engine_url='https://searx.be/?category_general=on&q=' ;;
            searx4) engine_url='https://searx.monicz.pl/?category_general=on&q=' ;;
            startpage) engine_url='https://startpage.com/do/search?query=' ;;
            qwant) engine_url='https://www.qwant.com/?q=' ;;
            stackoverflow) engine_url='http://stackoverflow.com/search?q=' ;;
            github) engine_url='https://github.com/search?utf8=\u2713&q=' ;;
            gitlab) engine_url='https://gitlab.com/explore?utf8=\u2713&sort=latest_activity_desc&name=' ;;
            mdn) engine_url='https://developer.mozilla.org/en-US/search?q=' ;;
            devhints) engine_url='https://devhints.io/' ;;
            gentoo_wiki) engine_url='https://wiki.gentoo.org/index.php?title=Special%3ASearch&profile=default&fulltext=Search&search=' ;;
            arch_wiki) engine_url='https://wiki.archlinux.org/index.php?search=' ;;
            alpine_wiki) engine_url='https://wiki.alpinelinux.org/w/index.php?title=Special%3ASearch&fulltext=Search&search=' ;;
            wikipedia) engine_url='https://en.wikipedia.org/wiki/' ;;
            open_street_map) engine_url='https://www.openstreetmap.org/search?query=' ;;
            google_images) engine_url='https://www.google.com/search?tbm=isch&q=' ;;
            urban_dictionary) engine_url='https://www.urbandictionary.com/define.php?term=' ;;
            thesaurus) engine_url='https://www.thesaurus.com/browse/' ;;
            vocabulary) engine_url='https://www.vocabulary.com/dictionary/' ;;
            wiktionary_en) engine_url='https://en.wiktionary.org/wiki/' ;;
            twitter) engine_url='https://twitter.com/search?q=' ;;
            youtube) engine_url='https://www.youtube.com/results?search_query=' ;;
            odysee) engine_url='https://odysee.com/$/search?q=' ;;
            archive.org) engine_url='https://archive.org/search.php?query=' ;;
            alternativeto) engine_url='https://alternativeto.net/browse/search?q=' ;;
            *) echo "Invalid engine name."; exit 1 ;;
        esac
    else
        echo "Search engine name is blank."
        exit 1
    fi

    {{ default_browser }} "${engine_url}${query}"
fi
