-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_associated_files
executable file
·53 lines (45 loc) · 1.72 KB
/
collect_associated_files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /usr/bin/env bash
set -eu
shopt -s nocasematch
declare -r BASE_DIR="$(dirname "$(readlink -e "$0")")"
source "$BASE_DIR/lib/photofiles.sh"
PATH="$BASE_DIR/lib:$PATH"
while getopts "i" opt; do
case $opt in
i )
declare -r SUPPRESS_ORIGINAL=
;;
esac
done
shift $(expr $OPTIND - 1 )
if (( $# == 0 )); then
declare -r READ_PHOTOS_FROM_STDIN=
else
declare -r PHOTOS=$@
fi
find_associated_files_by_fullname() {
local -r photo=$1
local -r photo_filename=$(basename "$photo")
local -r sourcephoto_fullname=${photo_filename%%.*}
# Find all files, i.e. actual photo file and all associated files: search for the basename without
# extensions in the directory of $photo and below.
find $(dirname "$photo") -type f -path "*/${sourcephoto_fullname}*" ${SUPPRESS_ORIGINAL+-not -name "$photo_filename"}
}
# About the "sort -u" as the last pipeline step: if we have both a raw and a jpg version of a photo (i.e. there
# are two original files), we will find the jpg file as an associated file or the raw file, respectively.
# this leads to problems when changing title or album (because commands will try to change things twice, failing
# the 2nd time because the file was changed already because it was collected twice). But using "sort -u" will
# work around that quite nicely.
if [[ -v READ_PHOTOS_FROM_STDIN ]]; then
while read -r photo; do
if is_original_photofile "$photo"; then
find_associated_files_by_fullname "$(readlink -e "$photo")"
fi
done < /dev/stdin | sort -u
else
for photo in $PHOTOS; do
if is_original_photofile "$photo"; then
find_associated_files_by_fullname "$(readlink -e "$photo")"
fi
done | sort -u
fi