#!/bin/bash
#
# $Id: rdisc,v 1.5 2005/04/05 10:06:01 bastian Exp $
#
# chkconfig: 2345 98 01
### BEGIN INIT INFO
# Provides: ualftnl
# Required-Start: $syslog $local_fs $remote_fs $network $rpcbind
# Required-Stop: $syslog $local_fs $remote_fs $network $rpcbind
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: This is a daemon which establishes a tunnel.
# Description: This daemon sets tunnel to home
### END INIT INFO

# Source function library.
. /etc/init.d/functions
. /etc/sysconfig/network

pid_file=/var/run/ualftnl.pid;

start_tunnel(){
    # daemon -f /sbin/ualf_tunnel > /dev/null);
    # > /dev/null 2>&1
    # watch /sbin/ualf_tunnel  > /dev/null 2>&1;
 
    /sbin/ualf_tunnel.sh > /dev/null 2>&1 & exit 0;
}
 
case "$1" in
  start)
    if [ -f $pid_file ]; then
        echo "ualf_tunnel is running already."
        failure
    else
        echo "Starting tunnel."
        start_tunnel
        success
    fi;
    exit 1;
    ;;
  stop)
    echo "Stopping tunnel."
    if [ -f $pid_file ]; then
        shell_pid=$(cat $pid_file)
	ssh_pid=$(ps --ppid $shell_pid | grep  ssh | sed -e "s/^ *\([0-9]*\) .*$/\1/")
        kill -KILL $shell_pid;
        kill -KILL $ssh_pid;
        rm -f $pid_file;
        success
    else
        echo "ualf_tunnel is not running."
        failure
    fi;
    
    exit 0;
    ;;
  *)
    echo "Usage: /etc/init.d/ualftnl {start|stop}"
    exit 1;
    ;;
esac
 
exit 0;
