#!/bin/sh

WMENU="wmenu"
DMENU="dmenu"
BROWSER="firefox-bin"
NOTIFY="notify-send"

CHOICES="Paulgo
URL
Incognito URL
YouTube
Codeberg
Tildegit
IPLeak
Safebooru
Wikipedia"

notify() {
    "$NOTIFY" "$1" "$2" 2>/dev/null
}

run_wmenu() {
    printf '%s\n' "$1" | $WMENU -p "$2"
}

run_dmenu() {
    printf '' | $DMENU -p "$1"
}

urlencode() {
    python3 -c "import sys, urllib.parse as ul; print(ul.quote_plus(sys.argv[1]))" "$1"
}

open_url() {
    url="$1"
    if pgrep -x "$BROWSER" >/dev/null; then
        "$BROWSER" --new-tab "$url" &
    else
        "$BROWSER" "$url" &
    fi
}

main() {
    choice=$(run_wmenu "$CHOICES" "Where to?")
    [ -z "$choice" ] && notify "Error" "No selection made" && exit 1

    case "$choice" in
        Codeberg)
            repo=$(run_dmenu "Username & repo (user/repo):")
            [ -z "$repo" ] && notify "Error" "No input" && exit 1
            open_url "https://codeberg.org/$repo"
            ;;
        Tildegit)
            repo=$(run_dmenu "Username & repo (user/repo):")
            [ -z "$repo" ] && notify "Error" "No input" && exit 1
            open_url "https://tildegit.org/$repo"
            ;;
        IPLeak)
            open_url "https://ipleak.net"
            ;;
        Paulgo)
            query=$(run_dmenu "Search Paulgo:")
            [ -z "$query" ] && notify "Error" "No input" && exit 1
            qenc=$(urlencode "$query")
            open_url "https://paulgo.io/search?q=$qenc"
            ;;
        Wikipedia)
            query=$(run_dmenu "Search Wikipedia:")
            [ -z "$query" ] && notify "Error" "No input" && exit 1
            qenc=$(urlencode "$query")
            open_url "https://en.wikipedia.org/wiki/Special:Search?search=$qenc"
            ;;
        URL)
            url=$(run_dmenu "Enter URL:")
            [ -z "$url" ] && notify "Error" "No URL entered" && exit 1
            case "$url" in
                http*) ;;
                *) url="https://$url" ;;
            esac
            open_url "$url"
            ;;
        "Incognito URL")
            url=$(run_dmenu "Enter incognito URL:")
            [ -z "$url" ] && notify "Error" "No URL entered" && exit 1
            case "$url" in
                http*) ;;
                *) url="https://$url" ;;
            esac
            "$BROWSER" --private-window "$url" &
            ;;
        YouTube)
            query=$(run_dmenu "Search YouTube:")
            [ -z "$query" ] && notify "Error" "No input" && exit 1
            qenc=$(urlencode "$query")
            open_url "https://youtube.com/results?search_query=$qenc"
            ;;
        Safebooru)
            query=$(run_dmenu "Search Safebooru:")
            [ -z "$query" ] && notify "Error" "No input" && exit 1
            qenc=$(urlencode "$query")
            open_url "https://safebooru.org/index.php?page=post&s=list&tags=$qenc"
            ;;
        *)
            notify "Error" "Invalid selection: $choice"
            exit 1
            ;;
    esac
}

for cmd in "$WMENU" "$DMENU" "$BROWSER" "$NOTIFY"; do
    if ! command -v "$cmd" >/dev/null 2>&1; then
        echo "Error: required command '$cmd' not found" >&2
        exit 1
    fi
done

main

