Cool TWiki trick: auto-print changed documents

We’ve been using the TWiki system for documentation at work for a while now. But there’s some documents that while they’re convenient to edit in the TWiki, really need to have a paper version available in the data center. Luckily, our data center contains a duplex-capable printer, some red binders and occassionally some student assistants (interns).

We’re using a somewhat older version of Twiki (04Sep2004) instead of the relatively new 4.0 releases. I suspect that most of this works just fine, though.

We’re also using the GenPDF addon for TWiki, which relies on having htmldoc installed. This doesn’t require GenPDF, but I looked at how GenPDF was doing things to make this work.

Luckily, TWiki works with simple files on disk. The revisions are handled via RCS, but the latest version of a given document is a simple .txt file.

So, here’s my script. (modified to look a touch more generic) Probably a GNU-ism here or there, since I did do this on a Linux server.

#!/bin/sh

WIKIDIR=/var/html/wiki
DATADIR=$WIKIDIR/data
BINDIR=$WIKIDIR/bin

cd $BINDIR

for document in /Main/DCPowerUp /Main/DCEmergencyContacts
do
  if find $DATADIR/$document.txt -mtime -32 | grep $document
  then
    for copies in 1 2 3
    do
      PATH_INFO=$document ./view skin=plain 
        | tail --lines +3 
        | htmldoc --bodyfont times --headingfont helvetica --format ps3 
                  --duplex --gray --size letter --webpage 
                  --footer t.1 --header .c. 
                  - 
        | lp -o sides=one-side -d mr
    done
  fi
done

Originally, I was just piping HTML to links, but that wasn’t as pretty. I tried calling “genpdf pdfrecursive=1 pdfduplex=1”, stripping the HTTP headers off and then converting it to PostScript to send to the printer, but somehow that kept causing me problems.

The “tail –lines +3” is to strip the first 3 lines off (the HTTP headers).

The footer is the title on the left, nothing in the middle and the page number (arabic numeral) on the right. The header is the current chapter heading on top; aka, the most recent H1 tag, I think.

I send it 3 times to lp because telling lp I want 3 copies ends up disabling duplex.

And, of course, this is run in a cron job once a month. I went with 32 days to err on the side of printing. Wouldn’t want to modify a file with something important right at the start of the month and never have it come out.

The two documents are a list of emergency contacts and a document that mostly outlines what order to start everything up in. We’d had a few power outages caused by fire alarm testing gone wrong, and even though we try to make it so everything can just be turned on without regard to the state of anything else, in reality the SAN has to be running before you turn on the servers that use it; DNS, LDAP and DHCP are way more important than that build host; and turning on the webmail servers before you’ve got an IMAP server up is just plain confusing to the poor users.