A modules/shell-utils/bin/img-autoname => modules/shell-utils/bin/img-autoname +64 -0
@@ 0,0 1,64 @@
+#!/bin/sh
+
+# This script renames images automatically based on their metadata. If two
+# images would have the same name, then it will skip renaming one of them to
+# avoid data loss.
+
+set -e
+set -u
+
+if [ $# -eq 0 ] ; then
+ echo "usage: img-autoname IMG1 IMG2 ..." 1>&2
+ exit 1
+fi
+
+for dep in exiftool rq ; do
+ if [ ! -x "$(which "$dep")" ] ; then
+ echo "FATAL: '$dep' not in PATH" 1>&2
+ exit 1
+ fi
+done
+
+while true ; do
+ if [ $# -eq 0 ] ; then
+ break
+ fi
+
+ f="$1"
+ shift
+
+ if [ ! -e "$f" ] ; then
+ echo "WARNING: skipping '$f' because it does not exist" 1>&2
+ continue
+ fi
+
+ parent="$(dirname "$f")"
+ base="$(basename "$f")"
+
+ created="$(exiftool -j "$f" -d "%Y-%m-%d" | rq -R 'input[0].DateTimeOriginal')"
+ if [ -z "$created" ] ; then
+ created="$(exiftool -j "$f" -d "%Y-%m-%d" | rq -R 'input[0].CreationDate')"
+ fi
+
+ if [ -z "$created" ] ; then
+ created="$(exiftool -j "$f" -d "%Y-%m-%d" | rq -R 'input[0].CreateDate')"
+ fi
+
+ if [ -z "$created" ] ; then
+ created="$(date '+%Y-%m-%d')"
+ fi
+
+
+ seq="$(echo "$base" | egrep -o '[0-9][0-9]+')"
+ ext="$(echo "$base" | tr '.' '\n' | tail -n 1)"
+ new="$parent/$created""_$seq.$ext"
+
+ echo "'$f' -> '$new'" 1>&2
+
+ if [ -e "$new" ] ; then
+ echo "WARNING: skipping file '$f' because it's new name '$new' already exists" 1>&2
+ continue
+ fi
+
+ mv "$f" "$new"
+done