#!/bin/sh
# Gives a dmenu prompt to searx
# Without input, will open mainpage.
# URLs will be directly handed to the browser.
mainpage="https://bocken.org/searx"
searchpage="https://bocken.org/searx/search?q=%s"
browser=${BROWSER:-firefox}
if [ -z "$(xclip -o -selection clipboard)" ]; then
	choice=$(printf "šŸ”\n" | dmenu -i -p "Searx:") || exit 1
else
	choice=$(printf "šŸ”\nšŸ“‹\n" | dmenu -i -p "Searx:") || exit 1
fi

if [ "$choice" = "šŸ”"  ]; then
    $browser "$mainpage"
elif [ "$choice" = "šŸ“‹" ]; then
	$browser "$(xclip -o -selection clipboard)"
else
    if echo "$choice" | grep "^(http:\/\/|https:\/\/)?[a-zA-Z0-9]+\.[a-zA-Z]+(/)?.*$"; then
        $browser "$choice"
    else
	string="$(echo $choice | sed 's/\ /+/g')"
	$browser "$(echo "${searchpage}" | sed "s/%s/$string/")"
    fi
fi