#!/bin/sh
# Create a Flash slideshow
FILE="";
let "nbfiles = $#"
kdeinstdir=`kde4-config --prefix`
KIM_DIR=$kdeinstdir/share/apps/kim/slideshow
TITLE=`kdialog --inputbox "Kim - Title:" "My title ..."`;
AUTHOR=`kdialog --inputbox "Kim - Author:" "$USER"`;
DATE=`date '+%A %-d %B %Y'`;
SCALE=`kdialog --inputbox "Kim - Size:" "600x450"`;
DIR=`kdialog --getexistingdirectory /home/$USER`;
W=`echo $SCALE | sed 's/[^0-9]/ /g' | awk '{print $1}'`;
H=`echo $SCALE | sed 's/[^0-9]/ /g' | awk '{print $2}'`;

dbusRef=`kdialog --progressbar "Kim - Initialising ..." $nbfiles`
qdbus $dbusRef showCancelButton true

mkdir $DIR/images;
cp $KIM_DIR/dewslider.swf $DIR;
# Header of $DIR/index.html
cp $KIM_DIR/index.part1 $DIR/index.html;
echo '<h1>'$TITLE'</h1>' >> $DIR/index.html;
echo '<h5>Author: '$AUTHOR'</h5>' >> $DIR/index.html;
echo '<h5>Date: '$DATE'</h5>' >> $DIR/index.html;
let W2="$W+40";
let H2="$H+60";
echo '<object type="application/x-shockwave-flash" data="dewslider.swf?xml=dewslider.xml" width="'$W2'" height="'$H2'">' >> $DIR/index.html;
cat $KIM_DIR/index.part2 >> $DIR/index.html;
# Header of $DIR/dewslider.xml
echo '<?xml version="1.0" ?>' > $DIR/dewslider.xml;
echo '<album' >> $DIR/dewslider.xml;
echo 'width="'$W'" height="'$H'"' >> $DIR/dewslider.xml;
echo 'bgcolor="0xeeeeee" framecolor="0xffffff" padding="10" margin="10"' >> $DIR/dewslider.xml;
echo 'shadow="8" shadowopacity="15%" shadowcolor="0x000000" showbuttons="on"' >> $DIR/dewslider.xml;
echo 'buttonstyle="flat" showtxt="on" txtcolor="0xcccccc" auto="5" fullscreen="off"' >> $DIR/dewslider.xml;
echo '>' >> $DIR/dewslider.xml;

compteur=0
for i in "$@";do
	if [ -f "$i" ];then
		#test if cancel button has been pushed
		if [[ "$(qdbus $dbusRef wasCancelled)" == "true" ]] ; then
			qdbus $dbusRef close
			exit 1
		fi
		let "compteur +=1"
		FILE="$i"
		convert -resize $SCALE "$FILE" $DIR/images/`basename "$FILE"`.jpg;
		echo '<img src="images/'`basename "$FILE"`'.jpg" title="'`basename "$FILE"`'" />' >> $DIR/dewslider.xml;
		qdbus $dbusRef setLabelText "Kim - Treatment of file: `basename "$FILE"`"
 		qdbus $dbusRef org.freedesktop.DBus.Properties.Set org.kde.kdialog.ProgressDialog value "$compteur"
	fi;
done

echo '</album>' >> $DIR/dewslider.xml;
qdbus $dbusRef close;
konqueror $DIR/index.html;