#!/bin/bash

# ICOnvert v1.0
# Alex Ibrado <alex@ibrado.com>
#   Please put "iconvert" in email subject to bypass filters
# Released under GPL 2, http://www.fsf.org/licenses/gpl.html

KDE_LOCAL_DIR="$HOME/.kde"
STATE_FILE="$HOME/.iconvert"

PARM=$1
shift

if [ "$PARM" != "-e" ] && [ "$PARM" != "-s" ] \
	&& [ "$PARM" != "-v" ] && [ "$PARM" != "-i" ]; then

	THEME=`cat $KDE_LOCAL_DIR/share/config/kdeglobals | grep Theme | cut -f2 -d=`
	if [ "$THEME" = "" ]; then
		THEME="N/A"
	fi

	cat <<END
ICOnvert v1.0 by Alex Ibrado <alex@ibrado.com>

Syntax:
    iconvert [-e][-s][-v][-i] "Some File.ico"

Parameters:
   -e   Extract PNGs 
   -s   Extract PNGs grouped by size
   -v   Preview in kuickshow
   -i   Import into current KDE icon theme ($THEME)
END

	exit
fi

ORIGPWD=`pwd`
DESTDONE=""

for FILE in $@; do
	FILE=$1
	cd "$ORIGPWD"

	if [ "$FILE" ]; then

		DIR=`dirname "$FILE"`
		BASE=`basename "$FILE"`
		SHORT="${BASE%%\.*}"

		# Support command line/relative links
		if [ "${DIR%%.*}" = "" ]; then
			DIR=`cd $(dirname '$FILE') ; pwd ; cd -`
			FILE="$DIR/$BASE"
			cd "$ORIGPWD"
		fi

		cd "$DIR"
		if [ "$PARM" = "-e" ]; then	
			DEST=`kdialog --title "ICOnvert" --inputbox "Enter destination folder for $BASE images:" "$SHORT"`
			if [ $? = 1 ]; then 
				shift
				continue
			fi

			mkdir -p "$DEST"
			cd "$DEST"

			convert "$FILE" "$SHORT.png"
			COUNT=0
			for PNG in *; do
				DIM=`file "$PNG" | cut -f2 -d, | tr  -d " "`
				mv -f "$PNG" "$SHORT-$DIM.png"
				let "COUNT=COUNT+1"
			done
			kdialog --title ICOnvert --msgbox "$BASE has been extracted as $COUNT PNGs in \"$DEST\"."
		elif [ "$PARM" = "-s" ] || [ "$PARM" = "-v" ] || [ "$PARM" = "-i" ]; then
			CURDIR=`pwd`
			if [ "$PARM" = "-i" ]; then
				THEME=`cat $KDE_LOCAL_DIR/share/config/kdeglobals | grep Theme | cut -f2 -d=`
				DEST="$KDE_LOCAL_DIR/share/icons/$THEME"

				TYPE=`kdialog --title "ICOnvert" --menu "Select icon type for $BASE:" actions Actions apps Applications devices Devices filesystems Filesystems mimetypes "MIME types"`
				if [ $? = 1 ]; then 
					shift
					continue
				fi

				NEWNAME=`kdialog --title "ICOnvert" --inputbox "Enter filename (will overwrite existing files):" "$SHORT.png"`
				if [ $? = 1 ]; then 
					shift
					continue
				fi
			else
				if [ "$PARM" = "-s" ]; then
					DEFAULT=""
					if [ -e "$STATE_FILE" ]; then
						DEFAULT=`grep default_size_dir "$STATE_FILE" | cut -f2 -d=`
					fi
					if [ "$DEFAULT" = "" ]; then
						DEFAULT="PNGs"
					fi
				else
					DEFAULT=$SHORT
				fi

				if [ "$PARM" != "-v" ] &&  [ "$DESTDONE" = "" ]; then
					DEST=`kdialog --title "ICOnvert" --inputbox "Enter destination folder:" "$DEFAULT"`
					if [ $? = 1 ]; then 
						shift
						continue
					fi
					DESTDONE=1
				fi
				
				if [ -e "$STATE_FILE" ]; then
					cat "$STATE_FILE" | grep -v "default_size_dir" > "$STATE_FILE-tmp"
					cp -f "$STATE_FILE-tmp" "$STATE_FILE"
					rm -f "$STATE_FILE-tmp"
				fi
				echo "default_size_dir=$DEST" >> "$STATE_FILE"

				mkdir -p "$DEST"
			fi

			TEMPDIR=`mktemp -d "/tmp/iconvert-$SHORT.XXXXXX"`
			cd "$TEMPDIR"
			convert "$FILE" "$SHORT.png"

			# Split to two, since DEST may be absolute or relative
			cd "$CURDIR"
			cd "$DEST"

			COUNT=0
			FCOUNT=0
			for PNG in "$TEMPDIR"/*; do
				DIM=`file "$PNG" | cut -f2 -d, | tr  -d " "`
				if [ "$PARM" = "-i" ]; then
					mkdir -p $DIM/$TYPE
					mv -f "$PNG" "$DIM/$TYPE/$NEWNAME"
				elif [ "$PARM" = "-s" ]; then
					let "FCOUNT=FCOUNT+1"
					mkdir -p $DIM
					mv -f "$PNG" "$DIM/$SHORT.png"
				else
					mv -f "$PNG" "$TEMPDIR/$SHORT-$DIM.png"
				fi
				let "COUNT=COUNT+1"
			done

			if [ "$PARM" = "-v" ]; then
				kuickshow "$TEMPDIR"
			elif [ "$PARM" = "-i" ]; then
				kdialog --title ICOnvert --msgbox "$BASE has been imported as $NEWNAME ($COUNT images)"
			elif [ "$PARM" = "-s" ]; then
				kdialog --title ICOnvert --msgbox "$BASE has been extracted as $COUNT PNGs in $FCOUNT folders inside \"$DEST\"." 
			fi

			rm -fR "$TEMPDIR"
		fi
	fi
	shift
done
