#!/usr/bin/sh
#  Unknown author  --> Unknown licence
# Added localisation and dbus support in 2013 by Daniel Tartavel <contact@librepc.com>


# Create a html gallery
export TEXTDOMAIN="kim"
export TEXTDOMAINDIR="/usr/share/locale"
. gettext.sh

FILE="";
let "nbfiles = $#"
kdeinstdir=`kde4-config --prefix`
KIM_DIR=$kdeinstdir/share/apps/kim/galery
dtitle=`gettext "Kim HTML Gallery"`
gtitle=`gettext "Title of the Gallery:"`
mtitle=`gettext "My title ..."`
author=`gettext "Author:"`
TITLE=`kdialog --title "$dtitle" --inputbox "$gtitle" "$mtitle"`;
if [ $? -ne 0 ]; then
	kdialog --title "$dtitle" --passivepopup "Operation cancelled" 5
    exit 1
fi
AUTHOR=`kdialog --title "$dtitle" --inputbox "$author" "$USER"`;
if [ $? -ne 0 ]; then
	kdialog --title "$dtitle" --passivepopup "Operation cancelled" 5
    exit 1
fi
DATE=`date '+%A %-d %B %Y'`;
SCALE_1="800x800";
SCALE_2="200x200";
ncol=`gettext "Number of columns:"`
K=`kdialog --title "$dtitle" --inputbox "$ncol" "4"`;
if [ $? -ne 0 ]; then
	kdialog --title "$dtitle" --passivepopup "Operation cancelled" 5
    exit 1
fi
DIR=`kdialog --title "$dtitle" --getexistingdirectory /home/$USER`;
if [ $? -ne 0 ]; then
	kdialog --title "$dtitle" --passivepopup "Operation cancelled" 5
    exit 1
fi
W="200";
H="150";
init=`gettext "Initializing"`
dbusRef=`kdialog --title "$dtitle" --progressbar "$init" $nbfiles`
qdbus $dbusRef showCancelButton true

# Creation of directories
mkdir $DIR/images;
mkdir $DIR/thumbs;
# Header of $DIR/index.html
cp $KIM_DIR/index.part1 $DIR/index.html;
echo '<h1>'$TITLE'</h1>' >> $DIR/index.html;
echo '<h5>`gettext "Author:"`'$AUTHOR'<br>Date: '$DATE'</h5>' >> $DIR/index.html;
echo '<table><tr>' >> $DIR/index.html;

compteur=0;
tmp=0;
for i in "$@";do
	if [ -f "$i" ];then
		#test if cancel button has been pushed
		if test "true" = `qdbus $dbusRef wasCancelled`;then
			qdbus $dbusRef close
			exit 1
		fi
		let "compteur +=1"
		let "tmp +=1"
		FILE="$i";
		IMAGE=`basename "$FILE"`;
		qdbus $dbusRef setLabelText "`gettext "Treatment of file:"` $IMAGE"
		convert -resize $SCALE_1 "$FILE" $DIR/images/"$IMAGE";
		convert -resize $SCALE_2 "$FILE" $DIR/thumbs/"$IMAGE";
		W2=`identify -format "%w" $DIR/images/"$IMAGE"`;
		H2=`identify -format "%h" $DIR/images/"$IMAGE"`;
		echo '<td align='center'>' >> $DIR/index.html;
		if [ "$H2" -lt "$W2" ];
		then echo '<a href="images/'"$IMAGE"'"><img class="photo" src="thumbs/'"$IMAGE"'" width="'$W'" height="'$H'" alt="thumbs/'"$IMAGE"'" title="'"$IMAGE"'"></a>' >> $DIR/index.html;
		else echo '<a href="images/'"$IMAGE"'"><img class="photo" src="thumbs/'"$IMAGE"'" width="'$H'" height="'$W'" alt="thumbs/'"$IMAGE"'" title="'"$IMAGE"'"></a>' >> $DIR/index.html;
		fi;
		echo '<p>'"$IMAGE"'</p>' >> $DIR/index.html;
		echo '</td>' >> $DIR/index.html;
		if [ "$tmp" -eq "$K" ];
		then 	echo '</tr><tr>' >> $DIR/index.html;
			tmp=0;
		fi
		
 		qdbus $dbusRef value "$compteur"
	fi;
done

cat $KIM_DIR/index.part2 >> $DIR/index.html;
qdbus $dbusRef close;

xdg-open $DIR/index.html;
