#!/bin/sh
#
# +ICON_THEMES - icon theme cache management script
#
# Usage: ./+ICON_THEMES ADD|REMOVE [metadatadir]
#
# This script supports two actions, UPDATE and REMOVE, that will update or
# remove cache files for icon themes from the package associated with
# <metadatadir>.
#
# Lines starting with "# ICON_THEME: " are data read by this script that
# name the icon theme and directory containing the "index.theme".
#
#	# ICON_THEME: hicolor
#	# ICON_THEME: gnome
#
# For each ICON_THEME entry, if the path is relative, that it is taken to be
# relative to ${PKG_PREFIX}/share/icons.
#

ECHO="echo"
GREP="/bin/grep"
INSTALL_INFO=""
MKDIR="/bin/mkdir -p"
PWD_CMD="/bin/pwd"
RM="/bin/rm"
RMDIR="/bin/rmdir"
SED="/bin/sed"
SORT="/bin/sort"
TEST="test"
GTK2_UPDATE_ICON_CACHE="/afs/tcreech.com/system/amd64_linux26/bin/gtk2-update-icon-cache"
GTK3_UPDATE_ICON_CACHE="/afs/tcreech.com/system/amd64_linux26/bin/gtk-update-icon-cache"
: ${GTK_UPDATE_ICON_CACHE=true}

SELF=$0
ACTION=$1

CURDIR=`${PWD_CMD}`
PKG_METADATA_DIR="${2-${CURDIR}}"
: ${PKGNAME=${PKG_METADATA_DIR##*/}}
: ${PKG_PREFIX=/afs/tcreech.com/system/amd64_linux26}

for _t in ${GTK3_UPDATE_ICON_CACHE} ${GTK2_UPDATE_ICON_CACHE}; do
	if ${TEST} -x $_t; then
		GTK_UPDATE_ICON_CACHE=$_t;
		break;
	fi
done

update_icon_cache()
{
	_dir="$1"
	if ${TEST} ! -f "$_dir/index.theme"; then
		:
	else
		${GTK_UPDATE_ICON_CACHE}  -f -q "$_dir"
	fi
}

exitcode=0
case $ACTION in
UPDATE)
	${SED} -n "/^\# ICON_THEME: /{s/^\# ICON_THEME: //;p;}" ${SELF} | \
	${SORT} -u |
	while read theme; do
		case $theme in
		"")	continue ;;
		[!/]*)	theme="${PKG_PREFIX}/share/icons/$theme" ;;
		esac

		update_icon_cache $theme
	done
	;;

REMOVE)
	${SED} -n "/^\# ICON_THEME: /{s/^\# ICON_THEME: //;p;}" ${SELF} | \
	${SORT} -u |
	while read theme; do
		case $theme in
		"")	continue ;;
		[!/]*)	theme="${PKG_PREFIX}/share/icons/$theme" ;;
		esac

		${RM} -f "$theme/icon-theme.cache"
	done
	;;
esac
exit $exitcode

# ICON_THEME: hicolor
