#!/bin/bash
#/** **************************************************************************
# * Copyright (C) 2007 Eric Seigne <eric.seigne@ryxeo.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.
# *
# * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# * ***************************************************************************
# * File  : Original_WebGallery
# * Author  : Eric SEIGNE
# *           mailto:eric.seigne@ryxeo.com
# *           http://www.ryxeo.com/
# * Date    : 20/04/2007
# * Licence : GNU/GPL Version 2
# *
# * Description:
# * ------------
# *
# * Nautilus script to generate image galleries for use with original
# * http://jimmac.musichall.cz/original.php
# * based on imgconv bu Jakub Steiner 
# *
# * @version    $Id$
# * @author     Eric Seigne
# * @project   
# * @copyright  Eric Seigne 20/04/2007
# *
# * ************************************************************************* */

convertor=convert
convertormov=totem-video-thumbnailer
zip=zip
destination=web-gallery
version="0.1"
#################################################
#	TRADUCTIONS
	case "${LANG}" in
		######## Français ########
		fr* )
		msg_title="O.R.I.G.I.N.A.L WebGallery pour nautilus "$version""
		msg_pleasesel="Merci de selectionner au moins un fichier."
		msg_noselec=""$msg_title" permet de créer automatiquement une gallerie pour 'O.R.I.G.I.N.A.L'. "$msg_pleasesel""
		msg_warning="Attention"
		msg_nozip="Le programme $zip ne semble pas installé sur ce système, veuillez l'installer !"
		msg_noconvertor="Le programme $convertor ne semble pas installé sur ce système, veuillez l'installer !"
		msg_noconvertormov="Le programme $convertormov ne semble pas installé sur ce système, veuillez l'installer !" 
		msg_success="La conversion est terminée, le résultat est disponible dans le dossier $destination."
		msg_file="Fichier"
		;;
		######## Español ########
		es* )
		;;
		######## Deutsch ########
		de* )
		;;
		*)
		###### Default = English #####
		msg_title="O.R.I.G.I.N.A.L WebGallery for nautilus "$version""
		msg_pleasesel="Please select one or more files."
		msg_noselec=""$msg_title" automaticaly build 'O.R.I.G.I.N.A.L' gallery. "$msg_pleasesel""
		msg_warning="Warning"
		msg_nozip="$zip command seems to be not installed, please install it !"
		msg_noconvertor="$convertor command seems to be not installed, please install it !"
		msg_noconvertormov="$convertormov command seems to be not installed, please install it !" 
		msg_success="Done, result is stored in $destination directory."
		msg_file="File"
		;;
	esac

#################################################
#	PROGRAMME
#### Pas de fichiers sélectionné ###
if [ $# -eq 0 ]; then
	zenity --error --title="$msg_warning" --text="$msg_noselec"
	exit 1
fi
######## Test dépendance ########
which $convertor 2>/dev/null
if [ $? != 0 ]
then
	zenity --error --title="$msg_title" --text="$msg_noconvertor"
fi

which $zip 2>/dev/null
if [ $? != 0 ]
then
	zenity --error --title="$msg_title" --text="$msg_nozip"
fi

which $convertormov 2>/dev/null
if [ $? != 0 ]
then
	zenity --error --title="$msg_title" --text="$msg_noconvertormov"
fi

files=$@
nbfic=$#

mkdir ./$destination
mkdir ./$destination/thumbs
mkdir ./$destination/lq
mkdir ./$destination/mq
mkdir ./$destination/hq
mkdir ./$destination/zip #used for image tarballs
mkdir ./$destination/comments
chmod o+w ./$destination/comments #needed for user comments on the web

#strip exif info in thumbnails (usually a lot of data)
extra_opts="-strip"

#les echo sont utiles pour zenity progress
conversion()
{
       $convertor -geometry 120x120 -modulate 100,140,100 -unsharp 1x20 \
		 -quality 60 $extra_opts $imagefile ./$destination/thumbs/img-$i.jpg
       echo "33"
       $convertor -geometry 640x480 -modulate 100,130,100 -unsharp 1x5 \
		 -quality 90 $imagefile ./$destination/lq/img-$i.jpg
       echo "66"
       $convertor -geometry 1024x768 -modulate 100,130,100 -unsharp 1x5 \
		 -quality 80 $imagefile ./$destination/mq/img-$i.jpg
       echo "99"
       cp $imagefile ./$destination/hq/img-$i.jpg
       echo "100"
}

#les echo sont utiles pour zenity progress
conversionmov()
{
       $convertormov -s 120x120 $imagefile ./$destination/thumbs/img-$i.png && $convertor ./$destination/thumbs/img-$i.png ./$destination/thumbs/img-$i.jpg && rm -f ./$destination/thumbs/img-$i.png
       echo "33"
       $convertormov -s 640x640 $imagefile ./$destination/lq/img-$i.png && $convertor ./$destination/thumbs/img-$i.png ./$destination/thumbs/img-$i.jpg && rm -f ./$destination/thumbs/img-$i.png
       echo "66"
       $convertormov -s 1024x768 $imagefile ./$destination/mq/img-$i.png && $convertor ./$destination/thumbs/img-$i.png ./$destination/thumbs/img-$i.jpg && rm -f ./$destination/thumbs/img-$i.png
       echo "99"
       cp $imagefile ./$destination/hq/img-$i.mov
       echo "100"
}

i=1
for imagefile in $files
  do 
     ISMOV=`echo $imagefile | grep -iE '(mov|mpg)$'`
     #  c'est un jpeg
     if [ -z "${ISMOV}" ]; then
	 conversion | zenity --progress --text "$msg_file $imagefile ($i/$nbfic)" --auto-close --percentage=0
     # c'est un mov
     else
	 conversionmov | zenity --progress --text "$msg_file $imagefile ($i/$nbfic)" --auto-close --percentage=0
     fi

     # template for comment
     echo "<span>Photo $i</span>" > ./$destination/comments/$i.txt
     i=`expr $i + 1`
done

#####################
# Tar-up/zip images #
#####################

#tar cvjf $destination/zip/mq.tar.bz2 mq/*.jpg
#tar cvjf $destination/zip/hq.tar.bz2 hq/*.jpg
$zip -R $destination/zip/mq.zip  $destination/mq/*.jpg
$zip -R $destination/zip/hq.zip $destination/hq/*.jpg

zenity --info --title="$msg_title" --text="$msg_success"
