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

. /lib/libtaz.sh
. /etc/slitaz/slitaz.conf
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
rm -f $fifo
exit 0
}
case $1 in
usage|help|*-h)
_n "Usage:"; echo " $(basename $0)"
;;
*)
sleep 4
rm -f $fifo; mkfifo $fifo
exec 3<> $fifo
yad --notification --listen --image='software-update-available' <&3 &
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
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
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
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
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
