#!/bin/sh
# This file is part of debhelper-maemo-package-icons
# Copyright (C) 2008 Gabriel Schulhof <nix@go-nix.ca>
# Package debhelper-maemo-package-icons released under GPL (http://www.gnu.org/licenses/gpl.txt):
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.  

echo_help()
{
echo
echo 'Usage: dh_maemo_package_icons [ -r | --remove | -h | --help ]'
echo
################################################################################
echo 'Appends a uuencoded string representing a png icon named after a package'
echo 'defined in debian/control and located in debian/.'
echo
echo 'For example, if debian/control defines a package called "foo" and a png'
echo 'named "foo.png" is found under debian/, then "foo.png" is assumed to be'
echo 'of the prescribed size of 26x26 pixels, and will be added as a'
echo '"XB-Maemo-Icon-26: <uuencoded icon file contents>" tag to the appropriate'
echo 'section of the "debian/control" file.'
echo
echo 'Add dh_maemo_package_icons to the top of the binary-arch target and'
echo 'dh_maemo_package_icons --remove to the bottom of that target to leave the'
echo 'control file untouched at the end of the build process.'
}

echo_icon() # $1 = package_name
	{
	echo -n 'XB-Maemo-Icon-26: '
	uuencode -m debian/$1.png - | grep -vE '^(begin-base64|====$)' | awk '{printf $1;}'
	echo ''
	}

CURRENT_PACKAGE=""

if test "x$1" = "x"; then
	cat debian/control | \
	while true; do
		if read; then
			DO_ECHO=1
			if echo $REPLY | grep -q '^Package: '; then
				CURRENT_PACKAGE=`echo $REPLY | awk '{print $2;}'`
			fi
			if echo $REPLY | grep -q '^XB-Maemo-Icon-26: '; then
				DO_ECHO=0
			fi
			if test "$REPLY" = ""; then
				if test -f debian/$CURRENT_PACKAGE.png; then
					echo_icon $CURRENT_PACKAGE
				elif test "" != "$CURRENT_PACKAGE"; then
					echo -e "\033[7m$CURRENT_PACKAGE: No icon 'debian/$CURRENT_PACKAGE.png'\033[0m" > /dev/fd/2
				fi
				CURRENT_PACKAGE=""
			fi
			if test $DO_ECHO -eq 1; then
				echo $REPLY
			fi
		else
			if test -f debian/$CURRENT_PACKAGE.png; then
				echo_icon $CURRENT_PACKAGE
			elif test "" != "$CURRENT_PACKAGE"; then
				echo -e "\033[7m$CURRENT_PACKAGE: No icon 'debian/$CURRENT_PACKAGE.png'\033[0m" > /dev/fd/2
			fi
			break
		fi
	done > debian/control.icons && mv -f debian/control.icons debian/control
elif test "x$1" = "x-r" -o "x$1" = "x--remove"; then
	cat debian/control | grep -v '^XB-Maemo-Icon-26: ' > debian/control.no-icons && \
		mv debian/control.no-icons debian/control
elif test "x$1" = "x-h" -o "x$1" = "x--help"; then
	echo_help
fi
