#!/bin/bash
# DTstamp3 - use Imag Magick Convert to add date/time stamp YELLOW on .jpg photos
#          - batch script to annotate .jpg photos with filenames & Date/Time stamps
#            while copying from dir1/*/* to dir2
#          - uses ImageMagic convert annotate for filename & EXIF:DateTime 
#          - by Owen Townsend, UV Software, Jan19/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)
#
d1="$1"; d2="$2";
if [[ -d "$d1" && -n "$d2" && "$d1" != "$d2" ]]; then :
   else echo "usage: DTstamp3 indir outdir"
        echo "       ====================="
        echo "       - arg1 (input superdir) must be a directory & not same as output superdir"
        echo "       - arg2 (output superdir) must be specified, will make if not present"
        echo "       - but if output superdir present, it must be empty"
        echo "       - will create subdirs in outdir named same as subdirs in indir"
        echo " "
        echo "Example: DTstamp3 2002 2002DTS"
        echo "         ====================="
        echo " - process all files from ALL subdirs in 2002/.../... to 2002DTS/.../..."
        exit 91; fi
#
# if outdir not present, will make by appending 'DTS' on indir
# OK if present & empty, but ERROR if present & not empty
if   [[ ! -d "$d2" ]]; then mkdir ${d1}DTS;
elif [[ "$(ls -A $d2)" ]]; then echo "ERROR - outdir must be empty, exiting"; exit 92; fi;
tsd=0; tsdj=0; tsdx=0; tcvs=0;
#
#*eject
for d1s1 in $d1/*
  { ((tsd+=1));                # count total subdirs processed dir1 to diir2
    tsd1j=0; tsd1x=0; tcvs1=0; # clear acums jpgs & non-jpgs in current subdir
    s1=$(basename $d1s1)
    mkdir $d2/$s1              # make output subdir within dir2
    for d1s1f1 in $d1/$s1/*
      { f1=$(basename $d1s1f1)
        ## echo "DEBUG1: f1=$f1 in $d1/$s1/$f1"
        # convert/annotate only .jpg (or .JPG), else just copy
        if [[ ("$d1/$s1/$f1" == *.jpg) || ("$d1/$s1/$f1" == *.JPG) ]] ; then 
           convert $d1/$s1/$f1  -font Arial -pointsize 48 -fill yellow\
           -gravity SouthWest -annotate +40+40 '%f'\
           -gravity SouthEast -annotate +40+40 %[exif:DateTimeOriginal] $d2/$s1/$f1
           cvstat=$?                     # capture status returned by convert
           if ((cvstat)); then 
              ((tcvs1=+1)); ((tcvs+=1)); # count errorsin current & all subdirs
              echo "convert annotate ERROR# $tcvs for $d1/$s1/$f1";
           fi
           ((tsd1j+=1)); ((tsdj+=1));    # count .jpg's in current & all subdirs
        else
          cp $d1/$s1/$f1 $d2/$s1/        # copy non .jpg's unchanged
          ((tsd1x+=1)); ((tsdx+=1));     # count non-jpg files in current & all subdirs
        fi
      }
    echo "$tsd1j .jpg's date-time stamped from $d1/$s1 to $d2/$s1"
    if ((tsd1x)); then echo "$tsd1x non-jpgs copied unchanged from $d1/$s1/..."; fi
    if ((tcvs1)); then echo "$tcvs1 convert annotate ERRORs in $d1/$s1/..."; fi
  }
echo "$tsd subdirs processed from $d1/... to $d2/... "
echo "$tsdj .jpg's date-time stamped from $d1/... to $d2/... "
if ((tsdx)); then echo "$tsdx TOTAL non-jpgs copied unchanged to $d2/..."; fi
if ((tcvs)); then echo "$tcvs convert annotate TOTAL ERRORs in $d1/..."; fi
exit 0
