#!/bin/bash

# **************************************************************
#
# This script renames the original image files to a name 
# based on the date and time of exposure plus a camera model
# identifier derived from the exif information. 
#
# The original filenumber and the new filename are saved to a file 
# for future reference.
#
# Both jpeg and raw image files are renamed.
# Any raw image files are subsequently moved to a subdir named raw.
#
# This script is licensed under GPL version 2 or any subsequent
# version at your choice.
# Copyright 2012 - 2014 by Michael N. Steen
#
# Use at your own risk. There is no warrenty for anything at all
# except using space on your harddisk.
#
# Dependency: exiftool.
#
# **************************************************************

# default values
rawext="cr2"
log="billednr.txt"
verbose="false"
L_only="false"

# read arguments
if [ "$#" -gt 0 ]; then
while [ "$#" -gt 0 ]
do
  case "$1" in
    -l|--log   )	log="$2"
			shift  ;;
    -L|--log-only )	L_only="true" ;;
    -r|--raw   )	rawext="$2"
			shift  ;;
    -s|--separator )	separator="$2"
			shift  ;;
    -v|--verbose )	verbose="true" ;;
    -?|--help  )
    		echo "$0 - Copyright 2012-2014 by Michael N. Steen"
    		echo "See the file for license information."
    		echo "A number of options may be used. Defaults are given in brackets."
    		echo " -l --log	 : Log file [$log]"
    		echo " -L --log-only : Only log update [$L_only]"
    		echo " -r --raw	 : Raw file extension [$rawext]"
    		echo " -v --verbose    : Show settings. [$verbose]"
		exit 0
		;;
  esac
  shift
done
fi

if [ "$verbose" = "true" ]; then
   echo "$0 - Copyright 2008 - 2014 by Michael N. Steen"
   echo "See the file for license information."
   echo "A number of options may be used. Defaults are given in brackets."
   echo " Log file [$log]"
   echo " Only log update [$L_only]"
   echo " Raw file extension [$rawext]"
   echo " Show settings: [$verbose]"
fi

date >> $log
exiftool -ext jpg -if '$filename =~ "IMG_"' -p '$FileName --> $CreateDate - $Model' -q -f ./ | sort >> $log
if [ "$L_only" = "false" ]; then
  exiftool -if '$model =~ "EOS 70D"' -if '$filename =~ "IMG_"' '-FileName<CreateDate' -d %Y%m%d-%H%M%S_70D%%c.%%le ./ 
  exiftool -if '$model =~ "EOS 400D"' -if '$filename =~ "IMG_"' '-FileName<CreateDate' -d %Y%m%d-%H%M%S_EOS%%c.%%le ./ 
  exiftool -if '$model =~ "IXUS"' -if '$filename =~ "IMG_"' '-FileName<CreateDate' -d %Y%m%d-%H%M%S_IX%%c.%%le ./ 
  if [ -n "$(ls *.$rawext)" ]; then
    if [ ! -e "raw" ]; then
      mkdir raw
    fi
    mv *.$rawext raw/
  fi
fi
# date >> $log
# exiftool -p '$filenumber --> $filename' -q -f ./ | sort >> $log

