#!/bin/bash
#
#  Extracts the Exif:DateTimeOriginal field and prepends
#  it to the image file to facilitate sorting.
#
#  One off script, possibly has bugs, use at your own risk.
#
#  You will need imagemagick:
#    yum install imagemagick
#    apt-get install imagemagick
#    (whatever you use to install it)
#
# Script=addexiftime
# Author=Michael
# Email=staff_scripts at the commercial domain inet-design
# Website=http://www.inet-design.com/
# License=GPL
# Script repository=
# # # #

#   Prettify things
INVT="\033[7m";NORM="\033[0m";BOLD="\033[1m";BLINK="\033[5m";BLACK_F="\033[30m";BLACK_B="\033[40m";RED_F="\033[31m";RED_B="\033[41m";GREEN_F="\033[32m";GREEN_B="\033[42m";YELLOW_F="\033[33m";YELLOW_B="\033[43m";BLUE_F="\033[34m";BLUE_B="\033[44m";MAGENTA_F="\033[35m";MAGENTA_B="\033[45m";CYAN_F="\033[36m";CYAN_B="\033[46m";WHITE_F="\033[37m";WHITE_B="\033[47m";
####

#  Who am I?
MyName=`basename "$0"`

#  Check usage
if [ "$1" != "" ] && [ ! -d "$1" ] ; then
  echo -e "$YELLOW_F""Usage               :""$NORM  $GREEN_F""$MyName [\"Path\"] [\"File Filter\"]""$NORM"
  echo "Default Path        :  Current Directory"
  echo "Default File Filter :  *.jpg"
  echo -e "$YELLOW_F""Example             :""$NORM  $GREEN_F""$MyName \"/home/user/some directory\" \"*.jpeg\"""$NORM"
  exit
fi

#  Say Hello
StartTime=`date +%s`
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo -e "$RED_F$CurrDateTime:$GREEN_F$BOLD Starting$NORM \"$MyName\" It can take awhile, need coffee?"

#  Do the work
if [ "$1" != "" ] ; then
  cd "$1"
fi

ResultsDir="`pwd`/results"

FileExt="*.jpg"
if [ "$2" != "" ] ; then
  FileExt="$2"
fi

FileExt="*"  # Comment this out or you get everything in the current dir.


FileList=`find ./ -maxdepth 1 -type f -name "$FileExt"`
FilesToProcess=`echo "$FileList" | wc -l`

D00=`date +"%y-%m-%d %H:%M:%S"`
MESG="Adding EXIF timestamps and copying to: $ResultsDir"
echo -e "$YELLOW_F$D00:$NORM $GREEN_F$MESG$NORM"
MESG="Files to Process :  $FilesToProcess"
echo -e "$YELLOW_F$D00:$NORM $GREEN_F$MESG$NORM"

FilesProcessed="0"
FilesProcessedwDate="0"
mkdir -p results

while IFS= read -r filename
do
  DateString=""
  NameofFile=`basename "$filename"`
  DateString=`identify -verbose "$NameofFile" | grep exif:DateTimeOriginal | awk '{ print $2" "$3 }' | sed -e s/":"/"."/g -e s/" "/"--"/g`
# echo
# echo $NameofFile , "$DateString"
# echo
  if [ "$DateString" = "" -o "$DateString" = ".--." ]; then
    DateString=`identify -verbose "$NameofFile" | grep exif:DateTime: | awk '{ print $2" "$3 }' | sed -e s/":"/"."/g -e s/" "/"--"/g`
    if [ "$DateString" = "" ]; then
      DateString="1900.00.00--00.00.00"
    fi
  fi
  #Strip off any existing dates
  NameofFile=`echo $NameofFile | sed s/"^[0-9]\{4\}.[0-9]\{2\}.[0-9]\{2\}.[0-9]\{2\}.[0-9]\{2\}.[0-9]\{2\}-\{1,2\}"/""/`
  NameofFile=`echo $NameofFile | sed s/"^[0-9]\{4\}.[0-9]\{2\}.[0-9]\{2\}-\{1,2\}[0-9]\{2\}.[0-9]\{2\}.[0-9]\{2\}-\{1,2\}"/""/`
  cp --preserve=timestamps "$filename" "results/$DateString--$NameofFile"
# echo "results/$DateString--$NameofFile"
  if [ "$DateString" != "1900.00.00--00.00.00" ]; then
    FilesProcessedwDate=$((FilesProcessedwDate + 1))
  fi
  FilesProcessed=$((FilesProcessed + 1))
  D00=`date +"%y-%m-%d %H:%M:%S"`
  MESG="Files Processed  :  $FilesProcessedwDate / $FilesProcessed"
  echo -en "$YELLOW_F$D00:$NORM $GREEN_F$MESG$NORM"
  echo -en "\r"
done <<< "$FileList"

D00=`date +"%y-%m-%d %H:%M:%S"`
MESG="Files Processed  :  $FilesProcessedwDate / $FilesProcessed"
echo -e "$YELLOW_F$D00:$NORM $GREEN_F$MESG$NORM"
D00=`date +"%y-%m-%d %H:%M:%S"`
MESG="Files in $ResultsDir : `ls -1 "$ResultsDir" | wc -l`"
echo -e "$YELLOW_F$D00:$NORM $GREEN_F$MESG$NORM"

#  Say Goodbye
END_TIME=`date +%s`
ELAPSED=`expr $END_TIME - $StartTime`
remainder="$(expr $ELAPSED % 3600)"
hours="$(expr $(expr $ELAPSED - $remainder) / 3600)"
seconds="$(expr $remainder % 60)"
minutes="$(expr $(expr $remainder - $seconds) / 60)"
CurrDateTime=$(date +"%y-%m-%d %H:%M:%S")
echo -e "$RED_F$CurrDateTime:$GREEN_F$BOLD Finished$NORM \"$MyName\"  Elapsed time: `printf %02d $hours`:`printf %02d $minutes`:`printf %02d $seconds`"


