#!/bin/bash

SIZE=$1

usage() {
       echo "You must tell maximum size in ZZZxYYY."
       echo
       echo "Example: $0 640x640"
       exit 1
}

if [ "$SIZE" == "" ]; then
	usage
	exit 1
fi

if [ ! -d smaller ]; then
	echo "Creating dir smaller"
	mkdir -p smaller
fi

if [ ! -d old ]; then
	echo "Creating dir old"
	mkdir -p old
fi

for file in ./* ; do
	if [ "`echo $file |grep .[Jj][Pp][Gg]`" ] ; then
		if [ ! -f smaller/$file ] ; then
			echo "Converting $file"
			convert -size $SIZE $file -resize $SIZE +profile "*" smaller/$file
			mv $file old/$file
		else
			echo "$file already converted or file with same name already exists"
		fi
	fi
done

