#!/bin/bash
if ! [ "$(whoami)" = "www-data" ]
then
	echo "sudo -u www-data"
	exit
fi
for image in ../*/*.{jpg,jpeg,png}
do
	width="$(identify -ping -format '%w' "$image")"
	if ((width > 640))
	then
		origimage="original/${image#../}"
		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
