#!/bin/sh

set -e

CONFIG_FILE="/home/user/.config/cuteNews/EventFeed.conf"
DATABASE_FILE="/home/user/.config/cuteNews/cuteNews.db"

EVENT_FEED_SERVICE="org.hildon.eventfeed"
EVENT_FEED_PATH="/org/hildon/eventfeed"
EVENT_FEED_INTERFACE="org.hildon.eventfeed"

REFRESH_ACTION="dbus-send --type=method_call --dest=org.marxoft.cutenews.subscriptions /org/marxoft/cutenews/subscriptions org.marxoft.cutenews.subscriptions.updateAll"

if [ -f "$CONFIG_FILE" ]
    then
        echo "Disabling cuteNews event feed."
        rm "$CONFIG_FILE"
fi

if [ -f "$DATABASE_FILE" ]
    then
        for SUBSCRIPTION_ID in $(echo "SELECT id FROM subscriptions;" | sqlite3 "$DATABASE_FILE")
            do
                echo "Removing cuteNews event feed items for subscription $SUBSCRIPTION_ID."
                dbus-send --type=method_call --dest=$EVENT_FEED_SERVICE $EVENT_FEED_PATH $EVENT_FEED_INTERFACE.removeItemsBySourceName string:"cutenews_$SUBSCRIPTION_ID"
            done
fi

echo "Removing cuteNews event feed refresh action."
dbus-send --type=method_call --dest=$EVENT_FEED_SERVICE $EVENT_FEED_PATH $EVENT_FEED_INTERFACE.removeRefreshAction string:"$REFRESH_ACTION"

exit 0
