#!/bin/sh -e

shopt -s nullglob

update_icon_cache ()
{
	[ -z "${UHU_PKG_QUIET+x}" ] && echo -n "  Updating icon cache ..."
	for i in /usr/share/icons/*; do
		if [ -L "$i" -o ! -f "$i/index.theme" ]; then
			continue
		fi
		gtk-update-icon-cache -f -i -q "$i"
	done
	true > /var/lib/uhu-pkg/gtk-icon-cache
	while [ ! /var/lib/uhu-pkg/gtk-icon-cache -ot /var/lib/dpkg/info \
	     -a ! /var/lib/uhu-pkg/gtk-icon-cache -nt /var/lib/dpkg/info ]; do
		sleep 0.1
		true > /var/lib/uhu-pkg/gtk-icon-cache
	done
	[ -z "${UHU_PKG_QUIET+x}" ] && echo
}

if [ -f /var/lib/uhu-pkg/gtk-icon-cache -a /var/lib/uhu-pkg/gtk-icon-cache -nt /var/lib/dpkg/info ]; then
	exit 0
fi

if [ "$1" = "gtk-update-icon-cache" -o "$1" = "+" ]; then
	update_icon_cache
elif grep -q '^/usr/share/icons/' /var/lib/dpkg/info/"$1".list; then
	update_icon_cache
fi
