-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_title
executable file
·100 lines (76 loc) · 2.42 KB
/
set_title
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
89
90
91
92
93
94
95
96
97
98
99
100
#! /usr/bin/env bash
set -eu
shopt -s nocasematch
declare -r BASE_DIR="$(dirname "$(readlink -e "$0")")"
source "$BASE_DIR/lib/metadata.sh"
source "$BASE_DIR/lib/sidecar.sh"
source "$BASE_DIR/lib/jpeg.sh"
PATH="$BASE_DIR/lib:$PATH"
declare -r TITLE=$(echo "${1:-}" | tr ' ' '-')
if [[ -z $TITLE ]]; then
echo "[ERROR] Title parameter missing" >&2
exit 1
elif [[ -f $TITLE || -d $TITLE ]]; then
echo "[ERROR] Title parameter is a file or directory" >&2
exit 1
fi
shift 1
if [[ ${1:-} = "-" ]]; then
declare -r READ_PHOTOS_FROM_STDIN=
else
declare -r PHOTOS=$@
fi
new_filename() {
local -r filename=$1
local -r newtitle=$2
echo "${newtitle}_${filename#*_}"
}
old_title() {
local file=$1
local basename=$(basename "$1")
echo ${basename%%_*}
}
is_rawtherapee_sidecar() {
local -r file=$1
[[ $file =~ .*\.pp[23]$ ]]
}
is_converted_jpeg() {
local -r file=$1
[[ $file =~ .*/converted/.*\.jpg$ ]]
}
apply_set_title() {
local -r file=$1
local -r filename=${file##*/}
if [[ $(old_title "$file") = $TITLE ]]; then
echo "[WARN] Cannot change title of $file to \"$TITLE\", same as old title" >&2
return
fi
if ! [[ -w $file ]]; then
chmod u+w "$file"
local -r restore_write_protection=
fi
local -r newfile="$(dirname "$file")/$(new_filename "$filename" "$TITLE")"
# this is a real error case unlike the other check above (which stems from careless user input)
if [[ -e $newfile ]]; then
echo "[ERROR] Cannot change title of $file to \"$TITLE\", file $newfile exists already" >&2
exit 1
fi
if is_rawtherapee_sidecar "$file"; then
sidecar_set_property "$file" "IPTC" "Headline" "$(headline_from_photofile "$newfile")"
sidecar_set_property "$file" "IPTC" "Caption" "[$(fullname_from_photofile "$newfile")]"
elif is_converted_jpeg "$file"; then
jpeg_set_iptc "$file" "Headline" "$(headline_from_photofile "$newfile")"
jpeg_set_iptc "$file" "Caption" "[$(fullname_from_photofile "$newfile")]"
fi
mv "$file" "$newfile"
if [[ -v restore_write_protection ]]; then
chmod u-w "$newfile"
fi
echo "$newfile"
}
set -o pipefail
if [[ -v READ_PHOTOS_FROM_STDIN ]]; then
collect_associated_files.sh < /dev/stdin | while read -r file; do apply_set_title "$file"; done
else
collect_associated_files.sh "$PHOTOS" | while read -r file; do apply_set_title "$file"; done
fi