#!/usr/bin/perl
#
# SmoothWall CGIs
#
# This code is distributed under the terms of the GPL
#
# (c) The SmoothWall Team
#
# $Id: ip-down 2491 2009-02-19 14:22:17Z owes $
#

use strict;
require '/usr/lib/ipcop/general-functions.pl';

&General::log("PPP has gone down on $ARGV[0]");

umask 022;

my %mainsettings;

# read vars back from file.
&General::readhash('/var/ipcop/main/settings', \%mainsettings);

unlink "${General::swroot}/red/active";
system "/etc/rc.d/rc.updatered";

# Beep when ppp goes up or down if permittted.
system('/usr/bin/beep','-l 75', '-f 880', '-n', '-l 120', '-f 440') if ($mainsettings{'PPPUPDOWNBEEP'} eq 'on' );


if ( -e "${General::swroot}/red/keepconnected") {

	# We have to stop the ipppd (TYPE is ISDN) it stays even on disconnect (from ISP),
	# and rc.connectioncheck will fail to reconnect as it waits for [p]ppd to go away.
	my %pppsettings;
	&General::readhash("${General::swroot}/ppp/settings", \%pppsettings);

	if ($pppsettings{'TYPE'} eq 'isdn')
	{
		system ("/usr/bin/touch ${General::swroot}/red/redial");
		system ('/etc/rc.d/rc.red stop');
	}

	if ( system ('/bin/ps ax | /bin/grep -q "[r]c.connectioncheck reconnect"') ) {
		system ('/etc/rc.d/rc.connectioncheck reconnect &');
	} else {
		&General::log ('rc.connectioncheck reconnect already running');
	}
}
