#!/usr/bin/env bash

# This Bash4 script generates lens ID lists for rtexif/*.cc files using
# the exiftool version installed on the host system, so make sure you have
# the latest version of exiftool installed before running this script.
# It uses xmlstarlet to parse exiftool's output so make sure you have that.
#
# Run the script from the project root:
#   ./tools/generateLensList
#
# Manually replace old code in rtexif/* with new from /tmp/rt-generateLensList/*
#
# Blame DrSlony
# Please report bugs or enhancements to https://github.com/Beep6581/RawTherapee

hash exiftool 2>/dev/null || { echo >&2 "Exiftool not found, install it first."; exit 1; }
hash xmlstarlet 2>/dev/null || { echo >&2 "XMLStarlet not found, install it first."; exit 1; }

unset cam cams

cams=("canon" "nikon" "olympus" "pentax" "sony")
tmpdir="/tmp/rt-generateLensList"

head -n 15 "$0" | tail -n 14
printf '%s\n' "exiftool version: $(exiftool -ver)" "" "XMLStarlet version: $(xmlstarlet --version)"  | sed 's/^/# /'

if [[ -d ${tmpdir} ]]; then
    printf '%s\n' "" "Removing temp folder: $tmpdir"
    rm -rvI "$tmpdir" || exit 1
fi
mkdir -p "$tmpdir" || { printf '%s\n' "Error creating $tmpdir" ""; exit 1; }
echo

for cam in "${cams[@]}"; do
    if [[ "$cam" != nikon ]]; then
        printf '%s\n' "Saving ${tmpdir}/${cam}"
        xmlstarlet sel -T -t -m "taginfo/table/tag[@name='LensType']/values/key" -v "concat(@id,'	',val)" -n < <(exiftool -listx -"$cam":all) > "${tmpdir}/cam" || { printf '%s\n' "Saving failed: ${tmpdir}/cam"; exit 1; }
        sort -fuV "${tmpdir}/cam" > "${tmpdir}/${cam}"
        rm -f "${tmpdir}/cam"
    fi
    case $cam in
        canon) sed -r -i -e '/-1\tn\/a/d' -e 's/([0-9]+)[0-9.]*\t/\1, "/' -e 's/^/        choices.insert(p_t(/' -e 's/$/"));/' -e 's| F/([0-9]+)| f/\1|' "${tmpdir}/canon" ;;
        nikon)
            # Nikon LensIDs are composite tags
            printf '%s\n' "Saving ${tmpdir}/nikon"
            xmlstarlet sel -T -t -m "taginfo/table/tag[@name='LensID']/values/key" -v "concat(@id,'	',val)" -n < <(exiftool -listx -composite:all) > "${tmpdir}/nikon" || { printf '%s\n' "Saving failed: ${tmpdir}/nikon"; exit 1; }
            sed -r -i -e '/^...	/d' -e 's/^/        lenses["/' -e 's/([A-F0-9]+)[A-F0-9.]*\t/\1"] = "/' -e 's/$/";/' -e 's|(.* ")(.*) F([0-9]+)|\1\2 f/\3|' -e 's| F/([0-9]+)| f/\1|' "${tmpdir}/nikon"
            ;;
        olympus) sed -r -i -e '/0 00 00\tNone/d' -e 's/^/        lenses["0/' -e 's/\t/"] = "/' -e 's/$/";/' -e 's| F([0-9]+)| f/\1|g' "${tmpdir}/olympus" ;;
        pentax) sed -r -i -e 's/^/        choices.insert(p_t(256 * /' -e 's/([0-9]+) ([0-9]+)([0-9.]*)/\1 + \2/' -e 's/\t/, "/' -e 's/$/"));/' -e 's| F([0-9]+)| f/\1|' "${tmpdir}/pentax" ;;
        sony)
            # Sony has more lenses under the LensType2 tag
            printf '%s\n' "Saving ${tmpdir}/sony-lenstype2"
            xmlstarlet sel -T -t -m "taginfo/table/tag[@name='LensType2']/values/key" -v "concat(@id,'	',val)" -n < <(exiftool -listx -sony:all) > "${tmpdir}/cam" || { printf '%s\n' "Saving failed: ${tmpdir}/cam"; exit 1; }
            sort -fuV "${tmpdir}/cam" > "${tmpdir}/sony-lenstype2"
            rm -f "${tmpdir}/cam"
            sed -r -i -e '/255\tTamron Lens (255)/d' -e 's/([0-9]+)[0-9.]*\t/\1, "/' -e 's/^/        choices.insert(p_t(/' -e 's/$/"));/' -e 's| F([0-9]+)| f/\1|g' "${tmpdir}/sony" "${tmpdir}/sony-lenstype2"
            ;;
    esac

done

