#!/bin/sh

# The Booger Buster!
#
# This is a script to replace the default Debian Iceweasel icons with something more to your liking.  Some very nice ones are available at:
#
# https://wiki.ubuntu.com/IceWeaselIcon
#
# Usage:
# booger-buster input-file|URL
#
# That will run ImageMagick's convert utility to replace all of the icons in the Debian Iceweasel package with appropriately-resized versions of your input-file.  You should use a decently-sized input-file, probably a PNG that's at least 128x128 pixels.  You should run this as root or with sudo since it must replace files in /usr/share from the Debian package.
#
# You may use an HTTP URL as the argument, and the script will download it and use it as the replacement image.
#
# BE SURE TO PUT URLS IN QUOTATION MARKS.  Otherwise, special chars in the URL could cause weird shell commands to be run.

set -e

# Check for argument and root privileges
if [ -z "$1" ]
	then
	echo "Usage: booger-buster input-file|URL"
	echo "That will run ImageMagick's convert utility to replace all of the icons in the Debian Iceweasel package with appropriately-resized versions of your input-file.  You should use a decently-sized input-file, probably a PNG that's at least 128x128 pixels.  You should run this as root or with sudo since it must replace files in /usr/share from the Debian package."
	echo "You may use an HTTP URL as the argument, and the script will download it and use it as the replacement image."
	echo "BE SURE TO PUT URLS IN QUOTATION MARKS.  Otherwise, special chars in the URL could cause weird shell commands to be run."
	exit 1
elif [ "$(whoami)" != "root" ]
	then
	echo "Error: This script must be run with root privileges in order to replace the images from the Debian package.  Please change to root or run with sudo."
	exit 1
fi

# If the input-file is a URL, download it to a temp file, otherwise use the local file
if [[ "$1" == http* ]]
	then
	TEMPFILE="$(tempfile)"
	wget -O "$TEMPFILE" "$1"
	INPUTFILE="$TEMPFILE"
else
	INPUTFILE="$1"
fi

# Check image dimensions
WIDTH=$(identify -format "%h" "$INPUTFILE")
HEIGHT=$(identify -format "%w" "$INPUTFILE")
if [ "$HEIGHT" -le "128" ]
	then
	echo "Warning: input-file is less than 128 pixels in height.  This would cause the image to be scaled up, which wouldn't look good.  Please use an image that's at least 128x128 pixels.  This image is $HEIGHT pixels in height."
elif [ "$WIDTH" -le "128" ]
	then
	echo "Warning: input-file is less than 128 pixels in width.  This would cause the image to be scaled up, which wouldn't look good.  Please use an image that's at least 128x128 pixels.  This image is $WIDTH pixels in width."
fi
if [ "$WIDTH" -ne "$HEIGHT" ]
	then
	echo "Warning: input-file is not square.  This may result in a distorted icon.  Please use an exactly square input-file for best results.  This image is $WIDTH x $HEIGHT pixels."
fi

# Convert the PNGs
convert "$INPUTFILE" -resize 64x64 /usr/share/pixmaps/iceweasel.png
convert "$INPUTFILE" -resize 48x48 /usr/share/iceweasel/chrome/browser/content/branding/icon48.png
convert "$INPUTFILE" -resize 64x64 /usr/share/iceweasel/chrome/browser/content/branding/icon64.png
convert "$INPUTFILE" -resize 128x128 /usr/share/iceweasel/icons/mozicon128.png

# Convert the XPMs...
convert "$INPUTFILE" -resize 48x48 /usr/share/iceweasel/chrome/icons/default/default.png
convert "$INPUTFILE" -resize 16x16 /usr/share/iceweasel/chrome/icons/default/mozicon16.png
convert "$INPUTFILE" -resize 48x48 /usr/share/iceweasel/chrome/icons/default/mozicon50.png
convert "$INPUTFILE" -resize 16x16 /usr/share/iceweasel/icons/mozicon16.png
convert "$INPUTFILE" -resize 48x48 /usr/share/iceweasel/icons/mozicon50.png

# Rename the PNGs to XPMs.  Yes, this is an ugly hack, but as far as I can tell it works fine, and it's way better than looking at the ugliness that results from the alpha channel in a PNG turning into solid colors in the XPM.
mv /usr/share/iceweasel/chrome/icons/default/default.png /usr/share/iceweasel/chrome/icons/default/default.xpm
mv /usr/share/iceweasel/chrome/icons/default/mozicon16.png /usr/share/iceweasel/chrome/icons/default/mozicon16.xpm
mv /usr/share/iceweasel/chrome/icons/default/mozicon50.png /usr/share/iceweasel/chrome/icons/default/mozicon50.xpm
mv /usr/share/iceweasel/icons/mozicon16.png /usr/share/iceweasel/icons/mozicon16.xpm
mv /usr/share/iceweasel/icons/mozicon50.png /usr/share/iceweasel/icons/mozicon50.xpm

# Hooray, it worked!
echo "Icons replaced!  Enjoy your cool new Iceweasel!  (If for some reason you want the booger back, just reinstall the Iceweasel package.)"
exit 0