#! /usr/bin/awk -f # Created: 22 Mar 16 # By: H.A.Trujillo # Last Change: 27 Mar 2017 # mprint: # formats concatenated mail messages for pretty-printing # (similar to "enscript -E mail", but for a file containing # multiple messages) # # Usage: # mprint < foo.txt # generates postscript and pdf files ( /tmp/mprint.{ps,pdf} ) # ready for printing, from an mbox-formatted file. # # In addition, /tmp/mprint.XXX will be saved with # appropriate enscript escapes, should 2-up or other # special printing be desired. # # Caveat: # if envelope-from is missing, "Date:" is assumed to be # the first line of the header. BEGIN{ "mktemp /tmp/mprint.XXX" | getline tmpfile # plaintext output file outfile = "/tmp/mprint" # root for .ps and .pdf files StatesFile = "/opt/local/share/enscript/hl/enscript.st" PrettyPrint = "states -D color=0 -f " StatesFile " >> " tmpfile divider= "============================================================" } /^From .*@.*([[:digit:]]{2}:){2}/,/^$/ { InHdr=1 } # Envelope From /^Date: ([[:alnum:]]|[, ])+(:[[:digit:]]{2}){2}/ { InHdr=1 } InHdr == 1 { # we're in the message header if ( PutDivider == 1 ) {# don't do this block on 1st msg close PrettyPrint # start States afresh with each new message printf "\n\n\n" divider "\n\n" >> tmpfile PutDivider=0 } if ($1 ~ "^(Date|From|Subject):") { print $0 | PrettyPrint } if ($1 ~ "^(To|C[cC]):") { # To/Cc: fields may extend over several lines print $0 | PrettyPrint ToField=1 next } if ( ToField == 1 && $0 ~ "^[[:blank:]]" ) { # continuation of To/Cc print " " $0 | PrettyPrint next } ToField=0 if ( $0 == "" ) { # exiting the header print $0 | PrettyPrint InHdr=0 PutDivider=1 } next } # skip divider-line if a page-feed (^L) is inserted before a message /^\014Date: / { close PrettyPrint } { print $0 | PrettyPrint } # Message Body END{ close PrettyPrint close tmpfile # process file so formatting may be checked system("enscript -e -i4 --word-wrap -p" outfile".ps <" tmpfile "; \ ps2pdf -o" outfile".pdf " outfile".ps ;\ open " outfile".pdf" \ ) print " text output left in " tmpfile " <-- don't forget the -e flag" } # vim: ft=awk nowrap