#!/bin/sh

find . -type f \
    -maxdepth 1 \
    \( -iname "*.cr2" -o -iname "*.orf" -o -iname "*.dng" \) \
    -exec basename "{}" \; \
    | while read raw
do
    jpg="${raw%.*}.jpg"
    thm="${raw%.*}.thumb.jpg"

    if [ ! -f "${jpg}" ]
    then
        printf 'Extracting Preview: %s\n' "${raw}"
        dcraw -e "${raw}"
        mv "${thm}" "${jpg}"
    else
        printf 'Skipping (existing): %s\n' "${jpg}"
    fi
done

find . -type f \
    -maxdepth 1 \
    -iname "*.tif" \
    -exec basename "{}" \; \
    | while read raw
do
    jpg="${raw%.*}.jpg"
    thm="${raw%.*}.thumb.jpg"

    if [ ! -f "${jpg}" ]
    then
        printf 'Extracting Preview: %s\n' "${raw}"
        convert -scale 1600x1600 "${raw}" "${thm}"
        mv "${thm}" "${jpg}"
    else
        printf 'Skipping (existing): %s\n' "${jpg}"
    fi
done
