#!/bin/bash
if ! [ "$(whoami)" = "www-data" ]
then
	echo "sudo -u www-data"
	exit
fi
for image in ../*/*.{jpg,jpeg,png}
do
	origimage="original/${image#../}"
	if [ -f "$origimage" ]
	then
		continue
	fi
	#width="$(identify -ping -format '%wx$h' "$image")"
	IFS=x read width height < <(identify -ping -format '%wx%h' "$image")
	if ((width > 640)) || ((height > 640))
	then
		origdir="${origimage%/*}"
		if [ ! -d "$origdir" ]
		then
			mkdir "$origdir"
		fi
		if [ ! -f "$origimage" ]
		then
			mv "$image" "$origimage"
			convert "$origimage" -resize 640x640 "$image"
			echo "done $image $width"
		fi
	fi
done
