#!/bin/sh -e

unset LANG
unset LANGUAGE
unset LC_ALL

for img in `ls [A-z]*.jpg [A-z]*.CHK 2>/dev/null` ; do
	DATE=`exif --tag='DateTimeOriginal' "$img" | grep -i VALUE | sed -e 's/^.*VALUE:[[:space:]]*//i' -e 's/://g' -e 's/ /_/'`
	if [ "$DATE" != "" ] ; then
	    TOUCHDATE=`exif --tag='DateTimeOriginal' "$img" | grep -i VALUE | sed -e 's/^.*VALUE:[[:space:]]*//i' -e 's/\([0-9]\+\):\([0-9][0-9]\):\([0-9][0-9] \)/\1-\2-\3/'`
	    CAM=`exif --tag='Model' "$img" | grep -i VALUE | head -n 1 | sed -e 's?^.*VALUE:[[:space:]]*\([^[:space:]/,]\+\)[/,]*.*?\1?i'`
	    #IMGSC=`echo "$img" | tr 'A-Z' 'a-z'`
	    touch --date="$TOUCHDATE" "$img"; mv "$img" "${DATE}_${CAM}.jpg"
	fi
done
