#!/bin/sh

USAGE='Usage: brewid [-n AUTHOR_NAME] image-files

  Writes author and copyright information as EXIF-metatag.

  Depends on exiftool.

  Copyright © Daniel Ljunggren <foto@demulsion.se>. April 2009.

'

AUTHOR_NAME=""

while [ $# -gt 0 ] 
do
    case "$1" in
	-n)
	    if [[ -n $2 ]]; then
		AUTHOR_NAME="$2"
	    else
		echo "$USAGE" 1>&2
		exit 2
	    fi
	    ;;
	-*)	
	    echo "$USAGE" 1>&2
	    exit 2
	    ;;
	*)
            break
	    ;;
    esac
    shift 2
done

if [ -z "${AUTHOR_NAME}" ]; then
    echo "$USAGE" 1>&2
    exit 2
fi

for FILE 
do
  if [ -w "$FILE" ]; then


      #[ -z "${NAME}" ] && NAME=`exiftool -Copyright $FILE | cut -b35-`
      #[ -z "${NAME}" ] && NAME=`exiftool -OwnerName $FILE | cut -b35-`



      COPY_NAME=`exiftool -Copyright $FILE | cut -b35-`



      if [ -z "${COPY_NAME}" ]; then
	  exiftool "-Copyright=${AUTHOR_NAME}" \
	      "-Artist=${AUTHOR_NAME}" \
	      "-OwnerName=${AUTHOR_NAME}" \
	      "-XMP:Author=${AUTHOR_NAME}" \
	      "-XMP:Copyright=${AUTHOR_NAME}" \
	      -overwrite_original $FILE 
      else
	  echo brewid: "${FILE} has -Copyright already set to \"${COPY_NAME}\", leaving..."

	  OWNER_NAME=`exiftool -OwnerName $FILE | cut -b35-`
	  if [ -z "${OWNER_NAME}" ]; then
	      exiftool "-OwnerName=${AUTHOR_NAME}" \
		  "-Artist=${AUTHOR_NAME}" \
		  "-XMP:Author=${AUTHOR_NAME}" \
		  -overwrite_original $FILE 
	  else
	      echo brewid: "${FILE} has -OwnerName already set to \"${OWNER_NAME}\", leaving..."
	  fi
      fi
 fi
done
