#!/bin/bash

case $1 in
  --bemenu)
    menu1="bemenu -i -l 10 -p Search"
    menu2="bemenu -i -p Go"
    ;;
  --wofi)
    menu1="wofi -d -p Search"
    menu2="wofi -d -p Go"
    ;;
  --dmenu)
    menu1="dmenu -i -l 10 -p Search"
    menu2="dmenu -i -p Go"
    ;;
  --rofi)
    menu1="rofi -dmenu -matching fuzzy -location 0 -no-custom -p Search"
    menu2="rofi -dmenu -matching fuzzy -location 0 -p Go"
    ;;
  *)
    exit
    ;;
esac

browser='qutebrowser'

declare -A urls

urls=(
  ["duckduckgo"]="https://www.duckduckgo.com/?q="
  ["searx0"]="https://search.disroot.org/?category_general=on&q="
  ["searx1"]="https://searx.info/?category_general=on&q="
  ["searx2"]="https://searx.fmac.xyz/?category_general=on&q="
  ["searx3"]="https://searx.be/?category_general=on&q="
  ["searx4"]="https://searx.monicz.pl/?category_general=on&q="
  ["startpage"]="https://startpage.com/do/search?query="
  ["qwant"]="https://www.qwant.com/?q="
  ["stackoverflow"]="http://stackoverflow.com/search?q="
  ["github"]="https://github.com/search?utf8=\u2713&q="
  ["gitlab"]="https://gitlab.com/explore?utf8=\u2713&sort=latest_activity_desc&name="
  ["mdn"]="https://developer.mozilla.org/en-US/search?q="
  ["devhints"]="https://devhints.io/"
  ["gentoo_wiki"]="https://wiki.gentoo.org/index.php?title=Special%3ASearch&profile=default&fulltext=Search&search="
  ["arch_wiki"]="https://wiki.archlinux.org/index.php?search="
  ["wikipedia"]="https://en.wikipedia.org/wiki/"
  ["open_street_map"]="https://www.openstreetmap.org/search?query="
  ["google_images"]="https://www.google.com/search?tbm=isch&q="
  ["urban_dict"]="https://www.urbandictionary.com/define.php?term="
  ["thesaurus"]="https://www.thesaurus.com/browse/"
  ["vocabulary"]="https://www.vocabulary.com/dictionary/"
  ["twitter"]="https://twitter.com/search?q="
  ["youtube"]="https://www.youtube.com/results?search_query="
  ["odysee.com"]="https://odysee.com/$/search?q="
  ["archive"]="https://archive.org/search.php?query="
  ["alternativeto"]="https://alternativeto.net/browse/search?q="
)

gen_list() {
  for i in "${!urls[@]}"
  do
    echo "$i"
  done
}

engine=$( (gen_list) |  ${menu1} )
if [[ -n "$engine" ]]; then
  query=$( (echo) | ${menu2} )
  url=${urls[$engine]}$query
  $browser "$url"
else
  exit
fi
