#!/bin/sh
if [ $# -ne 2 ] ; then
	echo "Usage: $0 <angle> <file>"
	exit -1
fi
if [ $1 -ne 90 -a $1 -ne 270 -a $1 -ne 180 ] ; then
	echo "<angle> has to be 90, 180 or 270"
	exit -1
fi
if ! file $2 | grep -q -i JPEG ; then
	echo "<file> has to be JPEG"
	exit -1
fi
#dir=`dirname $2`
#tmp=`mktemp -p $dir`

## jhead manual sucks
## jhead -cmd 'jpegtran -rotate $1 &i' "$2"

neu=`basename $2 .jpg`_r.jpg
orig=`basename $2 .jpg`.orig
jpegtran -rotate $1 "$2" > "$neu"
mv "$2" "$orig"
mv "$neu" "$2"

jhead -te "$orig" -dt "$2"

touch -r "$orig" "$2"
