#!/bin/bash

# ==============================================================================
# Script: Exiftool IPTC Tag Updater
# Author: Theodore Zacharia
# V0.1: 18/08/2025
# V0.2: 28/08/2025
#
# Description:
# This script uses the 'exiftool' command-line utility to update a
# specific set of IPTC metadata tags in one or more JPG files. It supports
# wildcards for handling multiple files.
# It uses 'getopts' to accept tag values as command-line options.
#
# Usage:
#   ./setpictags.sh [options] FILENAME [FILENAME2 ...]
#   ./setpictags.sh [options] *.jpg
#
# Options:
#   -b <string>  ByLine
#   -B <string>  ByLineTitle
#   -c <string>  CopyrightNotice
#   -r <string>  Credit
#   -S <string>  Source
#   -l <string>  ContentLocationCode
#   -L <string>  ContentLocationName
#   -E <string>  ReleaseDate
#   -I <string>  SpecialInstructions
#   -F <string>  FixtureIdentifier
#   -H <string>  Headline
#   -a <string>  Caption-Abstract
#   -C <string>  Country-PrimaryLocationName
#   -P <string>  Province-State
#   -O <string>  Sub-location
#   -T <string>  City
#   -k <string>  Keywords
#   -s <string>  SupplementalCategory
#   -h           Show this help message and exit
#
# Requires: 'exiftool' to be installed.
#
# ==============================================================================

# --- Function to display usage information ---
usage() {
  echo "Usage: $(basename "$0") [options] FILENAME [FILENAME2 ...]"
  echo ""
  echo "Options:"
  echo "  -b <string>  ByLine"
  echo "  -B <string>  ByLineTitle"
  echo "  -c <string>  CopyrightNotice"
  echo "  -r <string>  Credit"
  echo "  -S <string>  Source"
  echo "  -l <string>  ContentLocationCode"
  echo "  -L <string>  ContentLocationName"
  echo "  -E <string>  ReleaseDate"
  echo "  -I <string>  SpecialInstructions"
  echo "  -F <string>  FixtureIdentifier"
  echo "  -H <string>  Headline"
  echo "  -a <string>  Caption-Abstract"
  echo "  -C <string>  Country-PrimaryLocationName"
  echo "  -P <string>  Province-State"
  echo "  -O <string>  Sub-location"
  echo "  -T <string>  City"
  echo "  -k <string>  Keywords"
  echo "  -s <string>  SupplementalCategory"
  echo "  -h           Show this help message and exit"
}

# --- Initialize variables for tags ---
BYLINE=""
BYLINE_TITLE=""
COPYRIGHT_NOTICE=""
CREDIT=""
SOURCE=""
CONTENT_LOCATION_CODE=""
CONTENT_LOCATION_NAME=""
RELEASE_DATE=""
SPECIAL_INSTRUCTIONS=""
FIXTURE_IDENTIFIER=""
HEADLINE=""
CAPTION_ABSTRACT=""
COUNTRY_NAME=""
PROVINCE_STATE=""
SUB_LOCATION=""
CITY=""
KEYWORDS=""
SUPPLEMENTAL_CATEGORY=""

# --- Parse command-line options using getopts ---
while getopts ":b:B:c:r:S:l:L:E:I:F:H:a:C:P:O:T:k:s:h" opt; do
  case $opt in
    b) BYLINE="$OPTARG";;
    B) BYLINE_TITLE="$OPTARG";;
    c) COPYRIGHT_NOTICE="$OPTARG";;
    r) CREDIT="$OPTARG";;
    S) SOURCE="$OPTARG";;
    l) CONTENT_LOCATION_CODE="$OPTARG";;
    L) CONTENT_LOCATION_NAME="$OPTARG";;
    E) RELEASE_DATE="$OPTARG";;
    I) SPECIAL_INSTRUCTIONS="$OPTARG";;
    F) FIXTURE_IDENTIFIER="$OPTARG";;
    H) HEADLINE="$OPTARG";;
    a) CAPTION_ABSTRACT="$OPTARG";;
    C) COUNTRY_NAME="$OPTARG";;
    P) PROVINCE_STATE="$OPTARG";;
    O) SUB_LOCATION="$OPTARG";;
    T) CITY="$OPTARG";;
    k) KEYWORDS="$OPTARG";;
    s) SUPPLEMENTAL_CATEGORY="$OPTARG";;
    h) usage; exit 0;;
    \?) echo "Invalid option: -$OPTARG" >&2; usage; exit 1;;
    :) echo "Option -$OPTARG requires an argument." >&2; usage; exit 1;;
  esac
done

# --- Shift positional parameters so $@ refers to the list of non-option arguments ---
shift $((OPTIND-1))

# --- Check for at least one filename ---
if [ $# -eq 0 ]; then
  echo "Error: You must provide at least one filename." >&2
  usage
  exit 1
fi

# --- Construct the base exiftool command with provided values ---
# We use -P to preserve the original file timestamp.
# We use -overwrite_original to avoid creating a new file with _original.
EXIFTOOL_CMD="exiftool -P -overwrite_original"

if [ -n "$BYLINE" ]; then EXIFTOOL_CMD+=" -IPTC:By-Line='$BYLINE'"; fi
if [ -n "$BYLINE_TITLE" ]; then EXIFTOOL_CMD+=" -IPTC:By-LineTitle='$BYLINE_TITLE'"; fi
if [ -n "$COPYRIGHT_NOTICE" ]; then EXIFTOOL_CMD+=" -IPTC:CopyrightNotice='$COPYRIGHT_NOTICE'"; fi
if [ -n "$CREDIT" ]; then EXIFTOOL_CMD+=" -IPTC:Credit='$CREDIT'"; fi
if [ -n "$SOURCE" ]; then EXIFTOOL_CMD+=" -IPTC:Source='$SOURCE'"; fi
if [ -n "$CONTENT_LOCATION_CODE" ]; then EXIFTOOL_CMD+=" -IPTC:ContentLocationCode='$CONTENT_LOCATION_CODE'"; fi
if [ -n "$CONTENT_LOCATION_NAME" ]; then EXIFTOOL_CMD+=" -IPTC:ContentLocationName='$CONTENT_LOCATION_NAME'"; fi
if [ -n "$RELEASE_DATE" ]; then EXIFTOOL_CMD+=" -IPTC:ReleaseDate='$RELEASE_DATE'"; fi
if [ -n "$SPECIAL_INSTRUCTIONS" ]; then EXIFTOOL_CMD+=" -IPTC:SpecialInstructions='$SPECIAL_INSTRUCTIONS'"; fi
if [ -n "$FIXTURE_IDENTIFIER" ]; then EXIFTOOL_CMD+=" -IPTC:FixtureIdentifier='$FIXTURE_IDENTIFIER'"; fi
if [ -n "$HEADLINE" ]; then EXIFTOOL_CMD+=" -IPTC:Headline='$HEADLINE'"; fi
if [ -n "$CAPTION_ABSTRACT" ]; then EXIFTOOL_CMD+=" -IPTC:Caption-Abstract='$CAPTION_ABSTRACT'"; fi
if [ -n "$COUNTRY_NAME" ]; then EXIFTOOL_CMD+=" -IPTC:Country-PrimaryLocationName='$COUNTRY_NAME'"; fi
if [ -n "$PROVINCE_STATE" ]; then EXIFTOOL_CMD+=" -IPTC:Province-State='$PROVINCE_STATE'"; fi
if [ -n "$SUB_LOCATION" ]; then EXIFTOOL_CMD+=" -IPTC:Sub-location='$SUB_LOCATION'"; fi
if [ -n "$CITY" ]; then EXIFTOOL_CMD+=" -IPTC:City='$CITY'"; fi
if [ -n "$KEYWORDS" ]; then EXIFTOOL_CMD+=" -IPTC:Keywords='$KEYWORDS'"; fi
if [ -n "$SUPPLEMENTAL_CATEGORY" ]; then EXIFTOOL_CMD+=" -IPTC:SupplementalCategory='$SUPPLEMENTAL_CATEGORY'"; fi

# --- Loop through each file provided as an argument ---
for filename in "$@"; do
  # Check if the file exists before processing
  if [ ! -f "$filename" ]; then
    echo "Warning: File not found at '$filename', skipping." >&2
    continue
  fi

  # --- Final command and execution for the current file ---
  echo ""
  echo "Updating metadata for '$filename'..."
  # The constructed command is now run for each file
  eval "$EXIFTOOL_CMD \"$filename\""

  # --- Report result for the current file ---
  if [ $? -eq 0 ]; then
    echo "Success: IPTC tags updated successfully for '$filename'."
  else
    echo "Error: An error occurred during the update for '$filename'."
  fi
done