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

FILE="";
let "nbfiles = $#"
title="`gettext "Kim - Slideshow"`"
kdeinstdir=`kde4-config --prefix`
KIM_DIR=$kdeinstdir/share/apps/kim/slideshow
dtitle=`gettext "Title:"`
gtitle=`gettext "My title ..."`
TITLE=`kdialog --title "$title" --inputbox "$dtitle" "$gtitle"`;
gauthor=`gettext "Author:"`
AUTHOR=`kdialog --title "$title" --inputbox "$gauthor" "$USER"`;
DATE=`date '+%A %-d %B %Y'`;
size=`gettext "Size:" `
SCALE=`kdialog --title "$title" --inputbox "$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}'`;
init=`gettext "Initialising"`
dbusRef=`kdialog --title "$title" --progressbar "$init" $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 test "true" = `qdbus $dbusRef wasCancelled`;then
			qdbus $dbusRef close
			exit 1
		fi
		let "compteur +=1"
		FILE="$i"
		qdbus $dbusRef setLabelText "`gettext "Treatment of file: "` `basename "$FILE"`"
		convert -resize $SCALE "$FILE" $DIR/images/`basename "$FILE"`.jpg;
		echo '<img src="images/'`basename "$FILE"`'.jpg" title="'`basename "$FILE"`'" />' >> $DIR/dewslider.xml;
 		qdbus $dbusRef value "$compteur"
	fi;
done

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