#!/bin/bash
# Original (NIS) author : Mathieu Vilaplana <mathieu@creationgif.com> 
# Modified by: Radagast <rhosgobel2@gmail.com>
# Date : 02/19/2007
#depends: imagemagick, zenity, rename
# thanks to coffe
#version 0.4
#	- check mime type
#since v 0.4, solve bug with filename spaces
#version 0.6
#     - correct bug in filename with spaces
#     - create a subdirectory to create images
#version 0.7
#     - changed name from NIS to Resize_images for ease of menu selection
#     - changed list to contain solely the max dimension
#     - added more size options
#     - made directory name nicer ("resized_to_xxx")
#     - appended image size to file name for jpgs and gifs ("file.jpg" becomes "file_xxx.jpg")
#     - created user-friendly dialog text
#version 0.75
#     - specified bash for Edgy compatibility, based on 0.8 of NIS (http://www.creationgif.com/debian/nis/); 
#     - added PNG to list of file types that is renamed.

#test if a file has been selected
if [ $# -eq 0 ]; then
	zenity --error --title="Viga!" --text="Pead valima tõõtlemiseks vähemalt ühe pildi"
	exit 1
fi

#=========================
#       SELECT SIZE DIALOG
# Add or remove (or resort) items from (in) the following list 
#   to customize the sizes available and the order in which they appear.
#   It would be nice to make this list easier to edit (make it a variable?  A file in user's home directory?)
title="Vali suurus"
dialogtext="Vali maksimaalne suurus(pikselites) \nmilleks soovid pilte muuta. \n\nNB! mõeldud .jpg ja .gif tüüpi pildifailide muutmiseks"
maximgsize=`zenity --title "$title" --text="$dialogtext" --list --separator=" " --column="suurus (px)" "75" "100" "150" "160" "200" "300" "320" "400" "500" "600" "640" "800" "1024" `

#if $? != 0, user click on cancel button, so exit
if [ "$?" != 0 ] ; then
	exit
fi

#user must select a target size
maximgsize=`echo $maximgsize | sed 's/ max//g'`
if [ ! "$maximgsize" ]; then
	zenity --error --title="Viga!" --text="Pead valima ka soovitud suuruse"
	exit
fi

#Assign max dimensions to variables for later.
imgsize="${maximgsize}x${maximgsize}" # used for the resize command
niceimgsize="$maximgsize" # used to append to file name
imgsizedir="suurusega_$maximgsize" # name of the directory that will be created

#       END SELECT SIZE DIALOG
#=========================

# Quality is currently hardcoded to be 80 (in the "convert" line, below).
# If desired, another dialog could be rasied here for user selection of quality level.

#Select only images
nb_images=0;
selection="";
while [ $# -gt 0 ]; do
	isimage=`file -bi "$1" | grep image | wc -l` 
	if [ $isimage -eq 1 ]; then
		selection[$nb_images]=$1
		let "nb_images++"
	fi
	shift
done

#create directory if not exist and at least one image to process
if [ ! -d $imgsizedir  ] && [ "$nb_images" -gt "0" ];then
		mkdir $imgsizedir
fi

#iterate through selected images, resize, and rename
n=$nb_images
let "n=n-1"
(for i in `seq 0 $n`;do
	picture=${selection[$i]}
	let "compteur += 1"
	echo "# Muudan pilti $compteur / $nb_images $picture ..."
	convert -quality 80 -resize $imgsize "$picture" $imgsizedir/"$picture"
        # rename the created file - there's got to be an easier way to handle all cases.
        rename s/.JPG$/_$niceimgsize.JPG/ $imgsizedir/"$picture"
        rename s/.jpg$/_$niceimgsize.jpg/ $imgsizedir/"$picture"
        rename s/.JPEG$/_$niceimgsize.JPEG/ $imgsizedir/"$picture"
        rename s/.jpeg$/_$niceimgsize.jpeg/ $imgsizedir/"$picture"
        rename s/.GIF$/_$niceimgsize.GIF/ $imgsizedir/"$picture"
        rename s/.gif$/_$niceimgsize.gif/ $imgsizedir/"$picture"
        rename s/.PNG$/_$niceimgsize.PNG/ $imgsizedir/"$picture"
        rename s/.png$/_$niceimgsize.png/ $imgsizedir/"$picture"
	let "progress = compteur*100/nb_images"
	echo $progress
done
) |
        zenity --progress --auto-close --title="Töötan..."  --text="Muudan pilte ..."  
--percentage=0


# 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.
# 
# A copy of the GNU General Public License is available at http://www.gnu.org/copyleft/gpl.html
# or can be obtained by writing Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
