#!/bin/sh -e
set -e

#DEBHELPER#

# Get OSSO environment variables (launchers, dbus bus, etc)
if [ -f /etc/osso-af-init/af-defines.sh ]; then
  source /etc/osso-af-init/af-defines.sh
fi

if [ "$2" = "0.0.1" ]; then
    update-rc.d -f x-skype-autolauncher remove
fi

if [ "$1" = "install" ] || [ "$2" = "0.0.1" ]; then

dbus-send --type=method_call --dest=org.freedesktop.Notifications \
    /org/freedesktop/Notifications \
    org.freedesktop.Notifications.SystemNoteInfoprint \
    string:"Installing alarm to restart
Automatic Skype Launcher
at early morning (03:27)."

    # Alarm at 03:27 local time to run our cron daily script
    EARLY_MORNING=`/bin/date -d 03:27:00 +%s`
    DBUS_ALARM_COOKIE_REPLY=`dbus-send --print-reply \
        --dest=com.nokia.alarmd --type=method_call \
        /com/nokia/alarmd com.nokia.alarmd.add_event \
        objpath:"/AlarmdEventRecurring" \
        uint32:7 \
        string:"action" \
        objpath:"/AlarmdActionExec" \
        uint32:6 \
        string:"flags" \
        int32:1025 \
        string:"title" \
        string:"" \
        string:"message" \
        string:"" \
        string:"sound" \
        string:"" \
        string:"icon" \
        string:"" \
        string:"path" \
        string:"/etc/cron.daily/skype-autolauncher" \
        string:"time" \
        int64:$EARLY_MORNING \
        string:"snooze_interval" \
        uint32:0 \
        string:"snooze" \
        uint32:0 \
        string:"recurr_interval" \
        uint32:1440 \
        string:"recurr_count" \
        int32:-1 \
        string:"real_time" \
        uint64:$EARLY_MORNING`

    ALARM_COOKIE=`/bin/echo $DBUS_ALARM_COOKIE_REPLY | /bin/sed 's/.* //'`

    /usr/bin/gconftool-2 -s /system/skype-autolauncher/alarm_event \
        --type int "$ALARM_COOKIE"

fi


exit 0
