-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_photo_title
executable file
·67 lines (53 loc) · 1.71 KB
/
change_photo_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
#! /usr/bin/env bash
set -eu
set -o pipefail
declare -r BASEDIR="$(dirname "$(readlink -e "$0")")"
source "$BASEDIR/lib/photofiles.sh"
source "$BASEDIR/lib/photoid.sh"
source "$BASEDIR/util/unprotect.sh"
PATH="$BASEDIR/metadata:$BASEDIR:$BASEDIR/lib:$PATH"
while getopts "kr" opt; do
case $opt in
k )
ADD_TITLE_KEYWORD=;;
r )
REMOVE_OLD_TITLE_KEYWORD=;;
esac
done
shift $(expr $OPTIND - 1 )
declare -r new_title=$(echo "${1:-}" | tr ' ' '-')
if [[ -z $new_title ]]; then
echo "[ERROR] Title parameter missing" >&2
exit 1
elif [[ -f $new_title || -d $new_title ]]; then
echo "[ERROR] Title parameter is a file or directory" >&2
exit 1
fi
shift 1
function apply_set_title() {
local old_file=$1
local old_photoid=$(photoid_get_from_file "$old_file")
local old_title=$(photoid_get_title "$old_photoid")
local new_photoid=$(photoid_set_title "$old_photoid" "$new_title")
local newfile="$(dirname "$old_file")/$new_photoid.${old_file#*.}"
if [[ -e $newfile ]]; then
echo "[ERROR] Rename $file to \"$new_title\" failed: $newfile exists" >&2
exit 1
fi
unprotect_file_if_needed "$old_file"
set_photoid "$old_file" "$new_photoid"
if [[ -v REMOVE_OLD_TITLE_KEYWORD ]]; then
update_keywords "-$old_title" "$old_file"
fi
if [[ -v ADD_TITLE_KEYWORD ]]; then
update_keywords "$new_title" "$old_file"
fi
reprotect_check_file "$old_file"
mv "$old_file" "$newfile"
echo "$newfile"
}
if [[ $# = 0 ]]; then
collect_associated_files < /dev/stdin | while read -r file; do apply_set_title "$file"; done
else
collect_associated_files "$@" | while read -r file; do apply_set_title "$file"; done
fi