#!/bin/bash

# CONFIGURATION
VERSION="0.2"
TITLE="Image anonymizer"

# ASSETS
GUIPATH=$(dirname "$0")
GUIPATH="$GUIPATH/gui"
GUIPATH=$(cd "$GUIPATH" && pwd)
if [[ -z "$GUIPATH" ]] ; then
  echo "For some reason, the path is not accessible."
  exit 1
fi

BASH=$(bash --version | head -1 | awk '{print $4}' | cut -d'(' -f1)
IM=$(convert -version | head -1 | awk '{print $3}' | cut -d'-' -f1)
EXIFTOOL=$(exiftool -ver)
OPTIPNG=$(optipng -version | head -1 | awk '{print $3}')
DIALOG=$(zenity --version)

# FUNCTIONS
zenexit() {
	zenity --window-icon="$GUIPATH/tux.png" \
		--width="200" \
		--title="$TITLE" \
		--error \
		--text="Operation canceled.\nThank you for using $TITLE." \
		2> /dev/null
	rm -f "$HTML"
	exit 1
}
zenstart() {
	zenity --window-icon="$GUIPATH/tux.png" \
		--title="$TITLE" \
		--width="600" --height="400" \
		--text-info \
		--html \
		--filename="$HTML" \
		--cancel-label="I don't care" \
		--ok-label="Choose a directory" \
		2> /dev/null
}
zendir() {
	DIR=$(zenity --window-icon="$GUIPATH/tux.png" --title="$TITLE" --file-selection --directory 2> /dev/null)
}
zenchoutff() {
	zenity --window-icon="$GUIPATH/tux.png" \
		--title="$TITLE" \
		--width="250" --height="20" \
		--text "Please choose the output format" \
		--list \
		--radiolist \
		--hide-header \
		--column "Radio buttons" \
		--column="Output file format" \
		--column="Output file format (extension)" \
		--hide-column=3 \
		--print-column=3 \
		TRUE PNG png FALSE JPEG jpg 2> /dev/null
}
zenend() {
	zenity --window-icon="$GUIPATH/tux.png" \
		--title="$TITLE" \
		--width="800" --height="600" \
		--text-info \
		--html \
		--filename="$HTML" \
		--cancel-label="Restart" \
		--ok-label="Exit" \
		2> /dev/null

	rm -f "$HTML"
}
zenjob() {
	zenity --window-icon="$GUIPATH/tux.png" \
		--title="$TITLE" \
		--progress \
		--width="600" \
		--percentage=0 \
		--cancel-label="Stop everything" \
		--ok-label="What happened?" \
		2> /dev/null
}

# MAIN
HTML="/tmp/ianon.html.$RANDOM"
cat "$GUIPATH/top.tpl" > $HTML
cat "$GUIPATH/start.tpl" \
	| sed s/'{VERSION}'/"$VERSION"/g \
	| sed s/'{BASH}'/"$BASH"/g \
	| sed s/'{DIALOG}'/"$DIALOG"/g \
	| sed s/'{IM}'/"$IM"/g \
	| sed s/'{EXIFTOOL}'/"$EXIFTOOL"/g \
	| sed s/'{OPTIPNG}'/"$OPTIPNG"/g \
	>> $HTML
cat "$GUIPATH/bottom.tpl" >> $HTML

zenstart
ZENSTART=$?
if [ "$ZENSTART" -eq 0 ]
then
  zendir
	ZENDIR=$?

	if [ "$ZENDIR" -eq 0 ]
	then
		cd $DIR;
		OUTFF=$(zenchoutff)
		if [ "$OUTFF" != "" ]; then
			cat "$GUIPATH/top.tpl" > $HTML
			echo "<h2>Thank you for using "$TITLE".</h2>" >> $HTML
			(
				P=0
				for F in *;
				do
						identify -quiet "$F" &> /dev/null
						if [ "$?" -eq 0 ]
						then

							OTYPE=$(file -b --mime-type "$F" | sed -e "s/^[^/]*\///")

							N=$(md5sum "$F" | awk '{print $1}')
							N="$N.anon.$OUTFF"

							OLDTYPE=$(file "$F" | cut -d':' -f2)
							OLDTYPE=$(echo $OLDTYPE | sed 's/ *$//g')
							if [ ${#OLDTYPE} -gt 64 ]
							then
								OLDTYPE="${OLDTYPE:0:61}..."
							fi

							# This is the important part of the script!
							[ "$OTYPE" != "$OUTFF" ] && convert "$F" "$N" &> /dev/null || cp "$F" "$N" &> /dev/null
							exiftool -overwrite_original -q -all= "$N" &> /dev/null
							[ "$OTYPE" == "png" ] && optipng --silent "$N" &> /dev/null

							OLDSIZE=$(ls -lah "$F" | awk '{print $5}')
							NEWSIZE=$(ls -lah "$N" | awk '{print $5}')

							OLDNAME="$F"
							if [ ${#OLDNAME} -gt 24 ]
							then
								OLDNAME="${OLDNAME:0:21}..."
							fi

							P=$(($P + 10))
							echo "$P"
							echo "# $OLDNAME ($OLDSIZE) $OLDTYPE"
							echo "<p>&larr; <b>$F</b> ($OLDSIZE)<br>	&bull; $OLDTYPE<br>&rarr; <b>$N</b> ($NEWSIZE)" >> $HTML
					fi
				done
				echo "100"
				echo "# Thats' all folks!"
			) | zenjob

			if [ "$?" -eq 1 ]
			then zenexit
			fi
		else
			zenexit
		fi
	fi
	if [ "$ZENDIR" -eq 1 ]
	then zenexit
	fi
	cat "$GUIPATH/bottom.tpl" >> $HTML
	zenend
	ZENEND=$?
	if [ "$ZENEND" -eq 1 ]
	then exit
	fi
	if [ "$ZENEND" -eq 0 ]
	then
		cd "$GUIPATH"
		exec ../$(basename $0) && exit
	fi
fi

if [ "$ZENSTART" -eq 1 ]
then zenexit
fi
