#!/bin/bash -e

# Rename image according to GPS timestamp
# Images will be renamed also when no GPS DateTime is set but the
# timestamp might need an offset for other time zones.

unset LANG
unset LANGUAGE
unset LC_ALL

usage() { echo "Usage: $0 [-o <0-12 hours>]" 1>&2; exit 1; }

dateoffset()
{
  if [ "$2" != "" ] ; then
    adjust=$((offset-$2))
  else
    adjust=$offset
  fi
  DAY=`echo $1 | sed 's/_.*$//'`
  HOURS1=`echo $1 | sed 's/^.*_\([0-9][0-9]\).*/1\1/'`
  MINSEC=`echo $1 | sed 's/^.*_[0-9][0-9]\(.*\)/\1/'`
  HOURS1=$((HOURS1+adjust))
  echo ${DAY}_`echo ${HOURS1}|sed 's/^1//'`${MINSEC}
}

move_and_touch()
{
  img=$1
  DATE=$2
  newname=$3
  TOUCHDATE=`echo $DATE | sed 's/\([0-9][0-9][0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)_\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)/\1-\2-\3 \4:\5:\6/'`
  if [ "$TOUCHDATE" != "" ] ; then
     touch --date="$TOUCHDATE" "$img" && true
  fi
  chmod uga-x "$img"
  if [ "$newname" = "" ] ; then
    echo "ERROR: No proper name found for image $img"
  else
    mv "$img" "$newname"
  fi
}

offset=0
while getopts ":o:" o; do
    case "${o}" in
        o)
            o=${OPTARG}
            ((o == 0 || o == 1 || o == 2 || o == 3 || o == 4 || o == 5 || o == 6 || o == 7 || o == 8 || o == 9 || o == 10 || o == 11 || o == 12 )) || usage
            offset=$o
            ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

if [ -z "${offset}" ] ; then
    usage
fi

# echo "offset = ${offset}"

rename 's/ /_/g' [A-z]*.jpg [A-z]*.JPG [1-9]*.JPG [1-9]*.jpg
for img in `ls [0-9]*.jpg [0-9]*.jpg_ 2>/dev/null` ; do
    if echo "$img" | grep -q '^20[12][0-9]\{5\}_[0-9]\{6\}' ; then
	if echo "$img" | grep -qi '^20[12][0-9]\{5\}_[0-9]\{6\}.*\.jpg' ; then
            NAMEDATE=`echo $img | sed 's/^\(20[12][0-9]\{5\}_[0-9]\{6\}\).*\.jpg_*/\1/'`
            if [ "$NAMEDATE" = "" ] ; then
                echo "Unable to obtain named date from image $img ."
            else
                GPSDATE=`exiftool -GPSDateTime "$img" | sed -e 's?^GPS Date/Time[[:space:]]*??i' -e 's/: *//g' -e 's/Z$//g' -e 's/ /_/g'`
                if [ "$GPSDATE" = "" ] ; then
                    echo "No GPS Date/Time set for $img - we are using offset $((offset - 2))!!!"
                    ORIGDATE=`exiftool -DateTimeOriginal "$img" | sed -e 's?^Date/Time Original[[:space:]]*??i' -e 's/: *//g' -e 's/ /_/g'`
                    ###  FIXME: offset -2 is only for camera setting to European Summer time setting of the camera !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    GPSDATE=`dateoffset $ORIGDATE 2`
        	else
                    GPSDATE=`dateoffset $GPSDATE`
                fi
                if [ "$NAMEDATE" = "$GPSDATE" ] ; then
                    echo "$img is properly named according its GPS date $GPSDATE"
                else
                    newname=`echo $img | sed "s/^${NAMEDATE}_/${GPSDATE}_/"`
                    if [ "$img" = "$newname" ] ; then
                        echo "Hmmm, can not find a proper new name for $img."
                    else
                        move_and_touch $img $GPSDATE $newname
                    fi
                fi
            fi
	else
	    echo "do nothing with image $img"
	fi
    else
	echo "You should probably use cam_rename first to rename $img"
    fi
done
