-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_keywords
executable file
·88 lines (68 loc) · 1.83 KB
/
add_keywords
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /usr/bin/env bash
set -eu
shopt -s nocasematch
declare -r BASE_DIR="$(dirname "$(readlink -e "$0")")"
source "$BASE_DIR/lib/sidecar.sh"
source "$BASE_DIR/lib/jpeg.sh"
PATH="$BASE_DIR/lib:$PATH"
declare -r KEYWORDS=$1
if [[ -z $KEYWORDS ]]; then
echo "[ERROR] No keywords" >&2
exit 1
elif [[ -f $KEYWORDS || -d $KEYWORDS ]]; then
echo "[ERROR] Keywords parameter seems to be a file or directory - actual keywords parameter is missing?" >&2
exit 1
fi
shift 1
if [[ ${1:-} = "-" ]]; then
declare -r READ_PHOTOS_FROM_STDIN=
else
declare -r PHOTOS=$@
fi
is_rawtherapee_sidecar() {
local -r file=$1
[[ $file =~ .*\.pp[23]$ ]]
}
is_converted_jpeg() {
local -r file=$1
[[ $file =~ .*/converted/.*\.jpg$ ]]
}
process_rawtherapee_sidecar() {
local -r sidecar=$1
if ! [[ -w $sidecar ]]; then
chmod u+w "$sidecar"
local -r restore_write_protection=
fi
if ! grep -q "^\[IPTC\]$" "$sidecar"; then
echo -e "\n[IPTC]\nKeywords=;\n" >> $sidecar
fi
sidecar_add_iptc_keywords "$sidecar" "$KEYWORDS"
if [[ -v restore_write_protection ]]; then
chmod u-w "$sidecar"
fi
}
process_converted_jpeg() {
local -r jpgfile=$1
if ! [[ -w $jpgfile ]]; then
chmod u+w "$jpgfile"
local -r restore_write_protection=
fi
jpeg_set_iptc_keywords "$KEYWORDS" "$jpgfile"
if [[ -v restore_write_protection ]]; then
chmod u-w "$jpgfile"
fi
}
apply_add_keywords() {
local -r file=$1
if is_rawtherapee_sidecar "$file"; then
process_rawtherapee_sidecar "$file" && echo "$file"
elif is_converted_jpeg "$file"; then
process_converted_jpeg "$file" && echo "$file"
fi
}
set -o pipefail
if [[ -v READ_PHOTOS_FROM_STDIN ]]; then
collect_associated_files.sh < /dev/stdin | while read -r file; do apply_add_keywords "$file"; done
else
collect_associated_files.sh "$PHOTOS" | while read -r file; do apply_add_keywords "$file"; done
fi