#!/usr/bin/bash
#
# Copyright (C) Daniel Tartavel <contact@librepc.com>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, U
#
# Author : 	Charles Bouveyron <charles.bouveyron@free.fr>
#		Raphaël Pinson <raphink@raphink.net>
#
# Added localisation and dbus support in 2013 by Daniel Tartavel <contact@librepc.com>

export TEXTDOMAIN="kim"
export TEXTDOMAINDIR="/usr/share/locale"
. gettext.sh
. kim_functions

FILES="";
DIR="$2"
ATTACHMENTS="";
ACTION="$1";
init=`gettext "Initialising"`
kdeinstdir=`kde4-config --prefix`
let "nbfiles = $# -1"
title="`gettext "Kim - Images to GIF animation"`" 
dbusRef=`kdialog --title "$title" --progressbar "$init" $nbfiles`
qdbus $dbusRef showCancelButton true


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
		qdbus $dbusRef setLabelText "`gettext "Adding image:"` `basename "$i"`"
		let "compteur +=1"
		FILES+="$i "
		qdbus $dbusRef value "$compteur"
	fi
done
	
case $ACTION in 
    animation)
		param=`gettext "Delay between images in 1/100 second"`
		param2=`gettext "Name of the GIF animation image"`
		savefile="$DIR/`kdialog --title="$title" --inputbox "$param2"`.gif"
		file_exist $savefile
		qdbus $dbusRef setLabelText "`gettext "Creating GIF animation"`"
		convert -delay `kdialog --title="$title" --inputbox "$param" "100"` $FILES "$savefile"
		error $? "`gettext "command execution error "`" "convert -delay \`kdialog --title="$title" --inputbox "$param" "100"\` $FILES $savefile"
		fi
 		;;
    adjoin)
		param=`gettext "Name of the Tiff image"`
		savefile="$DIR/`kdialog --title="$title" --inputbox "$param"`.tiff"
		file_exist $savefile
		qdbus $dbusRef setLabelText "`gettext "Creating multipages TIFF"`"
		convert -adjoin $FILES "$savefile"
		error $? "`gettext "command execution error "`" "convert -adjoin $FILES $savefile"
 		;;
esac
qdbus $dbusRef close
