3611d6723d4e7be4ef58edf90324d0d924ea455b
[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 # TODO: add a switch to replace only non RFC-compliant Date: headers
11
12 VERSION="0.0.3"
13
14
15 function version()
16 {
17         echo "$VERSION"
18         exit 0
19 }
20
21 function usage()
22 {
23         echo "usage:"
24         echo "  ${0##*/} [<switches>] <filename> [<filename> ..]"
25         echo "switches:"
26         echo "  --help     display this help then exit"
27         echo "  --version  display version information then exit"
28         echo "  --force    force writting of Date: header even if it already exists"
29         echo "  --debug    turn on debug information (be more verbose)"
30         echo "  --         end of switches (in case a filename starts with a -)"
31         exit $1
32 }
33
34
35 # use --force to always write the Date header
36 # otherwise, the Date header will be written if only it doesn't already
37 # exist
38 FORCE=0
39 # use --debug to display more information about what's performed
40 DEBUG=0
41
42 while [ -n "$1" ]
43 do
44         case "$1" in
45         --help)         usage 0;;
46         --version)      version;;
47         --force)        FORCE=1;;
48         --debug)        DEBUG=1;;
49         --)                     shift
50                                 break;;
51         -*)                     echo "error: unrecognized switch '$1'"
52                                 usage 1;;
53         *)                      break;;
54         esac
55         shift
56 done
57
58 test $# -lt 1 && \
59         usage 1
60
61 TMP="/tmp/${0##*/}.tmp"
62
63 while [ -n "$1" ]
64 do
65         # skip if file is empty or doesn't exist
66         if [ ! -s "$1" ]
67         then
68                 shift
69                 continue
70         fi
71
72         X_ORIGINAL_DATE=$(grep -Eim 1 '^X-Original-Date: ' "$1" | cut -d ':' -f 2)
73         DATE=$(grep -Eim 1 '^Date: ' "$1" | cut -d ':' -f 2)
74         RECEIVED_DATE=$(grep -Eim 1 ';( (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]+' "$1" | cut -d ';' -f 2)
75 # strict, day of week needed
76 #       RECEIVED_DATE=$(grep -Eim 1 '; (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]+' "$1" | cut -d ';' -f 2)
77         FILE_DATE=$(ls -l --time-style="+%a, %d %b %Y %X %z" "$1" | tr -s ' ' ' ' | cut -d ' ' -f 6-11)
78         # we could also use the system date as a possible replacement
79         #SYSTEM_DATE="$(date -R)"
80
81         # determine which replacement date to use
82         if [ -z "$RECEIVED_DATE" ]
83         then
84                 # don't forget the leading whitespace here
85                 REPLACEMENT_DATE=" $FILE_DATE"
86                 REPLACEMENT="file date"
87 #               REPLACEMENT_DATE=" $SYSTEM_DATE"
88 #               REPLACEMENT="system date"
89         else
90                 REPLACEMENT_DATE="$RECEIVED_DATE"
91                 REPLACEMENT="received date"
92         fi
93
94         # ensure that a X-Original-Date is set (but don't override it)
95         if [ -z "$X_ORIGINAL_DATE" ]
96         then
97                 if [ -z "$DATE" ]
98                 then
99                         echo "X-Original-Date:$REPLACEMENT_DATE" > "$TMP"
100                 else
101                         test $FORCE -eq 1 && \
102                                 echo "X-Original-Date:$DATE" > "$TMP"
103                 fi
104         else
105                 :> "$TMP"
106         fi
107
108         # replace/set the date and write all lines
109         if [ -z "$DATE" ]
110         then
111                 test $DEBUG -eq 1 && \
112                         echo "$1: date not found, using $REPLACEMENT now"
113                 echo "Date:$REPLACEMENT_DATE" >> "$TMP"
114                 cat "$1" >> "$TMP"
115         else
116                 if [ $FORCE -eq 1 ]
117                 then
118                         test $DEBUG -eq 1 && \
119                                 echo "$1: date already found, replacing with $REPLACEMENT"
120                         sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
121                 else
122                         test $DEBUG -eq 1 && \
123                                 echo "$1: date already found, skipping"
124                         cat "$1" >> "$TMP"
125                 fi
126         fi
127
128         # uncomment the following line to backup the original file
129         #mv -f "$1" "$1.bak"
130
131         mv -f "$TMP" "$1"
132         if [ $? -ne 0 ]
133         then
134                 echo "error while moving '$TMP' to '$1'"
135                 exit 1
136         fi
137
138         shift
139 done
140 exit 0