#!/bin/bash

#
#   Display an icon on the notification area if there are updates for Fedora
#   needs: Gnome desktop, notification-area applet, yum, zenity
#   Copyright (C) 2005-2006 Mario Pascucci <ilpettegolo (a) yahoo it>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#   or
#   visit the URL http://www.fsf.org/
#



# Changelog
# 13 dicembre 2005	First version


# starting directory
# the config file must be in the same directory of the script
confdir=`dirname $0`

cd $confdir

# config file pathname
conffile=$confdir/fedupdatealert.conf

# sources config file
. $conffile


# initial wait for complete desktop loading
sleep 20


for ((;;)); do

  # start check
  # delete previous icon
  kill %- > /dev/null 2>&1
  zenity --notification --window-icon=$WORKico --notification-text "Checking..." &
  # update check
  # WARNING: this can be a security hole
  nice sudo yum check-update > /dev/null 2>&1
  res=$?
  kill %- > /dev/null 2>&1
  if [ $res -eq 100 ]; then
    # updates available
    # display new icon
    zenity --notification --window-icon=$UPDico --notification-text "Updates available!" &
    res=$?
  elif [ $res -eq 0 ]; then
    # no updates
    zenity --notification --window-icon=$OKico --notification-text "No updates available" &
    res=$?
  else
    # yum returns error
    zenity --notification --window-icon=$WARNico --notification-text "Problems...?" &
    res=$?
  fi

  # very rough session ending detection...
  if [ $res -eq 255 ]; then
    # if zenity return this value
    # it means that X for this user is dead
    # user has terminated session
    exit 0
  fi
  
  # wait to the next check
  sleep $INTERVAL

done

# remove zenity applet
kill %- > /dev/null 2>&1


