#!/bin/sh
#
# tinc		tincd VPN setup script
#
# chkconfig: 	2345 46 54
#
# version:	1.0.8
# authors:	Lubomir Bulej <pallas@kadan.cz>
#               Mads Kiilerich <mads@kiilerich.com>
#
# description:	This script parses tinc configuration files for networks given \
#		in /etc/tinc/nets.boot and for each of the networks it sets up \
#		the interface and static routes and starts the tinc daemon.
#
# processname:	tincd

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

#############################################################################
# configuration & sanity checks

TINCD=/usr/sbin/tincd
TCONF=/etc/tinc
TPIDS=/var/run
#DEBUG=-dddd

NETSFILE=$TCONF/nets.boot

# Check the daemon
if [ ! -x $TINCD ]; then
    echo "**tinc: $TINCD does not exist or is not executable!" >&2
    exit
fi

# Check the configuration directory
if [ ! -d $TCONF ]; then
    echo "**tinc: configuration directory ($TCONF) not found!" >&2
    exit
fi

# Check nets.boot
if [ ! -f $NETSFILE ]; then
    echo "**tinc: file with list of VPNs to start ($NETSFILE) not found!" >&2
    exit
fi

# Load names of networks to be started
NETS="$(sed -e 's/#.*//; s/[[:space:]]//g; /^$/ d' $NETSFILE)"


##############################################################################
# vpn_start ()		starts specified VPN
# 
# $1 ... VPN to start

vpn_start () {    
    # start tincd
    $TINCD --net="$1" $DEBUG || \
	{ MSG="could not start daemon for network $1"; return 3; }
    return 0
} # vpn_start


##############################################################################
# vpn_stop ()		Stops specified VPN
#
# $1 ... VPN to stop

vpn_stop () {
    # kill the tincd daemon
    PID="$TPIDS/tinc.$1.pid"
    if [ -f $PID ]; then
        $TINCD --net="$1" --kill &> /dev/null
        RET=$?
    
        if [ $RET -eq 0 ]; then
	    dly=0
	    while [ $dly -le 5 ]; do
		[ -f $PID ] || break
	        sleep 1; dly=$((dly + 1))
	    done
	fi
	
	# remove stale PID file
	[ -f $PID ] && rm -f $PID
    fi
    return 0
} # vpn_stop


# Check if there is anything to start
if [ ! -z "$1" -a "$1" != "status" -a -z "$NETS" ]; then
    echo "**tinc: no networks found in $NETSFILE!" >&2
    exit
fi


# See how we were called.
case "$1" in
    start)
	for vpn in $NETS; do
            echo -n "Bringing up TINC network $vpn: "
	    vpn_start $vpn && \
		success "startup of network $vpn" || \
		failure "startup of network $vpn"
	    echo
	    
	    if [ ! -z "$MSG" ]; then
	    	[ ! -z "$ERR" ] && echo "$ERR" >&2
		echo "**tinc: $MSG" >&2
	    fi
	done
	
        touch /var/lock/subsys/tinc
        ;;
	
    stop)
	for vpn in $NETS; do
  	    echo -n "Shutting down TINC network $vpn: "
	    vpn_stop $vpn && \
		success "shutdown of network $vpn" || \
		failure "shutdown of network $vpn"
	    echo
	    
	    if [ ! -z "$MSG" ]; then
	    	[ ! -z "$ERR" ] && echo "$ERR" >&2
		echo "**tinc: $MSG" >&2
	    fi
	done
	
	rm -f /var/lock/subsys/tinc
	;;
	
    status)
	echo -n "Configured VPNs: "
	for vpn in $NETS; do
	    PID="$TPIDS/tinc.$vpn.pid"
	    
	    [ -f $PID ] && PID="$(cat $PID)" || PID="-dead-"
	    ps ax | grep "^[[:space:]]*$PID" && STS="OK" || STS="DEAD"
	    echo -n "$vpn:$STS "
	done
	echo	         
	;;	
	
    restart)
        $0 stop
	$0 start
	;;
		
    *)
        echo "Usage: tinc {start|stop|status|restart}"
        exit 1
esac

exit 0
