#!/bin/sh
#set -x

. /etc/tunnel/tunnel.conf

KILL=0
if [ _"$2"_ = _"-kill"_ ] ; then
    KILL=1
fi
if [ _"$1"_ = _"-kill"_ ] ; then
    KILL=1
    shift
fi


# using /home/tunnel/.ssh/config
CON="ssh -F /var/lib/tunnel/.ssh/config -N -R${tport}:localhost:22 ${CONHOST}"
if [ `ps ax | grep -v grep | grep -c "$CON"` -gt 0 ] ; then
    if [ $KILL -eq 0 ] ; then
        # echo "Connection $CONUSER to $CONHOST just established"
        exit
    fi
    # Killen der existierenden Verbindung
    conpid=`pgrep -f "${CON}$"`
    kill -9 $conpid > /dev/null 2> /dev/null
    ## Hier bleibt manchmal was übrig ...
    # pkill -f "${CON}$"
fi

$CON
