#!/usr/bin/env bash
# [[file:../../Desktop.org::*Buku bookmarks][Buku bookmarks:1]]
if [ $(hostname) = 'pdsk' ]; then
    BUKU="/home/pavel/.local/bin/buku"
else
    BUKU="/home/pavel/Programs/miniconda3/bin/buku"
fi

# COMMAND="$BUKU -o %"
# COMMAND="qutebrowser $(buku -f 10 -p %)"
COMMAND="firefox %"
if [[ $1 == '-e' ]]; then
    COMMAND="$BUKU -w %"
fi
$BUKU -f 4 -p | awk -F'\t' -v OFS='\t' '{
    split($4, tags, ",")
    joined = sep = ""
    for (i = 1; i in tags; i++) {
        joined = joined sep "[" tags[i] "]"
        sep = " "
    }
    url = substr($2, 1, 40)
    if (length($2) > 40) {
        url = url "..."
    }
    if ($1 != "waiting for input") {
        printf "%-5s %-60s %-45s %s\n", $1, $3, url, joined
    }
}' | sort -k 2 | rofi -dmenu -matching normal -sort -sorting-method fzf -width 80 -l 20 | cut -d ' ' -f 1 | {
    read index;
    if [[ -z "$index" ]]; then
        exit 0
    fi
    url=$($BUKU -f 10 -p $index)
    echo ${url#"waiting for input"} | cut -d ' ' -f 1 | xargs -I % $COMMAND
}
# Buku bookmarks:1 ends here
