#!/usr/bin/env bash

BOOKMARKS="$HOME/.local/share/bookmarks/"

isxclient=$( readlink /dev/fd/2 | grep -q 'tty' && [[ -n $DISPLAY ]] ; echo $? )
if [[ ! -t 2  || $isxclient == "0" ]]; then
	DMENU="rofi -dmenu"
else
	DMENU="fzf"
fi

function formatFile(){
	keywords="$(grep -E '^keywords:' "$1" | sed 's/^keywords://')"
	echo "$1 : $keywords" | sed 's/  / /g'
}
export -f formatFile

if [ -d "$BOOKMARKS" ]; then
	cd "$BOOKMARKS"
	selection=$(fd --type f --exclude README.md --exec bash -c 'formatFile "{}"' ";" . | $DMENU | sed 's/ : .*//')
	if [ -f "$selection" ]; then
		head -n 1 "$selection"
	fi
else
	echo "$BOOKMARKS doesn't exist"
fi
