#! /bin/sh
# This script gets invoked by a hacked TFTP server.
# It is intended to allow students to print router config files.
#
# first line of stdin is meta information (IP address, file name).
# remainder of stdin is file contents.

read ipaddr filename

head1="Printed at AfNOG 2006 workshop"
head2="[$ipaddr] $filename"

spooldir="/var/spool/tftpd-print"
mkdir -p "${spooldir}" || spooldir="/tmp"
sanitised_filename="$( printf "%s" "${filename}" | tr -cs -- '-_.a-zA-Z0-9' . )"
timestamp="$( date +%Y%m%d.%H%M%S )"
spoolfile="${spooldir}/${ipaddr}-${timestamp}-${sanitised_filename}"

controlC="$(printf '\003')"

sed "s/${controlC}/\^C/g" | /usr/bin/tee "${spoolfile}" | /usr/local/bin/a2ps -q --header="$head1" --footer="$head2" --center-title="$head2" -o- | lpr -Plp
