#!/bin/bash

#set -e
url=$(xsel -b)
tags=$(zenity --entry --text="enter tags for $url")

if [[ $? -ne 0 ]]; then
    exit 0
fi

FOO=$(buku --nostdin --np --nc -p -1)
OUT="$(buku --nostdin --np --nc -a "$url" "$tags" 2>&1)"
if [[ $OUT =~ "ERROR" ]] ; then
    notify-send "Bookmarking Failed" "$OUT" --app-name="buku" --icon="dialog-error"
    exit 1
fi

BAR=$FOO

while [[ $BAR == "$FOO" ]]; do
    sleep 1
    BAR=$(buku --nostdin --np --nc -p -1)
done;

notify-send "Success" "$BAR" --app-name="buku" --icon="checkmark"

exit 0