#!/bin/sh

# Dmenu for doing everything web related

ScriptName=${0##*/}
Version=1.0.0

#browser='firefox' # firefox
browser='dbrowsel' # firefox
search_engine='https://duckduckgo.com/?q='

# Set menu options:
SearchWeb='0. Search the web.'
MenuOpen='1. Open a bookmark.'
MenuSave='2. Create new bookmark.'
RunFirefox='3. Run Firejailed Firefox.'

ChosenMenu=$(printf '%s\n%s\n%s\n%s' \
    "$SearchWeb" "$MenuOpen" "$MenuSave" "$RunFirefox" \
    | dmenu -i -l 10 -p "$ScriptName:")
# Search web
if [ "$ChosenMenu" = "$SearchWeb" ]; then
	search=$(dmenu -l 1 -i -p "Search:")
	[ "$search" ] && $($browser "$search_engine$(echo $search | sed -r 's/ /+/g')") &
fi

# run firefox
if [ "$ChosenMenu" = "$RunFirefox" ]; then
	gtk-launch Firejail-Firefox
# Open bookmark:
elif [ "$ChosenMenu" = "$MenuOpen" ]; then
    buku --np -p -f 5 \
        | awk -F '\t' '{print $1, "-", $2, "#(" $3 ")"}' \
        | dmenu -i -l 30 \
        | awk '{print $1}' \
        | xargs --no-run-if-empty buku --np -o
# Create bookmark:
elif [ "$ChosenMenu" = "$MenuSave" ]; then
    # Get the name of the active window:
    ActiveWindow=$(ps -p "$(xdotool getwindowfocus getwindowpid)" -o comm=)

    # Set the name if the active window is a chromium based browser:
    printf '%s' "$ActiveWindow" | grep -Eq 'chromium|chrome|brave' \
        && ActiveWindow='chromium-based'
    ### Haven't actually checked whether Chromium's window name is chromium.

    # Get URL/title based on browser:
    if [ "$ActiveWindow" = 'qutebrowser' ]; then
        xdotool search --name qutebrowser key --clearmodifiers y P
        sleep 0.1
        URL=$(xclip -o)
        sleep 0.1
        xdotool search --name qutebrowser key --clearmodifiers y T
        sleep 0.1
        Title=$(xclip -o)
    elif [ "$ActiveWindow" = 'chromium-based' ]; then
        xdotool key --clearmodifiers ctrl+l
        sleep 0.1
        xdotool key --clearmodifiers ctrl+c
        sleep 0.1
        URL=$(xclip -o)
        sleep 0.1
        ### Maybe write it to a file in the background to speed it up.
        ### Doesn't work for some generated sites.
        Title=$(curl -s "$URL" \
            | awk -F '<title>|</title>' '{for(i=2;i<=NF;i+=2){print $i}}' \
            RS='' \
            | recode html)
    fi

    # Prompt user to edit the URL:
    EditedURL=$(printf "%s" "$URL" | dmenu -l 1 -p "Edit URL:")
    [ "$EditedURL" ] && URL=$EditedURL

    # Prompt user to edit the title:
    EditedTitle=$(printf '%s' "$Title" | dmenu -l 1 -p 'Edit title:')
    [ "$EditedTitle" ] && Title=$EditedTitle

    # Prompt the user to pick tags for the bookmark:
    BukuTags=$(buku --np -t)
    BukuTags=$(printf '%s' "$BukuTags" \
        | grep '[0-9]\..' \
        | awk '{print $2}')
        # awk is POSIX, but means you need to avoid spaces in tags.
        #| grep -oP '(?<=[0-9]\.\s).+(?=\s\([0-9])')
        # grep -oP is non-POSIX, but allows for spaces in tags.
    while true; do
        SelectedTag=$(printf '%s' "$BukuTags" \
            | dmenu -i -l 30 \
            -p "Add tags. SHIFT+ENTER to create a new tag. $Tags")
        [ -z "$SelectedTag" ] && break
        Tags="$Tags$SelectedTag,"
    done
    Tags=$(printf '%s' "${Tags%?}")

    # Prompt the user to add a comment/description:
    Comment=$(printf '' | dmenu -p 'Input comment:')

    # Prompt the user whether they want the title and description to not update:
    Immutability=$(printf 'No\nYes' | dmenu -i -p 'Enable immutability?')
    [ "$Immutability" = 'Yes' ] && Immutable='--immutable 1'
    [ "$Immutability" = 'No' ] && Immutable=''

    # Create a preview for the user to see their input:
    Lock=''
    [ "$Immutability" = 'Yes' ] && Lock='(L)'
    Confirmation=$(printf '%s\nIDN. %s %s\n   > %s\n   + %s\n   # %s' \
        'This is a preview. Press ENTER to confirm or ESCAPE to cancel.' \
        "$Title" "$Lock" "$URL" "$Comment" "$Tags" \
        | dmenu -i -l 5)

    # If the user pressed ENTER, create the bookmark:
    [ "$Confirmation" ] && \
        buku $(printf '%s %s %s --title %s -c %s %s' \
        '-a' "$URL" "$Tags" "$Title" "$Comment" "$Immutable")
elif [ -z "$ChosenMenu" ]; then
    exit 1
else
    printf 'ERROR: invalid selection.\n'
    exit 1
fi
