#!/bin/bash
# DTstamp2 - use Imag 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
#
d1="$1"; d2="$2";
if [[ -d "$d1" && -d "$d2" ]]; then :
   else echo "usage: DTstamp2 indir outdir"
        echo "       ====================="
        echo " - arg1 & arg2 must be directories"
        echo " "
        echo "Example: DTstamp2 20020809 20020809DTS"
        echo "         ============================="
        echo " - process all files from indir 20020809/... to outdir 20020809DTS/..."
        exit 99; fi
#
countj=0;
countx=0;
cnvstat=0;
#
#*eject
for d1f1 in $d1/*
  { f1=$(basename $d1f1)
    # convert/annotate only .jpg (or .JPG), else just copy
    if [[ ("$f1" == *.jpg) || ("$f1" == *.JPG) ]] ; then 
       # 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]" "$d1/$f1")
       exifdt2=$(echo "$exifdt1" | tr " " "_" | tr -d ":")
       f1dt=$(basename $d1/$f1)
       f2dt=${f1dt%_*}  # drop right side back to '_'
       #                # Converts: "yyyymmdd_hhmmss_dscf001.jpg" to "yyyymmdd_hhmmss"
       echo "DEBUG1: d1/f1=$d1/$f1, d2/f1=$d2/$f1"
       # echo "DEBUG2: f2dt=-->$f2dt<--, exifdt2=-->$exifdt2<--"
       if [[ "$f2dt" != "$exifdt2" ]]; then
          convert $d1/$f1 -font Arial -pointsize 40 -fill yellow\
                   -gravity SouthWest -annotate +40+130 %[exif:DateTimeOriginal]\
                   -gravity SouthWest -annotate +40+080 '%f' $d2/$f1
       else   
          convert $d1/$f1 -font Arial -pointsize 40 -fill yellow\
                   -gravity SouthWest -annotate +40+080 '%f' $d2/$f1
       fi
       cvstat=$?                         # capture status returned by convert
       if ((cvstat)); then ((tcvs=+1));  # count errorsin current & all subdirs
          echo "convert annotate ERROR# $tcvs for $d1/$s1/$f1";
       fi
       ((countj+=1))       # count .jpg's annotated
    else
      cp $d1/$f1 $d2/$f1  # copy non .jpg's copied unchanged
      ((countx+=1))       # count others (not .jpg's)
    fi
  }
echo "$countj .jpg's date-time stamped from $d1 to $d2"
if ((countx)); then echo "$countx other images copied unchanged"; fi
if ((tcvs)); then echo "$tcvs convert annotate TOTAL ERRORs in $d1/..."; fi
exit 0
#
