#!/bin/bash -e
# if-up.d script to set up an ssh tunnel
# Don Marti <dmarti@zgp.org>
# Based on script for Postfix 
#   by LaMont Jones <lamont@debian.org>

# 'root' here must be able to log in as
# $REMOTE_USER there.

REMOTE_HOST=allium.zgp.org
REMOTE_USER=tunnel
LOCAL_PORT=10025
REMOTE_PORT=25
LOCAL_PIDFILE=/var/run/tunnel-out.pid
START_OPTS='--start --background --make-pidfile'

# don't bother when lo is configured.
if [ "$IFACE" = "lo" ]; then
    exit 0
fi

# Need to have /var mounted
if [ ! -d /var/run ]; then
    exit 0
fi

# Do we have a route to the outside?
# If yes then try the tunnel.
if /sbin/route -n | /bin/grep -q ^0.0.0.0 ; then
    /sbin/start-stop-daemon $START_OPTS \
    --pidfile $LOCAL_PIDFILE --exec /usr/bin/ssh \
    -- -NL $LOCAL_PORT:localhost:$REMOTE_PORT \
    $REMOTE_USER@$REMOTE_HOST
fi
