#!/bin/bash

# Original Script by (C) Nick Bargnesi nbargnesi at den-4.com
# This script is distributed under the GPL.  See the file GPL.
# 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="nuvoX_0.7"
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.
#
#	Note:
#	If you add additional sizes, you must update your index.desktop file to 
#	access the additional icons. I've added the non-standard 56x56 category 
#	as an example.
 
SIZES="128x128 96x96 72x72 64x64 56x56 48x48 $REQUIRED_SIZES"
DIRS="actions 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 "\nnuvoX icons theme for KDE.\n"
echo -ne "$PACKAGENAME\n\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

if test -f 128x128/apps/kmenu.png
	then
		cp -f 128x128/apps/kmenu.png 128x128/apps/kmenu.png
		cp -f 128x128/apps/kmenu.png 128x128/apps/go.png
		
#	Mandrake Specific Kmenu Icon to create

		cp -f 128x128/apps/kmenu.png 128x128/apps/menuk-mdk.png

#	Distribution Specific Kmenu Icon to create

	else
		echo -ne "Invalid selection (kmenu), the kmenu icon was not found in the .128x128/apps/ directory....exiting...\n \n"
		exit 1
fi
echo -ne "\nReady to go!  Converting all icons!...\nPLEASE WAIT..."
echo

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

# Required sizes for actions
#mkdir -p 32x32/actions 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 32x32 ../../32x32/actions/"$icon"
	convert "$icon" -resize 22x22 ../../22x22/actions/"$icon"
	convert "$icon" -resize 16x16 ../../16x16/actions/"$icon"
done


# Move to top directory
cd ../../
#cp -R addicons/application ~/.kde/share/mimelnk/
cp -R addicons/16x16/actions 16x16/
cp -R addicons/22x22/actions 22x22/
cp -R addicons/32x32/actions 32x32/
mkdir $PACKAGENAME
cp -R addicons/48x48/actions 48x48/
cp -R 128x128 $PACKAGENAME
cp -R 32x32 $PACKAGENAME
cp addicons/readme.txt $PACKAGENAME
cp license.txt $PACKAGENAME
cp addicons/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 "\nDone with conversions.\n"
echo -ne "Tarring and compressing.\n"
if test -f $COMPRESSOR
	then
		tar cf $PACKAGENAME.tar $PACKAGENAME && $COMPRESSOR $PACKAGENAME.tar
		echo -ne "\nThe $PACKAGENAME icon set has been built.  Use kcontrol to install the icon set.\n"
		echo && ls -sh $PACKAGENAME.tar* && echo
fi
echo -ne "Removing all temporary directories...\n"
rm -fr $PACKAGENAME

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

