#!/bin/sh
# Adds a bookmark. 
# Either grabs xclip, or requests input of the string to bookmark. 
# If passed --ask (-a), then ignore the clipboard and prompt the user what to 
# bookmark. 
coast="$(xclip -o -selection primary)"
alias bmnotify-send="notify-send -i $HOME/.local/share/img/bm-icon.png"
commonargs="-i $HOME/.local/share/img/bm-icon.png"
if isflag '-a' "$@" ; then 
	coast="$(dmenu -p "Enter coast entry: " < /dev/null)"
fi
# Exit if entry is not present. 
[ -z "$coast" ] && bmnotify-send "Did not add coast." "Nothing selected to add!" && exit
# Request comment. 
comment="$(dmenu -p "Add comment ($coast): " < /dev/null)"
# Exit if the comment is blank. 
[ -z "$comment" ] && exit
# Find the file which holds coast entries, and process it. 
if [ "$ORGD_KT_PATH" = "" ]; then 
	notify-send "ORGD_KT_PATH empty." "Using $HOME/docs/wr/orgd/kt as fallback."
	file="$HOME/docs/wr/orgd/kt/coast"
else
	file="$ORGD_KT_PATH/coast"
fi
stripped="$(strip-comments "$file")"
# Add the entry, unless its already added. 
if echo "$stripped" | grep -q "^$coast[/ \t]*$"; then 
	bmnotify-send "No!" "$coast already added!"
else
	echo "$coast # $comment" >> "$file"
	bmnotify-send "Entry Added!" "$coast added and tagged as $comment." $commonargs
fi