fix_date.sh 3.5 KB

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