#!/bin/sh
# a very crude script to replace quoted-printable encoded strings
# (possibly spanning mulitiple lines) with 8-bit characters

perl -pe 's/(=[a-f\d][a-f\d])=\r?\n$/$1/i' $1 \
| perl -pe 'next unless /(=[a-f\d][a-f\d]){2}/i; s/=([a-f\d][a-f\d])/chr(hex($1))/ieg' \
| perl -pe 's/CHARSET=UTF-8.*?:/CHARSET=UTF-8;ENCODING=8BIT:/;'

# First join multiple qp-encoded lines. Make a minimal check
# so as not to mistakenly process non-qp-encoded lines ending with "=".
# Also take care of both linux newline (\n) and dos/windows newline (\r\n).

# Then do the conversion. Make a minimal check so as not to mistakenly
# process non-qp-encoded strings such as "PHOTO;ENCODING=BASE64;"

# Finally change the encoding string, or add one if there wasn't one.
