#!/bin/sh
#
# TazPKG Notify - Notification icon for Tazpkg packages. Recharging pkgs
# list can be done automatically at boot, so notifies users if some
# updates are available. Also notifies users if the packages list is too
# old and out-of-date or if no packages list found. This script should
# be run by the WM autostart script or ~/.xsession and needs a systray to
# sit in like in LXpanel or Tint2.
#
# Copyright (C) 2011 SliTaz GNU/Linux - GNU gpl v2
#
# Authors : Christophe Lincoln <pankso@slitaz.org>
#

. /etc/slitaz/tazpkg.conf
LOCALSTATE="/var/lib/tazpkg"

# Include gettext helper script.
. /usr/bin/gettext.sh

# Export package name for gettext.
TEXTDOMAIN='tazpkg-notify'
export TEXTDOMAIN

up=`cat $LOCALSTATE/packages.up | wc -l`
rel=`cat /etc/slitaz-release`

# Kill the notification icon after user pressed on it.
kill_notification() {
	for p in `ps | grep "yad --notification" | awk '{print $1}'`
	do
		kill -9 $p 2>/dev/null
	done
}

# Message for up
up_msg() {
	eval_gettext "<b>There are \$up upgradeable packages for your SliTaz \$rel</b>
You should upgrade your system to get all the latest fixes
and improvements from the SliTaz contributors"
}

# Message for list older than 10 days
old_list_msg() {
	eval_gettext "<b>Your SliTaz \$rel packages list is older than 10 days</b>
You should recharge the list to check for updates"
}

# Message if packages.list is missing
no_list_msg() {
	eval_gettext "<b>No packages list found on your SliTaz \$rel system</b>
You will need to recharge the list of mirrored packages
if you want to install packages or upgrade your system"
}

#
# Main GUI box for up packages
#
up_main() {
	yad --text --width=400 \
		--geometry="$NOTIFY_GEOM" \
		--undecorated \
		--title="TazPKG Notification" \
		--image="tazpkg-up" \
		--image-on-top \
		--on-top \
		--text="`up_msg`" \
		--button="`gettext \"Upgrade:2\"`" \
		--button="gtk-close:1"
}

# Notification icon for up packages
up_notify() {
	yad --notification \
		--image=tazpkg \
		--text="`up_msg`" \
		--command="tazpkg-notify upgrade"
}

# Main up function
up() {
	# Store box results
	main=`up_main`
	ret=$?
	kill_notification
	# Deal with --button values
	case $ret in
		1) exit 0 ;;
		#tazweb http://tazpanel:8090/pkgs.cgi?up &
		2) subox tazpkgbox & ;;
		*) continue ;;
	esac
}

#
# Main GUI box for missing packages.list
#
old_list_main() {
	yad --text --width=400 \
		--geometry="$NOTIFY_GEOM" \
		--undecorated \
		--title "TazPKG Notification" \
		--image="tazpkg-up" \
		--image-on-top \
		--on-top \
		--text "`old_list_msg`" \
		--button="`gettext \"Recharge now:2\"`" \
		--button="gtk-close:1"
}

# Notification icon if no packages.list
old_list_notify() {
	yad --notification \
		--image=tazpkg \
		--text="`old_list_msg`" \
		--command="tazpkg-notify no-list"
}

# Main missing packages.list function
old_list() {
	# Store box results
	main=`old_list_main`
	ret=$?
	kill_notification
	# Deal with --button values
	case $ret in
		1) exit 0 ;;
		# tazweb http://tazpanel:8090/pkgs.cgi?recharge &
		2) subox tazpkgbox & ;;
		*) continue ;;
	esac
}

#
# Main GUI box for missing packages.list
#
no_list_main() {
	yad --text --width=400 \
		--geometry="$NOTIFY_GEOM" \
		--undecorated \
		--show-uri \
		--title="TazPKG Notification" \
		--image="tazpkg-up" \
		--image-on-top \
		--on-top \
		--text "`no_list_msg`" \
		--button="`gettext \"Recharge now:2\"`" \
		--button="gtk-close:1"
}

# Notification icon if no packages.list
no_list_notify() {
	yad --notification \
		--image=tazpkg \
		--text="`no_list_msg`" \
		--command="tazpkg-notify no-list"
}

# Main missing packages.list function
no_list() {
	# Store box results
	main=`no_list_main`
	ret=$?
	kill_notification
	# Deal with --button values
	case $ret in
		1) exit 0 ;;
		# tazweb http://tazpanel:8090/pkgs.cgi?recharge &
		2) subox tazpkgbox & ;;
		*) continue ;;
	esac
}

#
# Script commands
#

case $1 in
	upgrade)
		up ;;
	old-list)
		old_list ;;
	no-list)
		no_list ;;
	*)
		# Sleep first to let tazpkg upgrade on boot finish. Check if
		# any upgrades, then for an old list and then if any lists at all
		# (live or first boot)
		sleep 10
		[ "$up" -gt 0 ] && up_notify && exit 0
		mtime=`find /var/lib/tazpkg/packages.list -mtime +10;`
		[ "$mtime" ] && old_list_notify && exit 0
		[ ! -f $LOCALSTATE/packages.list ] && no_list_notify \
			&& exit 0 ;;
esac

exit 0		
