2.10.0 unleashed
[claws.git] / tools / fix_date.sh
blob901fa5a256d5a2400ad3ad8a01b1299793497e26
1 #!/bin/sh
3 # * Copyright 2004 Tristan Chabredier <wwp@claws-mail.org>
4 # *
5 # * This file is free software; you can redistribute it and/or modify it
6 # * under the terms of the GNU General Public License as published by
7 # * the Free Software Foundation; either version 2 of the License, or
8 # * (at your option) any later version.
9 # *
10 # * This program is distributed in the hope that it will be useful, but
11 # * WITHOUT ANY WARRANTY; without even the implied warranty of
12 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # * General Public License for more details.
14 # *
15 # * You should have received a copy of the GNU General Public License
16 # * along with this program; if not, write to the Free Software
17 # * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # fix_date.sh helper script to fix non-standard date or add missing
20 # date header to emails
22 # usage: fix_date.sh <filename> [<filename>..]
23 # It will replace the Date: value w/ the one picked up from more recent
24 # Received: field if this field resides in one line. Otherwise, it will
25 # take the file modification time (using a RFC 2822-compliant form).
26 # If no X-Original-Date already exist, the former Date value will be set
27 # in such field.
29 VERSION="0.0.4"
32 function version()
34 echo "$VERSION"
35 exit 0
38 function usage()
40 echo "usage:"
41 echo " ${0##*/} [<switches>] <filename> [<filename> ..]"
42 echo "switches:"
43 echo " --help display this help then exit"
44 echo " --version display version information then exit"
45 echo " --force always force (re-)writing of Date: header"
46 echo " --rfc force re-writing of Date: header when it's not RFC-compliant"
47 echo " --debug turn on debug information (be more verbose)"
48 echo " --strict use RFC-strict matching patterns for dates"
49 echo " -- end of switches (in case a filename starts with a -)"
50 exit $1
53 function date_valid()
55 test $STRICT -eq 1 && \
56 REGEXP="$DATE_REGEXP_STRICT" || \
57 REGEXP="$DATE_REGEXP"
59 echo "$1" | grep -qEim 1 "$REGEXP"
60 DATE_VALID=$?
63 # use --force to always (re-)write the Date header
64 # otherwise, the Date header will be written if only it doesn't exist
65 FORCE=0
66 # use --rfc to (re-)write the Date header when it's not RFC-compliant
67 # otherwise, the Date header will be written if only it doesn't exist
68 RFC=0
69 # use --debug to display more information about what's performed
70 DEBUG=0
71 # use --strict to use strict matching patterns for date validation
72 STRICT=0
73 # 0 = valid, always valid until --strict is used, then date_valid overrides this value
74 DATE_VALID=0
76 while [ -n "$1" ]
78 case "$1" in
79 --help) usage 0;;
80 --version) version;;
81 --force) FORCE=1;;
82 --debug) DEBUG=1;;
83 --rfc) RFC=1;;
84 --strict) STRICT=1;;
85 --) shift
86 break;;
87 -*) echo "error: unrecognized switch '$1'"
88 usage 1;;
89 *) break;;
90 esac
91 shift
92 done
94 if [ $FORCE -eq 1 -a $RFC -eq 1 ]
95 then
96 echo "error: use either --force or --rfc, but not both at the same time"
97 usage 1
100 test $# -lt 1 && \
101 usage 1
103 TMP="/tmp/${0##*/}.tmp"
105 DATE_REGEXP="( (Mon|Tue|Wed|Thu|Fri|Sat|Sun),)? [0-9]+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dev) [0-9]+ [0-9]+:[0-9]+:[0-9}+ [-+][0-9]+"
106 DATE_REGEXP_STRICT="(Mon|Tue|Wed|Thu|Fri|Sat|Sun), [0-9]+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dev) [0-9]+ [0-9]+:[0-9]+:[0-9}+ [-+][0-9]+"
108 while [ -n "$1" ]
110 # skip if file is empty or doesn't exist
111 if [ ! -s "$1" ]
112 then
113 shift
114 continue
117 X_ORIGINAL_DATE=$(grep -Eim 1 '^X-Original-Date: ' "$1" | cut -d ':' -f 2-)
118 DATE=$(grep -Eim 1 '^Date: ' "$1" | cut -d ':' -f 2-)
119 test $STRICT -eq 1 && \
120 RECEIVED_DATE=$(grep -Eim 1 ";$DATE_REGEXP" "$1" | cut -d ';' -f 2) || \
121 RECEIVED_DATE=$(grep -Eim 1 "; $DATE_REGEXP_STRICT" "$1" | cut -d ';' -f 2)
122 FILE_DATE=$(ls -l --time-style="+%a, %d %b %Y %X %z" "$1" | tr -s ' ' ' ' | cut -d ' ' -f 6-11)
123 # we could also use the system date as a possible replacement
124 #SYSTEM_DATE="$(date -R)"
126 # determine which replacement date to use
127 if [ -z "$RECEIVED_DATE" ]
128 then
129 # don't forget the leading whitespace here
130 REPLACEMENT_DATE=" $FILE_DATE"
131 REPLACEMENT="file date"
132 # REPLACEMENT_DATE=" $SYSTEM_DATE"
133 # REPLACEMENT="system date"
134 else
135 REPLACEMENT_DATE="$RECEIVED_DATE"
136 REPLACEMENT="received date"
139 # ensure that a X-Original-Date is set (but don't override it)
140 if [ -z "$X_ORIGINAL_DATE" ]
141 then
142 if [ -z "$DATE" ]
143 then
144 echo "X-Original-Date:$REPLACEMENT_DATE" > "$TMP"
145 else
146 test $FORCE -eq 1 && \
147 echo "X-Original-Date:$DATE" > "$TMP"
149 else
150 :> "$TMP"
153 # replace/set the date and write all lines
154 test $RFC -eq 1 && \
155 date_valid "$DATE"
156 if [ -z "$DATE" ]
157 then
158 test $DEBUG -eq 1 && \
159 echo "$1: date not found, using $REPLACEMENT now"
160 echo "Date:$REPLACEMENT_DATE" >> "$TMP"
161 cat "$1" >> "$TMP"
162 else
163 if [ $FORCE -eq 1 ]
164 then
165 test $DEBUG -eq 1 && \
166 echo "$1: date already found, replacing with $REPLACEMENT"
167 sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
168 else
169 if [ $RFC -eq 1 ]
170 then
171 if [ $DATE_VALID -ne 0 ]
172 then
173 test $DEBUG -eq 1 && \
174 echo "$1: date already found but not RFC-compliant, replacing with $REPLACEMENT"
175 sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
176 else
177 test $DEBUG -eq 1 && \
178 echo "$1: date already found and RFC-compliant, skipping"
179 cat "$1" >> "$TMP"
181 else
182 test $DEBUG -eq 1 && \
183 echo "$1: date already found, skipping"
184 cat "$1" >> "$TMP"
189 # uncomment the following line to backup the original file
190 #mv -f "$1" "$1.bak"
192 mv -f "$TMP" "$1"
193 if [ $? -ne 0 ]
194 then
195 echo "error while moving '$TMP' to '$1'"
196 exit 1
199 shift
200 done
201 exit 0