#!/bin/sh
#
if [ -f /var/run/tunnel.pid ] ; then 
    echo "tunnel is already running... skipping"
else
    if [ -f /etc/sysconfig/givc-server.conf ] ; then
	. /etc/sysconfig/givc-server.conf
        if [ $dhcp = "y" ] ; then
	    if [ -z `pidof dhcpcd|cut -f1 -d" "` ]; then
		dhcpcd eth0
	    fi
        fi
    else
        dir="n"
    fi
    if [ $dir = "proxy" ] ; then
	touch /var/run/tunnel.pid
	htcpid=`pidof htc|cut -f1 -d" "`	
	while [ -z $htcpid ] ; do
	    htc -P $prip:$prport -A $upname:$ppasswd -F $lport $serv:$rport &
	    htcpid=`pidof htc|cut -f1 -d" "`
	    if ! [ -z $htcpid ] ; then
		date1=`date`
		echo "$date1 started htc with pid $htcpid">>/store/log
		sshpid=`pidof ssh|cut -f1 -d" "`
		while [ -z $sshpid ] ; do
		    ssh -f updateserver -N &
		    sleep 30s
		    sshpid=`pidof ssh|cut -f1 -d" "`
		    if ! [ -z $sshpid ] ; then 
			date1=`date`
			echo "$date1 started ssh-tunnel with pid $sshpid">>/store/log
		    fi
		done
	    fi
	    sleep 5m
	    touch /var/run/tunnel.pid
	done
    else
        sshpid=`pidof ssh|cut -f1 -d" "`
	while [ -z $sshpid ] ; do  
	    ssh -f updateserver -N &
	    touch /var/run/tunnel.pid
	    sleep 30s
	    sshpid=`pidof ssh|cut -f1 -d" "`
	    if ! [ -z $sshpid ] ; then 
		date1=`date`
    		echo "$date1 started ssh-tunnel with pid $sshpid">>/store/log
	    fi
	    sleep 5m
	done
    fi
    touch /var/run/tunnel.pid
fi    
