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
10 # TODO: add a switch to replace only non RFC-compliant Date: headers
24 echo " ${0##*/} [<switches>] <filename> [<filename> ..]"
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 -)"
35 # use --force to always write the Date header
36 # otherwise, the Date header will be written if only it doesn't already
39 # use --debug to display more information about what's performed
51 -*) echo "error: unrecognized switch '$1'"
61 TMP="/tmp/${0##*/}.tmp"
65 # skip if file is empty or doesn't exist
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)"
81 # determine which replacement date to use
82 if [ -z "$RECEIVED_DATE" ]
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"
90 REPLACEMENT_DATE="$RECEIVED_DATE"
91 REPLACEMENT="received date"
94 # ensure that a X-Original-Date is set (but don't override it)
95 if [ -z "$X_ORIGINAL_DATE" ]
99 echo "X-Original-Date:$REPLACEMENT_DATE" > "$TMP"
101 test $FORCE -eq 1 && \
102 echo "X-Original-Date:$DATE" > "$TMP"
108 # replace/set the date and write all lines
111 test $DEBUG -eq 1 && \
112 echo "$1: date not found, using $REPLACEMENT now"
113 echo "Date:$REPLACEMENT_DATE" >> "$TMP"
118 test $DEBUG -eq 1 && \
119 echo "$1: date already found, replacing with $REPLACEMENT"
120 sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
122 test $DEBUG -eq 1 && \
123 echo "$1: date already found, skipping"
128 # uncomment the following line to backup the original file
134 echo "error while moving '$TMP' to '$1'"