#!/bin/bash
# If you make any improvements, feel free to email me any changes. nbargnesi at den-4.com
# PACKAGENAME is the final tarred, compressed, iconset.
PACKAGENAME="Graphite-Rade8-3.1"
REQUIRED_SIZES="32x32 22x22 16x16"

# DEFINE NEW SIZES HERE
#	These are sizes for everything except the actions folder, which contains
#	menu and toolbar related stuff for KDE.  Add or remove a size here, I
#	would recommend not removing the 32, 22, and 16 sizes.
SIZES="96x96 72x72 64x64 48x48 $REQUIRED_SIZES"
DIRS="apps devices filesystems mimetypes" # no actions directory needed, its hardcoded

CONVERT_PATH=
TAR_PATH=
COMPRESSOR=
function checkCompressor() {
	echo -ne "Checking for bzip2... "
	FOUND=`which bzip2`
	if [ "$FOUND" != "" ]; then
		echo -ne "found $FOUND\n"
		COMPRESSOR=$FOUND
		return
	else
		echo -ne " no.\n"
		echo -ne "Checking for gzip... "
		FOUND=`which gzip`
		if [ "$FOUND" != "" ]; then
			echo -ne "found $FOUND\n"
			COMPRESSOR=$FOUND
			return
		else
			echo -ne " no.\n"
			echo -ne "\nNo compressor found (bzip2 | gzip).\n"
			exit 1
		fi
	fi
}
function checkNeeded() {
	echo -ne "Checking for tar... "
	FOUND=`which tar`
	if [ "$FOUND" != "" ]; then
		echo -ne " found $FOUND\n"
		TAR_PATH=$FOUND
		echo -ne "Checking for convert... "
		FOUND=`which convert`
		if [ "$FOUND" != "" ]; then
			echo -ne " found $FOUND\n"
			CONVERT_PATH=$FOUND
			return
		else
			echo -ne " no.\n"
			echo -ne "\nNo convert found in path.\n"
			exit 1
		fi
	else
		echo -ne " no.\n"
		echo -ne "\nNo tar found in path.\n"
		exit 1
	fi
}
function printFound() {
	echo -ne "\nDependencies met - this script is using:\n"
	echo -ne "\t\t$COMPRESSOR as compressor\n"
	echo -ne "\t\t$TAR_PATH as tar path\n"
	echo -ne "\t\t$CONVERT_PATH as convert path\n"
}

echo -ne "This script builds an installable KDE iconset using bash and convert.\n"
echo -ne "Change what you want, add additional sizes, whatever... :)\n"
echo

checkCompressor
checkNeeded
printFound
echo
echo -ne "You get your choice of kmenu icons.  Select from: \n"
echo -ne "\n\tgentoo, xeyes, redhat, mandrake, suse, openbsd, or nvidia and enter it now\n"
read KMENU_ICON
echo -ne "Using $KMENU_ICON.png as your kmenu icon.\n"
if test -f 128x128/apps/$KMENU_ICON.png
	then
		cp -f 128x128/apps/$KMENU_ICON.png 128x128/apps/kmenu.png
		cp -f 128x128/apps/$KMENU_ICON.png 128x128/apps/go.png
	else
		echo -ne "Invalid selection ($KMENU_ICON), exiting...\n"
		exit 1
fi
echo -ne "Ready to go!  Converting all icons! (about 15 seconds on an Athlon 64)\n"
echo

#Loop directory creation according to SIZES specified at startup
for size in $SIZES
do
	mkdir -p $size/apps $size/devices $size/mimetypes $size/filesystems
done

# Required sizes for actions
mkdir -p 22x22/actions 16x16/actions

# Mmmm... loops...
for dir in $DIRS
do
	cd 128x128/$dir
	for icon in *
	do
		# Loop the specified sizes
		for size in $SIZES
		do
			convert "$icon" -resize $size ../../$size/$dir/"$icon"
		done
	done
	# Move from 128x128/$directory to toplevel
	cd ../../
done

# Move from 128x128/ to 32x32/
cd 32x32/actions
for icon in *
do
	convert "$icon" -resize 22x22 ../../22x22/actions/"$icon"
	convert "$icon" -resize 16x16 ../../16x16/actions/"$icon"
done

# Move to top directory
cd ../../

mkdir $PACKAGENAME
cp -R 128x128 $PACKAGENAME
cp -R 32x32 $PACKAGENAME
cp index.desktop $PACKAGENAME

# Move/Remove the created directories so the user can rebuild if needed.
rm -fr 32x32/apps 32x32/devices 32x32/mimetypes 32x32/filesystems
for size in $SIZES
do
	if [ "$size" != "32x32" ]; then # Already did the 32x32 size above
		mv $size $PACKAGENAME
	fi
done

echo -ne "Done with conversions.\n"
echo -ne "Tarring and compressing.\n"
if test -f $COMPRESSOR
	then
		tar cf $PACKAGENAME.tar $PACKAGENAME && $COMPRESSOR $PACKAGENAME.tar
		echo -ne "$PACKAGENAME has been built.  Use kcontrol to install it.\n"
		echo && ls -sh $PACKAGENAME.tar* && echo
fi
echo -ne "Removing temporary directory...\n"
rm -fr $PACKAGENAME

echo -ne "\nAll done. ;)\n"

