#!/bin/sh


# This script can be used to automatically add 
# subscriptions to Liferea. Just supply a valid
# and correctly escaped feed URL as parameter.

if [ $# -ne 1 ]; then
	echo "Wrong parameter count!"
	echo ""
	echo "Syntax: $0 <feed URL>"
	echo ""
	exit 1
fi

URL=$1

if ! which dbus-send >/dev/null 2>&1; then
	echo "Unable to locate the 'dbus-send' tool."
	echo "You need DBUS installed!"
	exit 1
fi

result=0
i=1
while [ $i -le 15 -a $result -eq 0 ]; 
do
	if dbus-send --session --print-reply=literal \
	             --dest=org.gnome.feed.Reader \
	             --type=method_call /org/gnome/feed/Reader \
	             org.gnome.feed.Reader.Ping 2>/dev/null |\
	   grep "boolean true" >/dev/null; 
	then
		result=1
	else
		result=0
	fi
	
	if [ $result -eq 0 ]; then
		if [ $i -eq 1 ]; then
			echo "Liferea is not running! Trying to start it..."
			`which liferea` &
			if [ $? -ne 0 ]; then
				echo "ERROR: Starting Liferea failed!"
				exit 1
			fi
		else
			echo "Still not running..."
		fi
		sleep 2
	fi
	i=`expr $i + 1` 
done

if [ $result -eq 0 ]; then
	echo "ERROR: Liferea did not come up after 30s. Cannot add subscription..."
	exit 1
fi

dbus-send --session --dest=org.gnome.feed.Reader /org/gnome/feed/Reader org.gnome.feed.Reader.Subscribe string:$URL
