#! /usr/bin/perl -w

### vtund-start
###
### script to start vtund as either a server or a client, according to
### the config file /etc/vtund-start.conf
###
### Copyright 1999 Craig Sanders <cas@taz.net.au>
###
### Written for the Debian GNU/Linux distribution.  This script is free
### software licensed under the terms of the GNU General Public License.

$DAEMON="/usr/sbin/vtund" ;

$do_what = shift ;
$args="start" ;
if ( $do_what !~ /^($args)$/i ) {
    print "Usage: /etc/init.d/vtun {$args}\n" ;
        exit 0 ;
}

$SSD="/sbin/start-stop-daemon" ;
$SSDARGS="--verbose --exec $DAEMON" ;

$sconf="/etc/vtund-start.conf" ;
open(SCONF,"<$sconf") || die "couldn't open $sconf: $!\n" ;
while (<SCONF>) {
        chomp ;
        s/#.*//;
        s/^ +| +$//;
        next if (/^$/) ;

        @line = split ;
        $host = shift(@line) ;
        $server = shift(@line) ;
        $args = "" ;
        foreach (@line) { $args .= " $_" } ;

        $host='' if ($host =~ /--server--/i ) ;

        &start($host,$server,$args) ;
}
close (SCONF);

sub start {
        my($host,$server,$args) = @_ ;
    print "  Starting vtun " ;
        if ($host eq '') {
                print "server\n" ;
				#print "$SSD --start $SSDARGS -- $args -s -P $server" ;
				system "$SSD --start $SSDARGS -- $args -s -P $server" ;
        } else {
                print "client $host to $server\n" ;
                #print  "$SSD --start $SSDARGS -- $args $host $server" ;
                system "$SSD --start $SSDARGS -- $args $host $server" ;
        }
} ;
