#!/usr/bin/bash
# A simple bash script to screen capture
#
# Copyright (C) 2005,2006  Charles Bouveyron <charles.bouveyron@free.fr>
# 
# 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

format=$1
text=`echo $format |tr "[a-z]" "[A-Z]"`

test -n "$KDEHOME" || KDEHOME="`kde4-config --localprefix`"; export KDEHOME
#KIMDIR=`mktemp -d "$KDEHOME"/tmp-"$USER"/kim.XXXXXXXX` || exit 1
KIMDIR=`mktemp -d "$KDEHOME"/tmp-"$HOSTNAME"/kim.XXXXXXXX` || exit 1
title=`gettext ("Kim - $format animation")`
param=`gettext "Choose duration of animation (sec):"`
tps=`kdialog --title "$title" --inputbox "$param" 10`
nb=$[3*$tps]
param=`gettext "Click 'Ok' and then choose the window target !"`
kdialog --title "$title" --msgbox "$param"
id=`xwininfo | grep "id:" | sed -e "s/.*id: //" -e "s/ .*//"`;
let x=1;

# loop until it has captured the number of captures requested
while [ "$x" -le "$nb" ]
do
  	if [ "$x" -lt "10" ]
	then
		xwd -id "$id" -silent -out "$KIMDIR"/capture0"$x".xwd;
		#import -window $id "$KIMDIR"/capture0"$x".miff; 
	else
		xwd -id $id -silent -out "$KIMDIR"/capture"$x".xwd;
		#import -window $id "$KIMDIR"/capture"$x".miff; 
	fi
	#sleep 0.25
	let x+=1
done

# convert all images in a simple gif animation
# mpeg2vidcodec is required for mpeg movie !
convert -delay 30 "$KIMDIR"/capture*.xwd `kdialog --title "$title" --getsavefilename ~ *."$format"`

#rm "$KIMDIR"/capture*.xwd
rm -rf "$KIMDIR"
msg=`gettext "The animation is created !"`
kdialog --title "$title" --msgbox "$msg"