#!/bin/bash

# simple replacement for lpr -- uses ssh
# This script does not filter, so only use it to "lpr" PostScript
# files.  (use enscript for text)
# exercise for the reader: make this script use magicfilter

# an unprivileged port you don't use for anything else
LOCAL_PORT=10515

# A host that is allowed to connect to your networked printer or
# print server, and on which you have an account you can ssh to
# without being prompted for a password (see man ssh-agent)
HOST=capsicum.zgp.org

# Name of your printer or printer server
PRINTER=treekiller.sandbox.zgp.org

# How long to leave the connection up.  If this script fails for you,
# try increasing this
SLEEP=5

# Here's the fun part: we open up a port for forwarding, put ssh in
# the background as soon as it's authenticated, and sleep to give
# rlpr a chance to connect
ssh -fL ${LOCAL_PORT}:${PRINTER}:515 $HOST sleep $SLEEP && /usr/bin/rlpr --send-data-first --printhost localhost --port $LOCAL_PORT
$*

# you might not have to use --send-data-first, but we had to for
# an older Hewlett-Packard LaserJet.

# This last line makes sure the script does not exit until LOCAL_PORT
# is available to use again

sleep $SLEEP

