#!/bin/bash
# Author : Eddy (ed42@free.fr)
# Date   : 6/11/2005
# Modifié le 23/04/2011 par bract@free.fr pour fonctionner sous debian squeeze avec icedove
# Warning : icedove, zenity and imagemagick packages must be installed
#

# Default language = English
_warning_title="Warning"
_warning_text="Please select at least one file !"
_progress_title="Preparing"
_progress_first_text="Processing ..."
_progress_text="Processing file"
_prompt_title="Scale image(s) ..."
_prompt_text="Choose the desired definition
(or Cancel to send pictures unmodified)"
_prompt_column_title="Size"
case $LANG in
# If language = French
	fr* )
	_warning_title="Attention"
	_warning_text="Il faut sélectionner au moins un fichier !"
	_progress_title="Préparation"
	_progress_first_text="Traitement en cours ..."
	_progress_text="Traitement du fichier"
	_prompt_title="Redimensionner l'(es) image(s) ..."
	_prompt_text="Choisir une définition maximum ci-dessous
(ou cliquer sur Annuler pour envoyer les images sans modification)"
	_prompt_column_title="Taille"
esac

# First we make sure there's something selected.
if [ $# -eq 0 ]; then
	zenity --warning --title="$_warning_title" --text="$_warning_text"
	exit 1
fi

# Store the number of files to process.
NB_FILES=$#
# Reset the attachment list.
echo "" > /tmp/attachment.log

(while [ $# -gt 0 ];
do
  # Store the file name and uri.
  FILE_NAME="$1"
  FILE_URI="$NAUTILUS_SCRIPT_CURRENT_URI/$FILE_NAME"
  # Send info to the progress window.
  let "COMPTEUR += 1"
  echo "# $_progress_text $FILE_NAME ($COMPTEUR/$NB_FILES) ..."
  let "PROGRESS = COMPTEUR*100/NB_FILES"
  echo $PROGRESS
  # Test if the file is a picture.
  # The following line may not work if the file is on the desktop ...
  # IS_IMAGE=`file -bi "$FILE_NAME" | grep -c image`
  # so we do differently.
  # Here we reduce only jpeg files.
  # If we wanted to reduce other types of picture file then we could do something like that :
  # IS_IMAGE=`echo $FILE_NAME | grep -c -E [.]jpg\|[.]gif\|[.]png`
  IS_IMAGE=`echo $FILE_NAME | grep -c -E [.]jpg`
  if [ $IS_IMAGE != 0 ]; then
    # If the file is an image, prompt for reducing it.
    if [ ! $DEF_ASKED ]; then
      # Wait a little so that the window does not appear under the progress window ...
      sleep 2
      DEF=`zenity --list --title "$_prompt_title" --text="$_prompt_text" --radiolist --column "" --column $_prompt_column_title FALSE 1280x1024 TRUE 1024x768 FALSE 800x600 FALSE 640x480`
      MAX_DEF=`echo "$DEF" | awk -F 'x' '{ print $1 }'`
      DEF="${MAX_DEF}x${MAX_DEF}"
      DEF_ASKED=1
    fi
    if [ "$DEF" != "" ]; then
      # Build a reduced copy of the file in the trash.
      # The following line may not work if the file is on the desktop.
      # convert "$FILE_NAME" -resize $DEF "$HOME/.Trash/files/$FILE_NAME"
      # so we do something quite more complicated ...
      WORKDIR=$(echo $NAUTILUS_SCRIPT_CURRENT_URI | sed '	s/file:\/\///g 
								s/%20/\ /g
								s/%23/\#/g
								s/%24/\$/g
								s/%25/\%/g
								s/%35/\?/g
								s/%3B/\;/g
								s/%3C/\</g
								s/%3E/\>/g
								s/%40/\@/g
								s/%5B/\[/g
								s/%26/\&/g
								s/%2C/\,/g
								s/%5E/\^/g
								s/%5D/\]/g
								s/%60/\`/g
								s/%7B/\{/g
								s/%7C/\|/g
								s/%7D/\}/g
								s/%C3%A9/\Ã©/g
								s/%C3%B9/\Ã¹/g
								s/%C3%A8/\Ã¨/g
								s/%C3%A7/\Ã§/g
								s/%C3%A0/\Ã /g')

      convert "$WORKDIR/$FILE_NAME" -scale $DEF "$HOME/.local/share/Trash/files/$FILE_NAME"
      # Store the reduced file uri in place of the file uri.
      FILE_URI="file://$HOME/.local/share/Trash/files/$FILE_NAME"
    fi
  fi
  # Add the file uri to the list.
  if [ "$ATTACHMENT" == "" ]; then
    ATTACHMENT=$FILE_URI
  else
    ATTACHMENT=$ATTACHMENT,$FILE_URI
  fi
  # Store the attachement list into a text file
  echo $ATTACHMENT > /tmp/attachment.log
  # And continue with the next file ...
  shift
done) | zenity --progress --auto-close --title="$_progress_title"  --text="$_progress_first_text"  --percentage=0

# Quotes are required for the attachment list
ATTACHMENT=`cat /tmp/attachment.log`
ATTACHMENT=\'$ATTACHMENT\'

# Test if Thunderbird is running to make properly the attachment.
MOZ_ON=`ps -ec | grep -c icedove`
if [ $MOZ_ON != 0 ]; then
  /usr/lib/icedove/mozilla-xremote-client "xfeDoCommand(composeMessage,attachment=$ATTACHMENT)"
else
  icedove -compose "attachment=$ATTACHMENT"
fi