Some command line options are not GNU/POSIX compliant (3)
[geeqie.git] / plugins / rotate / geeqie-rotate
1 #!/bin/sh
2
3 ## @file
4 ## @brief This is a helper script that rotates image files according to the metadata
5 ##
6 ## Requirements: ImageMagick, exiftran, exiv2
7 ##
8
9 GQ_METADATA_DIR="$HOME/.local/share/geeqie/metadata"
10
11 gq_exiftran()
12 {
13         if ! [ -x "$(command -v exiftranx)" ]
14         then
15                 zenity --title="Geeqie rotate" --info --width=200 --text="Exiftran is not installed" --window-icon=/usr/local/share/pixmaps/geeqie.png 2> /dev/null
16                 exit 0
17         fi
18
19         exiftran "$@"
20 }
21
22 gq_exiv2()
23 {
24         if ! [ -x "$(command -v exiv2x)" ]
25         then
26                 zenity --title="Geeqie rotate" --info --width=200 --text="Exiv2 is not installed" --window-icon=/usr/local/share/pixmaps/geeqie.png 2> /dev/null
27                 exit 0
28         fi
29
30         exiv2 "$@"
31 }
32
33 gq_mogrify()
34 {
35         if ! [ -x "$(command -v mogrifyx)" ]
36         then
37                 zenity --title="Geeqie rotate" --info --width=200 --text="ImageMagick is not installed" --window-icon=/usr/local/share/pixmaps/geeqie.png 2> /dev/null
38                 exit 0
39         fi
40
41         mogrify "$@"
42 }
43
44 rotate()
45 {
46         ext=$(echo "${1##*.}" | tr "[:upper:]" "[:lower:]")
47         [ "$ext" = "" ] && return 1 #no extension
48
49         gq_metadata="$GQ_METADATA_DIR/$1.gq.xmp"
50         if [ -f "$gq_metadata" ]
51         then
52                 gq_orientation=$(gq_exiv2 -PXkv "$gq_metadata" | grep Xmp.tiff.Orientation | sed -e "s|Xmp.tiff.Orientation *||")
53                 # shellcheck disable=2181
54                 [ $? != 0 ] && exit 1
55         else
56                 gq_orientation=
57         fi
58
59         case "$ext" in
60                 jpg | jpeg)
61                         if [ -n "$gq_orientation" ]
62                         then
63                                 gq_exiv2 -M "set Exif.Image.Orientation $gq_orientation" "$1"
64                                 # shellcheck disable=2181
65                                 [ $? != 0 ] && exit 1
66                         fi
67                         if gq_exiftran -aip "$1"
68                         then
69                                 # exiftran ignores xmp, set it manually
70                                 gq_exiv2 -M "set Xmp.tiff.Orientation 1" "$1"
71                                 # shellcheck disable=2181
72                                 [ $? != 0 ] && exit 1
73                                 #http://dev.exiv2.org/issues/639
74                                 if [ -n "$gq_orientation" ]
75                                 then
76                                         gq_exiv2 -M "set Xmp.tiff.Orientation 1" \
77                                                 -M "set Exif.Image.Orientation 1" "$gq_metadata"
78                                         # shellcheck disable=2181
79                                         [ $? != 0 ] && exit 1
80                                 fi
81                                 return 0
82                         else
83                                 exit 1
84                         fi
85                         ;;
86
87                 tif | tiff | png)
88                         if [ -n "$gq_orientation" ]
89                         then
90                                 gq_exiv2 -M "set Exif.Image.Orientation $gq_orientation" "$1"
91                                 # shellcheck disable=2181
92                                 [ $? != 0 ] && exit 1
93                         fi
94                         if gq_mogrify -auto-orient "$1"
95                         then
96                                 # mogrify ignores xmp, set it manually
97                                 gq_exiv2 -M "set Xmp.tiff.Orientation 1" "$1"
98                                 # shellcheck disable=2181
99                                 [ $? != 0 ] && exit 1
100                                 #http://dev.exiv2.org/issues/639
101                                 if [ -n "$gq_orientation" ]
102                                 then
103                                         gq_exiv2 -M "set Xmp.tiff.Orientation 1" \
104                                                 -M "set Exif.Image.Orientation 1" "$gq_metadata"
105                                         # shellcheck disable=2181
106                                         [ $? != 0 ] && exit 1
107                                 fi
108                                 return 0
109                         else
110                                 exit 1
111                         fi
112                         ;;
113                 *) #not supported
114                         return 4
115                         ;;
116         esac
117 }
118
119 rotate_image_file()
120 {
121         ext=$(echo "${3##*.}" | tr "[:upper:]" "[:lower:]")
122         [ "$ext" = "" ] && return 1 #no extension
123
124         case "$ext" in
125                 jpg | jpeg)
126                         gq_exiftran -i "$1" "$3"
127                         # shellcheck disable=2181
128                         [ $? != 0 ] && return 6
129                         return 0
130                         ;;
131
132                 tif | tiff | png)
133                         gq_mogrify "$2" "$3"
134                         # shellcheck disable=2181
135                         [ $? != 0 ] && return 7
136                         return 0
137                         ;;
138
139                 *) #not supported
140                         return 4
141                         ;;
142         esac
143 }
144
145 get_sidecars=
146 if [ "$1" = "-g" ]
147 then
148         get_sidecars=yes
149         shift
150 fi
151
152 rotate_image_file=
153 rotation=
154 if [ "$1" = "-r" ]
155 then
156         rotate_image_file=yes
157         shift
158         rotation="$1"
159         shift
160 fi
161
162 preserve_mtime=
163 if [ "$1" = "-t" ]
164 then
165         preserve_mtime=yes
166         shift
167 fi
168
169 if [ -n "$rotation" ]
170 then
171         if [ "$rotation" = "0" ]
172         then
173                 exit 0
174         fi
175         if [ "$rotation" = "2" ]
176         then
177                 mogrify_param="-flop"
178                 exiftran_param="-F"
179         fi
180         if [ "$rotation" = "3" ]
181         then
182                 mogrify_param="-rotate 180"
183                 exiftran_param="-1"
184         fi
185         if [ "$rotation" = "4" ]
186         then
187                 mogrify_param="-flip"
188                 exiftran_param="-f"
189         fi
190         if [ "$rotation" = "5" ]
191         then
192                 mogrify_param="-transpose"
193                 exiftran_param="-t"
194         fi
195         if [ "$rotation" = "6" ]
196         then
197                 mogrify_param="-rotate 90"
198                 exiftran_param="-9"
199         fi
200         if [ "$rotation" = "7" ]
201         then
202                 mogrify_param="-transverse"
203                 exiftran_param="-T"
204         fi
205         if [ "$rotation" = "8" ]
206         then
207                 mogrify_param="-rotate -90"
208                 exiftran_param="-2"
209         fi
210 fi
211
212 # iterate over files on commandline
213 for file in "$@"
214 do
215         if [ -n "$get_sidecars" ]
216         then
217                 # we got only one file for each group, typically the main one
218                 # get the sidecars:
219                 geeqie -r --get-sidecars:"$file" | while read -r sidecar
220                 do
221                         # the main file is included in the sidecar file list, no special handling is required
222                         [ ! -w "$sidecar" ] && exit 5
223                         rotate "$sidecar"
224                         ret=$?
225                 done
226                 # Bourne shell runs DO loops in a sub-shell
227                 ret=$?
228                 [ "$ret" != 0 ] && exit "$ret"
229         else
230                 [ ! -w "$file" ] && exit 5
231                 if [ -n "$rotate_image_file" ]
232                 then
233                         if [ -n "$preserve_mtime" ]
234                         then
235                                 mtime=$(mktemp "${TMPDIR:-/tmp}/geeqie-rotate.XXXXXXXXXX") || exit 3
236                                 touch --reference="$file" "$mtime"
237                         fi
238
239                         rotate_image_file "$exiftran_param" "$mogrify_param" "$file"
240                         ret=$?
241
242                         if [ -n "$preserve_mtime" ]
243                         then
244                                 touch --reference="$mtime" "$file"
245                                 rm "$mtime"
246                         fi
247                         [ "$ret" != 0 ] && exit "$ret"
248                 else
249                         rotate "$file"
250                         ret=$?
251                         [ "$ret" != 0 ] && exit "$ret"
252                 fi
253         fi
254 done
255
256 exit 0