#!/usr/bin/perl

#
# rcsPppControl
#
# This script is a helper script that is designed to run the pppd program.
#
# Revisions:
#   02/03/2005  Stapels     Initial release.
#   02/23/2005  Stapels     Added --kill.
#

$mode = shift;
$pppUser = shift;
$pppPass = shift;
$pppArgs = "@ARGV";
$0 = "[hidden]";

if( $mode eq "--kill" )
{
    $pidfile = "/var/run/ppp-$pppUser.pid";

    exit( 0 ) unless( -f $pidfile );

    open( PIDFILE, $pidfile );
    chomp( $pid = (readline( PIDFILE ) or 0) );
    #chomp( $dev = (readline( PIDFILE ) or "") );
    close( PIDFILE );

    exit( 0 ) unless( $pid );

    kill( 15, $pid );
    exit( 0 );
}

$secretsFile = ($mode eq "--chap") ? "/etc/ppp/chap-secrets" : "/etc/ppp/pap-secrets";

umask( "0077" );

unless( open( SECRETS, ">$secretsFile" ) )
{
    print( STDERR "Failed to write to $secretsFile\n" );
    exit( -1 );
}

print( SECRETS "# Generated by rcsPppControl - " . `date` . "\n" );
print( SECRETS "$pppUser * $pppPass *\n" );
close( SECRETS );

umask( "0033" );

exec( "/usr/sbin/pppd $pppArgs" )
    or exit( 200 );
