a1d3f44df1c2184be3abd14ec920df2846b36c94
[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 # if [[ -z $* ]] && [[ ${PHO_NOTE_1} ]]; then
32     # echo "$0 ${PHO_NOTE_1}"
33     # $0 ${PHO_NOTE_1}
34     # exit 0
35 # fi
36
37 MONTHS=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
38
39 export ARCHIVED_PICS="$@"
40
41 set -e
42
43 if [[ ! ${IMAGE_ARCHIVE_DIR} ]]; then
44     echo "You have to set \$IMAGE_ARCHIVE_DIR" >&2
45     exit 1
46 fi
47
48 # Checking the image sizes
49
50 for i in "$@"; do
51
52     if [[ ! -f ${i/JPG/MOV} ]] && [[ ! -f ${i/JPG/3gp} ]]; then
53
54         if [[ $(stat --printf=%s "$i") -lt ${archive_size_min} ]]; then
55             echo "Image $i is too small."
56             exit 1
57         fi
58
59     fi
60
61 done
62
63 # Setting the file modification date according to the exif tag
64
65 for i in "$@"; do
66
67     base=${i/%.???/}
68
69     if [[ -f ${base}.JPG ]]; then ref=${base}.JPG; fi
70     if [[ -f ${base}.jpg ]]; then ref=${base}.jpg; fi
71     if [[ -f ${base}.jpeg ]]; then ref=${base}.jpeg; fi
72
73     # echo "ref=${ref}"
74
75     if [[ -f ${ref} ]]; then
76
77         echo "Checking ${ref} date"
78
79         TAG=$(exif --ifd=EXIF -t "Date and Time (original)" "${ref}" | grep Value)
80
81         if [[ ${TAG} ]]; then
82             TIMESTAMP=$(echo ${TAG} | sed -e \
83                 "s/^ *Value: *\([0-9]*\):\([0-9]*\):\([0-9]*\) *\([0-9]*\):\([0-9]*\):\([0-9]*\).*$/\1\2\3\4\5.\6/")
84             touch -t $TIMESTAMP "$i"
85         else
86             echo "No exif tag in $i, can not set the date properly."
87         fi
88
89     else
90
91         timestamp=($(basename $i | sed -e "s/^VID_\([0-9][0-9][0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)_\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\).*$/\1\2\3\4\5.\6/"))
92
93         if [[ "${timestamp}" ]]; then
94             echo "Setting time of $i to ${timestamp}"
95             touch -t ${timestamp} "$i"
96         else
97             echo "Can not guess date for $i."
98         fi
99
100     fi
101
102 done
103
104 # Moving the file to the proper archive directory, which is created
105 # first when needed
106
107 LOG_ARCHIVING=archive_$(date +%Y_%m_%d_%H:%M:%S).log
108
109 for i in "$@"; do
110
111     ARCHIVING_PATH=$(ls -l --time-style=+%Y/%b --format=verbose "$i" | awk '{ print $6 }')
112
113     mkdir -p ${IMAGE_ARCHIVE_DIR}/${ARCHIVING_PATH}
114
115     echo "Archiving ${IMAGE_ARCHIVE_DIR}/${ARCHIVING_PATH}/"$(basename "$i")
116
117     cp -p -i "$i" ${IMAGE_ARCHIVE_DIR}/${ARCHIVING_PATH} && \
118         echo "$i -> ${IMAGE_ARCHIVE_DIR}/${ARCHIVING_PATH}" >> ${LOG_ARCHIVING}
119
120 done