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
22 echo " ${0##*/} [<switches>] <filename> [<filename> ..]"
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 -)"
36 test $STRICT -eq 1 && \
37 REGEXP="$DATE_REGEXP_STRICT" || \
40 echo "$1" | grep -qEim 1 "$REGEXP"
44 # use --force to always (re-)write the Date header
45 # otherwise, the Date header will be written if only it doesn't exist
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
50 # use --debug to display more information about what's performed
52 # use --strict to use strict matching patterns for date validation
54 # 0 = valid, always valid until --strict is used, then date_valid overrides this value
68 -*) echo "error: unrecognized switch '$1'"
75 if [ $FORCE -eq 1 -a $RFC -eq 1 ]
77 echo "error: use either --force or --rfc, but not both at the same time"
84 TMP="/tmp/${0##*/}.tmp"
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]+"
91 # skip if file is empty or doesn't exist
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)"
107 # determine which replacement date to use
108 if [ -z "$RECEIVED_DATE" ]
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"
116 REPLACEMENT_DATE="$RECEIVED_DATE"
117 REPLACEMENT="received date"
120 # ensure that a X-Original-Date is set (but don't override it)
121 if [ -z "$X_ORIGINAL_DATE" ]
125 echo "X-Original-Date:$REPLACEMENT_DATE" > "$TMP"
127 test $FORCE -eq 1 && \
128 echo "X-Original-Date:$DATE" > "$TMP"
134 # replace/set the date and write all lines
139 test $DEBUG -eq 1 && \
140 echo "$1: date not found, using $REPLACEMENT now"
141 echo "Date:$REPLACEMENT_DATE" >> "$TMP"
146 test $DEBUG -eq 1 && \
147 echo "$1: date already found, replacing with $REPLACEMENT"
148 sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
152 if [ $DATE_VALID -ne 0 ]
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"
158 test $DEBUG -eq 1 && \
159 echo "$1: date already found and RFC-compliant, skipping"
163 test $DEBUG -eq 1 && \
164 echo "$1: date already found, skipping"
170 # uncomment the following line to backup the original file
176 echo "error while moving '$TMP' to '$1'"