#!/bin/sh
# TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
# tazpkg-notify - part of TazPkg
# Notification icon for TazPkg packages

# Recharging pkgs list can be done automatically at boot, so notify users
# if some updates are available. Also notify 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) 2012-2015 SliTaz - GNU General Public License v3.
# Authors: See the AUTHORS files


. /lib/libtaz.sh
. /etc/slitaz/slitaz.conf

# I18n
export TEXTDOMAIN='tazpkg'

fifo=/tmp/$(basename $0).fifo
doc="file:///usr/share/doc/tazpkg/tazpkg.html"
alias notify=/usr/libexec/tazpkg-notification
cmd_recharge='tazbox su tazpanel pkgs#recharge'
cmd_up='tazbox su tazpanel pkgs#up'

installed=$(wc -l < "$PKGS_DB/installed.info")
text="$(_p \
		'%s installed package' \
		'%s installed packages' "$installed" \
		"<b>$installed</b>")"

[ -f "$PKGS_DB/IDs" ] && mtime=$(find "$PKGS_DB/IDs" -mtime +10;)
up=0; [ -f "$PKGS_DB/packages.up" ] && up=$(wc -l < "$PKGS_DB/packages.up")


set_yad_action() {
	echo -e "action:$1\ntooltip:$2\nicon:software-update-urgent" > $fifo
}


quit() {
	echo "quit" > $fifo
	# Clean-up
	rm -f $fifo
	exit 0
}


case $1 in
	usage|help|*-h)
		_n "Usage:"; echo " $(basename $0)"
		;;
	*)
		# Sleep before displaying the notification icon
		sleep 4
		# Manage the I/O redirection from shell
		rm -f $fifo; mkfifo $fifo
		# Attach a file descriptor
		exec 3<> $fifo
		# Notification icon
		yad --notification --listen --image='software-update-available' <&3 &

		# Notification menu (right click)
		cat > $fifo << EOT
menu:\
$(_n 'My packages'       )!tazpanel pkgs#list!package-x-generic|\
$(_n 'Recharge lists'    )!$cmd_recharge!system-software-update|\
$(_n 'Check upgrade'     )!$cmd_up!system-software-install|\
$(_n 'TazPkg SHell'      )!terminal -e tazpkg shell!utilities-terminal|\
$(_n 'TazPkg manual'     )!tazweb --notoolbar $doc!slitaz-doc|\
$(_n 'Close notification')!quit!gtk-close
EOT

		# Missing packages list
		if [ ! -f "$PKGS_DB/packages.info" ]; then
			tooltip="$(_ 'No packages list found')"
			set_yad_action "$cmd_recharge" "$tooltip"
			[ "$(notify "$tooltip" '1' "$(_n 'Recharge lists')")" = '1' ] && $cmd_recharge
			quit
		fi

		# Too old packages list
		if [ -n "$mtime" ]; then
			tooltip="$(_ 'Your packages list is older than 10 days')"
			set_yad_action "$cmd_recharge" "$tooltip"
			[ "$(notify "$tooltip" '1' "$(_n 'Recharge lists')")" = '1' ] && $cmd_recharge
			quit
		fi

		# Available upgrades
		if [ "$up" -gt 0 ]; then
			tooltip="$(_p \
				'There is %s upgradeable package' \
				'There are %s upgradeable packages' "$up" \
				"<b>$up</b>")"
			set_yad_action "$cmd_up" "$tooltip"
			[ "$(notify "$tooltip" '1' "$(_n 'Check upgrade')")" = '1' ] && $cmd_up
			quit
		fi

		# Nothing to do, close notification
		tooltip="$(_ 'System is up to date')"
		echo "tooltip:$tooltip\n$text" > $fifo
		answer=$(notify "$tooltip"$'\n'"$text" '0' "$(_n 'Recharge lists')" "$(_n 'Check upgrade')")
		case "$answer" in
			1) $cmd_recharge;;
			2) $cmd_up;;
		esac
		sleep 10
		quit
		;;
esac
quit
