202d73c57dfcc176add80f7ed2dbb089e3cfabcb
[scripts.git] / archivepics.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 # (1) Check that all JPG are larger than 1M if there is no
21 # corresponding MOV
22
23 # (2) Change the modification time to match the EXIF info (use the
24 # accompanying JPG for the MOV)
25
26 # (3) Move the JPG and MOV to an archive directory of the form
27 # ${IMAGE_ARCHIVE_DIR}/year/month, create the directory when necessary.
28
29 [[ ${archive_size_min} ]] || archive_size_min=1048576 # 1Mb
30
31 set -e
32
33 if [[ ! ${IMAGE_ARCHIVE_DIR} ]]; then
34     echo "You have to set \$IMAGE_ARCHIVE_DIR" >&2
35     exit 1
36 fi
37
38 # Checking the image sizes
39
40 for i in "$@"; do
41
42     if [[ ! -f ${i/JPG/MOV} ]] && [[ ! -f ${i/JPG/3gp} ]]; then
43
44         if [[ $(stat --printf=%s $i) -lt ${archive_size_min} ]]; then
45             echo "Image $i is too small."
46             exit 1
47         fi
48
49     fi
50
51 done
52
53 # Setting the file modification date according to the exif tag
54
55 for i in "$@"; do
56
57     base=${i/%.???/}
58
59     if [[ -f ${base}.JPG ]]; then ref=${base}.JPG; fi
60     if [[ -f ${base}.jpg ]]; then ref=${base}.jpg; fi
61     if [[ -f ${base}.jpeg ]]; then ref=${base}.jpeg; fi
62
63     echo "ref=${ref}"
64
65     if [[ -f ${ref} ]]; then
66
67         TAG=$(exif --ifd=EXIF -t "Date and Time (original)" ${ref} | grep Value)
68
69         if [[ ${TAG} ]]; then
70             TIMESTAMP=$(echo ${TAG} | sed -e \
71                 "s/^ *Value: *\([0-9]*\):\([0-9]*\):\([0-9]*\) *\([0-9]*\):\([0-9]*\):\([0-9]*\).*$/\1\2\3\4\5.\6/")
72             touch -t $TIMESTAMP $i
73         else
74             echo "No exif tag in $i, can not set the date properly."
75         fi
76
77     else
78
79         echo "Can not find a reference picture file for $i, can not set the date properly."
80
81     fi
82
83 done
84
85 # Moving the file to the proper archive directory, which is created
86 # first when needed
87
88 for i in "$@"; do
89
90     ARCHIVING_PATH=$(ls -l --time-style=+%Y/%b --format=verbose $i | awk '{ print $6 }')
91
92     mkdir -p ${IMAGE_ARCHIVE_DIR}/${ARCHIVING_PATH}
93
94     echo "Archiving ${IMAGE_ARCHIVE_DIR}/${ARCHIVING_PATH}/$(basename $i)"
95
96     cp -p -i $i ${IMAGE_ARCHIVE_DIR}/${ARCHIVING_PATH}
97
98 done