2006-12-05 [paul] 2.6.1cvs3
[claws.git] / tools / fix_date.sh
1 #!/bin/sh
2
3 # usage: fix_date.sh <filename> [<filename>..]
4 # It will replace the Date: value w/ the one picked up from more recent
5 # Received: field if this field resides in one line. Otherwise, it will
6 # take the file modification time (using a RFC 2822-compliant form).
7 # If no X-Original-Date already exist, the former Date value will be set
8 # in such field.
9
10 VERSION="0.0.4"
11
12
13 function version()
14 {
15         echo "$VERSION"
16         exit 0
17 }
18
19 function usage()
20 {
21         echo "usage:"
22         echo "  ${0##*/} [<switches>] <filename> [<filename> ..]"
23         echo "switches:"
24         echo "  --help     display this help then exit"
25         echo "  --version  display version information then exit"
26         echo "  --force    always force (re-)writing of Date: header"
27         echo "  --rfc      force re-writing of Date: header when it's not RFC-compliant"
28         echo "  --debug    turn on debug information (be more verbose)"
29         echo "  --strict   use RFC-strict matching patterns for dates"
30         echo "  --         end of switches (in case a filename starts with a -)"
31         exit $1
32 }
33
34 function date_valid()
35 {
36         test $STRICT -eq 1 && \
37                 REGEXP="$DATE_REGEXP_STRICT" || \
38                 REGEXP="$DATE_REGEXP"
39                 
40         echo "$1" | grep -qEim 1 "$REGEXP"
41         DATE_VALID=$?
42 }
43
44 # use --force to always (re-)write the Date header
45 # otherwise, the Date header will be written if only it doesn't exist
46 FORCE=0
47 # use --rfc to (re-)write the Date header when it's not RFC-compliant
48 # otherwise, the Date header will be written if only it doesn't exist
49 RFC=0
50 # use --debug to display more information about what's performed
51 DEBUG=0
52 # use --strict to use strict matching patterns for date validation
53 STRICT=0
54 # 0 = valid, always valid until --strict is used, then date_valid overrides this value
55 DATE_VALID=0
56
57 while [ -n "$1" ]
58 do
59         case "$1" in
60         --help)         usage 0;;
61         --version)      version;;
62         --force)        FORCE=1;;
63         --debug)        DEBUG=1;;
64         --rfc)          RFC=1;;
65         --strict)       STRICT=1;;
66         --)                     shift
67                                 break;;
68         -*)                     echo "error: unrecognized switch '$1'"
69                                 usage 1;;
70         *)                      break;;
71         esac
72         shift
73 done
74
75 if [ $FORCE -eq 1 -a $RFC -eq 1 ]
76 then
77         echo "error: use either --force or --rfc, but not both at the same time"
78         usage 1
79 fi
80
81 test $# -lt 1 && \
82         usage 1
83
84 TMP="/tmp/${0##*/}.tmp"
85
86 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]+"
87 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]+"
88
89 while [ -n "$1" ]
90 do
91         # skip if file is empty or doesn't exist
92         if [ ! -s "$1" ]
93         then
94                 shift
95                 continue
96         fi
97
98         X_ORIGINAL_DATE=$(grep -Eim 1 '^X-Original-Date: ' "$1" | cut -d ':' -f 2-)
99         DATE=$(grep -Eim 1 '^Date: ' "$1" | cut -d ':' -f 2-)
100         test $STRICT -eq 1 && \
101                 RECEIVED_DATE=$(grep -Eim 1 ";$DATE_REGEXP" "$1" | cut -d ';' -f 2) || \
102                 RECEIVED_DATE=$(grep -Eim 1 "; $DATE_REGEXP_STRICT" "$1" | cut -d ';' -f 2)
103         FILE_DATE=$(ls -l --time-style="+%a, %d %b %Y %X %z" "$1" | tr -s ' ' ' ' | cut -d ' ' -f 6-11)
104         # we could also use the system date as a possible replacement
105         #SYSTEM_DATE="$(date -R)"
106
107         # determine which replacement date to use
108         if [ -z "$RECEIVED_DATE" ]
109         then
110                 # don't forget the leading whitespace here
111                 REPLACEMENT_DATE=" $FILE_DATE"
112                 REPLACEMENT="file date"
113 #               REPLACEMENT_DATE=" $SYSTEM_DATE"
114 #               REPLACEMENT="system date"
115         else
116                 REPLACEMENT_DATE="$RECEIVED_DATE"
117                 REPLACEMENT="received date"
118         fi
119
120         # ensure that a X-Original-Date is set (but don't override it)
121         if [ -z "$X_ORIGINAL_DATE" ]
122         then
123                 if [ -z "$DATE" ]
124                 then
125                         echo "X-Original-Date:$REPLACEMENT_DATE" > "$TMP"
126                 else
127                         test $FORCE -eq 1 && \
128                                 echo "X-Original-Date:$DATE" > "$TMP"
129                 fi
130         else
131                 :> "$TMP"
132         fi
133
134         # replace/set the date and write all lines
135         test $RFC -eq 1 && \
136                 date_valid "$DATE"
137         if [ -z "$DATE" ]
138         then
139                 test $DEBUG -eq 1 && \
140                         echo "$1: date not found, using $REPLACEMENT now"
141                 echo "Date:$REPLACEMENT_DATE" >> "$TMP"
142                 cat "$1" >> "$TMP"
143         else
144                 if [ $FORCE -eq 1 ]
145                 then
146                         test $DEBUG -eq 1 && \
147                                 echo "$1: date already found, replacing with $REPLACEMENT"
148                         sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
149                 else
150                         if [ $RFC -eq 1 ]
151                         then
152                                 if [ $DATE_VALID -ne 0 ]
153                                 then
154                                         test $DEBUG -eq 1 && \
155                                                 echo "$1: date already found but not RFC-compliant, replacing with $REPLACEMENT"
156                                         sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
157                                 else
158                                         test $DEBUG -eq 1 && \
159                                                 echo "$1: date already found and RFC-compliant, skipping"
160                                         cat "$1" >> "$TMP"
161                                 fi
162                         else
163                                 test $DEBUG -eq 1 && \
164                                         echo "$1: date already found, skipping"
165                                 cat "$1" >> "$TMP"
166                         fi
167                 fi
168         fi
169
170         # uncomment the following line to backup the original file
171         #mv -f "$1" "$1.bak"
172
173         mv -f "$TMP" "$1"
174         if [ $? -ne 0 ]
175         then
176                 echo "error while moving '$TMP' to '$1'"
177                 exit 1
178         fi
179
180         shift
181 done
182 exit 0