#!/bin/bash
# DTstamp1 - use Image Magick Convert to add date/time stamp YELLOW on .jpg photos
#          - batch script, date-time-stamps all photos while copying from dir1 to dir2
#          - by Owen Townsend, UV Software, Jan18/2022
#*DTstamp1 - processes dir1/1file to dir2/1file
# DTstamp2 - processes dir1/ALL*FILEs to dir2/ALL*FILEs 
# DTstamp3 - processes dir1/ALL*SUBDIRs/all*files to dir2/ALL*SUBDIRs/ALL*FILEs 
# DTstamp4 - calls DTstamp3 to annotate .jpg photos with filenames & Date/Time stamps
#            for all superdirs of photos by year (2002 - 2022)
#
#Feb12/2022 - was annotating both filename & DateTime, change to:
# - annotate DateTime only if different than Date_time prefix inserted by renameJPG2
#
f1="$1"; f2="$2";
if [[ -f "$f1" && -n "$f2" && "$f1" != "$f2" ]]; then :
   else echo "usage: DTstamp1 infile outfile"
        echo "       ======================="
        echo " - arg1 input filename must be a .jpg file"
        echo " - arg2 output filename must be specified & different dir or file"
        echo " "
        echo "Example: DTstamp1 2002/20020809/dscf0001.jpg tmp/dscf0001.jpg"
        echo "         ===================================================="
        exit 99; fi
#
# annotate DateTime only if different than Date_time prefix inserted by renameJPG2
# - get EXIF DateTime using IM identify
# - convert format to match filename prefix, "YYYY:MM:DD HH:MM:SS" to "YYYYMMDD_HHMMSS"
# - if match: annotate only the filename, if nonmatch: annotate both DateTime & filename
#
exifdt1=$(identify -format "%[EXIF:DateTime]" "$f1")
exifdt2=$(echo "$exifdt1" | tr " " "_" | tr -d ":")
f1dt=$(basename $f1)
f2dt=${f1dt%_*}  # drop right side back to '_'
#                # Converts: "yyyymmdd_hhmmss_dscf001.jpg" to "yyyymmdd_hhmmss"
if [[ "$f2dt" == "$exifdt2" ]]; then
   convert $f1 -font Arial -pointsize 40 -fill yellow\
            -gravity SouthWest -annotate +40+40 '%f' $f2
else   
   convert $f1 -font Arial -pointsize 40 -fill yellow\
            -gravity SouthWest -annotate +40+100 %[exif:DateTimeOriginal]\
            -gravity SouthWest -annotate +40+040 '%f' $f2
fi
#===========================================================================
echo "$f1 date-time stamped while copying to $f2"
exit 0
#
