Check that the reference file is indeed an image, write less warnings.
[scripts.git] / redatepic.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 while [ $1 ]; do
21
22     NAME=$1
23
24     REFERENCE=${NAME/MOV/JPG}
25
26     if [[ -f ${REFERENCE} ]] && file ${REFERENCE} | \grep -q image; then
27         # TAG=$(exif --ifd=EXIF -t "Date and Time (original)" ${REFERENCE} | grep Value)
28         TAG=$(exif 2>/dev/null --ifd=EXIF -t 0x9003 ${REFERENCE} | grep Value)
29
30         if [[ ! "${TAG}" ]]; then
31             # echo >&2 "  Cannot find the \"Date and Time (original)\" exif tag in ${REFERENCE}"
32             # echo >&2 "  Looking for \"Date and Time\""
33             TAG=$(exif -t 0x0132 ${REFERENCE} | grep Value)
34             # if [[ ${TAG} ]]; then
35                 # echo >&2 "  Ok, found!"
36             # fi
37         fi
38
39         if [[ ${TAG} ]]; then
40             TIMESTAMP=$(echo ${TAG} | sed -e \
41                 "s/^ *Value: *\([0-9]*\):\([0-9]*\):\([0-9]*\) *\([0-9]*\):\([0-9]*\):\([0-9]*\).*$/\1\2\3\4\5.\6/")
42             touch -t $TIMESTAMP ${NAME}
43             ls -l ${NAME}
44         else
45             echo "++ No exif tag in ${NAME}, can not set the date properly."
46         fi
47
48     else
49         echo "++ Can not find a reference picture file for ${NAME}, can not set the date properly."
50     fi
51
52     shift
53
54 done