1#!/bin/sh
2
3# icon-theme-installer
4# Copyright (C) 2006 Novell, Inc.
5# Written by Aaron Bockover <abock@gnome.org>
6# Licensed under the MIT/X11 license
7#
8# Modified by Peter Clifton to allow icons with numerals in the filename
9#
10# This script is meant to be invoked from within a Makefile/Makefile.am
11# in the install-data-local and uninstall-data sections. It handles the
12# task of properly installing icons into the icon theme. It requires a
13# few arguments to set up its environment, and a list of files to be
14# installed. The format of the file list is critical:
15#
16# <category>,<local-src-file-name>
17#
18#   apps,music-player-banshee.svg
19#   apps,music-player-banshee-16.png
20#   apps,music-player-banshee-22.png
21#
22# <category> is the icon theme category, for instance, apps, devices,
23# actions, emblems...
24#
25# <local-src-file-name> must have a basename in the form of:
26#
27#   proper-theme-name[-<SIZE>].<EXTENSION>
28#
29# Where <SIZE> should be either nothing, which will default to scalable
30# or \-[0-9]{2}, which will expand to <SIZE>x<SIZE>. For example:
31#
32#   music-player-banshee-16.png
33#
34# The <SIZE> here is -16 and will expand to 16x16 per the icon theme spec
35#
36# What follows is an example Makefile.am for icon theme installation:
37#
38# ---------------
39# theme=hicolor
40# themedir=$(datadir)/icons/$(theme)
41# theme_icons = \
42#	apps,music-player-banshee.svg \
43#	apps,music-player-banshee-16.png \
44#	apps,music-player-banshee-22.png \
45#	apps,music-player-banshee-24.png \
46#	apps,music-player-banshee-32.png
47#
48# install_icon_exec = $(top_srcdir)/build/icon-theme-installer -t $(theme) -s $(srcdir) -d "x$(DESTDIR)" -b $(themedir) -m "$(mkinstalldirs)" -x "$(INSTALL_DATA)"
49# install-data-local:
50#	$(install_icon_exec) -i $(theme_icons)
51#
52#	uninstall-hook:
53#		$(install_icon_exec) -u $(theme_icons)
54#
55#	MAINTAINERCLEANFILES = Makefile.in
56#	EXTRA_DIST = $(wildcard *.svg *.png)
57# ---------------
58#
59# Arguments to this program:
60#
61# -i         : Install
62# -u         : Uninstall
63# -t <theme> : Theme name (hicolor)
64# -b <dir>   : Theme installation dest directory [x$(DESTDIR)] - Always prefix
65#              this argument with x; it will be stripped but will act as a
66#              placeholder for zero $DESTDIRs (only set by packagers)
67# -d <dir>   : Theme installation directory [$(hicolordir)]
68# -s <dir>   : Source directory [$(srcdir)]
69# -m <exec>  : Command to exec for directory creation [$(mkinstalldirs)]
70# -x <exec>  : Command to exec for single file installation [$(INSTALL_DATA)]
71# <remainging> : All remainging should be category,filename pairs
72
73while getopts "iut:b:d:s:m:x:" flag; do
74	case "$flag" in
75		i) INSTALL=yes ;;
76		u) UNINSTALL=yes ;;
77		t) THEME_NAME=$OPTARG ;;
78		d) INSTALL_DEST_DIR="`echo $OPTARG | sed 's;^x;;'`" ;;
79		b) INSTALL_BASE_DIR=$OPTARG ;;
80		s) SRC_DIR=$OPTARG ;;
81		m) MKINSTALLDIRS_EXEC=$OPTARG ;;
82		x) INSTALL_DATA_EXEC=$OPTARG ;;
83	esac
84done
85
86shift `expr $OPTIND - 1`
87
88if test "x$INSTALL" = "xyes" -a "x$UNINSTALL" = "xyes"; then
89	echo "Cannot pass both -i and -u"
90	exit 1
91elif test "x$INSTALL" = "x" -a "x$UNINSTALL" = "x"; then
92	echo "Must path either -i or -u"
93	exit 1
94fi
95
96if test -z "$THEME_NAME"; then
97	echo "Theme name required (-t hicolor)"
98	exit 1
99fi
100
101if test -z "$INSTALL_BASE_DIR"; then
102	echo "Base theme directory required [-d \$(hicolordir)]"
103	exit 1
104fi
105
106if test ! -x `echo "$MKINSTALLDIRS_EXEC" | cut -f1 -d' '`; then
107	echo "Cannot find '$MKINSTALLDIRS_EXEC'; You probably want to pass -m \$(mkinstalldirs)"
108	exit 1
109fi
110
111if test ! -x `echo "$INSTALL_DATA_EXEC" | cut -f1 -d' '`; then
112	echo "Cannot find '$INSTALL_DATA_EXEC'; You probably want to pass -x \$(INSTALL_DATA)"
113	exit 1
114fi
115
116if test -z "$SRC_DIR"; then
117	SRC_DIR=.
118fi
119
120for icon in $@; do
121	size=`echo $icon | sed -n 's/.*-\([0-9]*\).*/\1/p'`
122	category=`echo $icon | cut -d, -f1`
123	build_name=`echo $icon | cut -d, -f2`
124	install_name=`echo $build_name | sed 's/-[0-9]\+//g'`
125	install_name=`basename $install_name`
126
127	if test -z $size; then
128		size=scalable;
129	else
130		size=${size}x${size};
131	fi
132
133	install_dir=${INSTALL_DEST_DIR}${INSTALL_BASE_DIR}/$size/$category
134	install_path=$install_dir/$install_name
135
136	if test "x$INSTALL" = "xyes"; then
137		echo "Installing $size $install_name into $THEME_NAME icon theme"
138
139		$MKINSTALLDIRS_EXEC $install_dir || {
140			echo "Failed to create directory $install_dir"
141			exit 1
142		}
143
144		$INSTALL_DATA_EXEC $SRC_DIR/$build_name $install_path || {
145			echo "Failed to install $SRC_DIR/$build_name into $install_path"
146			exit 1
147		}
148
149		if test ! -e $install_path; then
150			echo "Failed to install $SRC_DIR/$build_name into $install_path"
151			exit 1
152		fi
153	else
154		if test -e $install_path; then
155			echo "Removing $size $install_name from $THEME_NAME icon theme"
156
157			rm $install_path || {
158				echo "Failed to remove $install_path"
159				exit 1
160			}
161		fi
162	fi
163done
164
165if test "x$INSTALL" = "xyes"; then
166	gtk_update_icon_cache_bin="`(which gtk-update-icon-cache || echo /opt/gnome/bin/gtk-update-icon-cache)2>/dev/null`"
167	gtk_update_icon_cache_bin="${GTK_UPDATE_ICON_CACHE_BIN:-$gtk_update_icon_cache_bin}"
168
169	gtk_update_icon_cache="$gtk_update_icon_cache_bin -f -t $INSTALL_BASE_DIR"
170
171	if test -z "$INSTALL_DEST_DIR"; then
172		if test -x $gtk_update_icon_cache_bin; then
173			echo "Updating GTK icon cache"
174			$gtk_update_icon_cache
175		else
176			echo "*** Icon cache not updated. Could not execute $gtk_update_icon_cache_bin"
177		fi
178	else
179		echo "*** Icon cache not updated. After install, run this:"
180		echo "***   $gtk_update_icon_cache"
181	fi
182fi
183
184