-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_photo_album
executable file
·67 lines (55 loc) · 2.1 KB
/
change_photo_album
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
shopt -s nocasematch
declare -r BASE_DIR="$(dirname "$(readlink -e "$0")")"
source "$BASE_DIR/lib/directories.sh"
source "$BASE_DIR/lib/photoid.sh"
PATH="$BASE_DIR/metadata:$BASE_DIR:$PATH"
while getopts "ctao" opt; do
case $opt in
c ) shopt -u nocasematch;;
t ) CHANGE_PHOTO_TITLES_TO_ALBUM_NAME=;;
a ) ADD_NEW_ALBUM_KEYWORD=;;
o ) REMOVE_OLD_ALBUM_KEYWORD=;;
esac
done
shift $(expr $OPTIND - 1 )
declare -r ALBUM=$(echo "${1:-}" | tr ' ' '-')
if [[ -z $ALBUM ]]; then
echo "[ERROR] Album parameter missing" >&2
exit 1
elif [[ -f $ALBUM || -d $ALBUM ]]; then
echo "[ERROR] Album parameter is a file or directory" >&2
exit 1
fi
shift 1
set -o pipefail
collect_associated_files "$@" | while read -r file; do
old_album_path=$(directories_get_albumpath_from_file "$file")
old_album=${old_album_path##*/}
if [[ $old_album = $ALBUM ]]; then
echo "[WARN] Cannot change album of $file to \"$ALBUM\", same as old album" >&2
continue
fi
relative_to_album_path=${file#$old_album_path/}
new_album_parent=${old_album_path%/*}
newfullpath="$new_album_parent/$ALBUM/$relative_to_album_path"
if ! [[ -e $(dirname "$newfullpath") ]]; then
mkdir --parents "$(dirname "$newfullpath")"
elif [[ -e $newfullpath ]]; then
echo "[ERROR] Cannot change album of $file, file exists already in \"$ALBUM\": $newfullpath" >&2
continue
fi
mv "$file" "$newfullpath"
test -v REMOVE_OLD_ALBUM_KEYWORD && update_keywords "-$old_album" "$newfullpath"
test -v ADD_NEW_ALBUM_KEYWORD && update_keywords "$ALBUM" "$newfullpath"
if [[ -v CHANGE_PHOTO_TITLES_TO_ALBUM_NAME ]]; then
old_photoid=$(photoid_get_from_file "$newfullpath")
new_photoid=$(photoid_set_title "$old_photoid" "$ALBUM")
set_photoid "$newfullpath" "$new_photoid"
renamed_file="$(dirname "$newfullpath")/$new_photoid.${newfullpath#*.}"
# TODO we could do this a bit more cleverly, without an additional move
mv "$newfullpath" "$renamed_file"
fi
echo "$newfullpath"
done