#!/usr/bin/env bash
# Desc: Apply metadata to photos with exiftool
# Usage: bkphotorights [FILE in]
# Ref/Attrib: [1]. Creative Commons XMP recommendation. (2015). https://wiki.creativecommons.org/wiki/XMP
#             [2]. Eva, Johannes. (2023-03-30). “How to edit EXIF metadata via the command line with ExifTool”. libre-software.net. https://libre-software.net/linux/edit-metadata-exiftool/
#             [2]. “XMP Tags”. (2023-06-08). exiftool.org. https://exiftool.org/TagNames/XMP.html
# Version: 0.2.0
# Depends: * exiftool 12.16 (https://exiftool.org/ )
#          * bash 5.1.16
# Example: bkphotorights DSC00035.JPG
# Note: Change strCreator, strCreatorURL, strTerms, and strLicenseURL
#   values that should be added as metadata to the specified photo.

# Adjust me
strCreator="Steven Baltakatei Sandoval";
strCreatorURL="https://baltakatei.com";
strTerms="This work is licensed to the public under the Creative Commons Attribution-ShareAlike license http://creativecommons.org/licenses/by-sa/4.0/ .";
strLicenseURL="http://creativecommons.org/licenses/by-sa/4.0/";

yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
main() {
    declare -a cmd_args;
    file_in="$1";

    # Check plumbing
    if [[ ! -f "$1" ]]; then die "FATAL:Not a file:$1"; fi;
    if ! command -v exiftool 1>/dev/random 2>&1; then
        die "FATAL:exiftool not available. See https://exiftool.org/ ."; fi;

    # Check file
    # Check if exiftool recognizes the file format
    if ! exiftool -s -FileType "$file_in" 1>/dev/random; then
        yell "ERROR:File type not recognized by exiftool:$file_in";
    fi;
    
    # Change file metadata
    unset cmd_args;
    cmd_args+=("exiftool");
    # cmd_args+=("-overwrite_original"); # hashtag reckless

    value="$strTerms";
    cmd_args+=("-XMP-dc:Rights=""$value"); unset value;

    value="True";
    cmd_args+=("-XMP-xmpRights:Marked=""$value"); unset value;

    value="$strTerms";
    cmd_args+=("-XMP-xmpRights:UsageTerms=""$value"); unset value;

    value="$strLicenseURL";
    cmd_args+=("-XMP-cc:license=""$value"); unset value;

    value="$strCreator";
    cmd_args+=("-XMP-cc:AttributionName=""$value"); unset value;

    value="$strCreatorURL";
    cmd_args+=("-XMP-cc:AttributionURL=""$value"); unset value;

    cmd_args+=("$file_in");

    # Execute command
    must "${cmd_args[@]}";
}; # main program

main "$@";

# Author: Steven Baltakatei Sandoval
# License: GPLv3+