# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/sbin/install/ecc/lib/ECC/AIXDialer.pm 1.15 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
package AIXDialer;

use strict;
use Getopt::Std;
use vars qw/ %opt /;
use lib '/usr/ecc/lib/ECC';
use Definitions;
use DialPropertyManager;
use Utils;

################################################################################

sub addPAPUser() {
	my ($account, $pwd) = @_;
	my $rc = 99;

	my $cmd = "/etc/ppp/mkppp -pap -add -user $account -rh \'*\' -pw \'$pwd\' 1\>\/dev\/null 2\>\&1";
	$rc = &Utils::execute( $cmd );
  	return $rc;
}

################################################################################

sub delPAPUser() {
	my ($account, $pwd) = @_;
	my $rc = 99;

	my $cmd = "/etc/ppp/mkppp -pap -delete -user $account -rh \'*\' -pw \'$pwd\' 1\>\/dev\/null 2\>\&1";
	$rc = &Utils::execute( $cmd );
  	return $rc;
}


################################################################################

sub setupTTY() {
	my ($ttyPort, $baudRate) = @_;
	my $rc = 99;
	my $portSpeed = `lsattr -l $ttyPort -a speed -E \| awk \'{print \$2}\'`;
	if ( $portSpeed lt $baudRate ) {
		my @speeds = ($baudRate,'38400','19200');
		foreach (@speeds) {
		my $cmd = "chdev -l $ttyPort -a speed=$_ 1>\/dev\/console 2>/dev/null";
		$rc = &Utils::execute($cmd);
			if ( $rc eq 0 ) { 
				last;
			}
		}
	}
	return $rc;
}

################################################################################

sub disableLogin() {
	my ($ttyPort) = @_;
	my $rc = 99;

	my $portStat = `pstart\| egrep $ttyPort\| awk \'{print \$2}\'`;
	if ( $portStat ne "DISABLE" ) {
		my $cmd = "chdev -l $ttyPort -a login=disable 1>\/dev\/console";
		$rc = &Utils::execute($cmd);
	}
	else {
		$rc = 0;
	}
	return $rc;
}
################################################################################

sub resetTTY() {
	my ($ttyPort, $baudRate) = @_;
	my $rc = 99;

	my $cmd = "chdev -l $ttyPort -a login=disable 1>\/dev\/console";
	$rc = executeUtils::execute($cmd);
 	$cmd = "chdev -l $ttyPort -a speed=$baudRate";
	$rc = &Utils::execute($cmd);
	return $rc;
}

################################################################################

sub isPPPConfigured() {
	my $rc = 99;
	my $cmd = "ls  -l \/etc\/ppp\/lcp_config 1\>\/dev\/null 2\>\&1";
	$rc = system($cmd);
	return $rc;
}

################################################################################

sub isPPPAlive() {
	my $rc = 99;
	my $pid = `ps  -ef \| grep pppcontrold \| egrep -v grep \| awk \'{print \$2}\'`;
	if ( $pid ne "" ) {
		$rc = 0;
	}
	else {
		$rc = 1;
	}
	return $rc;
}

################################################################################

sub isPPPInterfaceAvailable() {
	my ($priority) = @_;
	my $rc = 99;

	my $cmd =  "ifconfig pp0 \| grep \"POINTOPOINT\" \| grep \"<UP,\" \| egrep -v grep 1>\/dev\/null 2>\&1";
	my $rc = system($cmd);
	if ( $rc ne 0 ) {
		return 1;
	}
	else {
		return 0;
	}	
}

################################################################################

sub pppLevel() {
	my $rc = 99;
	my $ppp = `lslpp -L|grep bos.net.ppp|awk \'{print \$2}\'`;
	if ( $ppp ne "") {
		$rc = 0;
	}
	else {
		$rc = 1;
	}
	return $rc;
}

################################################################################

sub startPPP() {
	my $rc = 99;
	my $cmd = "/etc/ppp/mkppp -start now > /dev/null 2>&1";
	my $rc = &Utils::execute($cmd);
	return $rc;
}

################################################################################

sub dialout() {
	my ($account, $ttyPort, $ipv) = @_;
	my $rc = 99;
   	my $cmd = "\/usr\/sbin\/pppattachd $ttyPort $ipv peer pap user \'$account\' client connect \'\/usr\/sbin\/pppdial -f ".Definitions::DIAL_FILE." -d ".Definitions::PPPDIAL_FILE."\' nodaemon &";
	$rc = &Utils::execute($cmd);
	return $rc;
}

################################################################################

sub pppGateway() {
	my $gateway = `netstat -rn \| grep pp0 \| grep UH \| awk \'{print \$1}\'`;
	chomp($gateway);
	return $gateway;
}

################################################################################

sub addRoute() {
	my ($host, $gateway) = @_;
	my $rc = 99;
	my $cmd = "route add $host $gateway 1>\/dev\/null 2>\&1";
	$rc = &Utils::execute($cmd);
	return $rc;
}

################################################################################

sub removeRoute() {
	my ($host, $gateway) = @_;
	my $rc = 99;	
	my $cmd = "route delete $host $gateway 1>\/dev\/null 2>\&1";
	$rc = &Utils::execute($cmd);
	return $rc;
}


################################################################################

sub endConnection() {
	my ($priority) = @_;
	my $rc = 99;

	my $ttyport = &getTTYPort($priority);
	my $ppp_pid = `ps -ef \| grep pppattachd \| grep $ttyport \| awk \'{print \$2}\'`;
	if ( $ppp_pid ne "" ) {
		$ppp_pid =~ s/\s+$//;
		my $cmd = "kill $ppp_pid  1>\/dev\/null 2>\&1";
		$rc = &Utils::execute($cmd);
	}
	if ( $rc ne 0 ) {
		return 1;
	}
	else {
		return 0;
	}
}

################################################################################

sub startConnection() {
	my ($priority, $location) = @_;

	my $dial_prop = new DialPropertyManager;
	$dial_prop->read( Definitions::DIAL_PROFILE_FILE );
	my $ttyport = $dial_prop->get(Definitions::TTYPORT.$priority);
	my $modem_type = $dial_prop->get(Definitions::MODEM_TYPE.$priority);
	my $init_string = $dial_prop->get(Definitions::INIT_STRING.$priority);
	my $reset_string = $dial_prop->get(Definitions::RESET_STRING.$priority);
	my $account;
	my $loc;
	if ( $location eq Definitions::PRIMARY_LOCATION ) {
		$account = $dial_prop->get(Definitions::ATT_PRIMARY_ID.$priority);
		$loc = $dial_prop->get(Definitions::PRIMARY_LOCATION."$priority");
	}
	else {
		$account = $dial_prop->get(Definitions::ATT_SECONDARY_ID.$priority);
       	$loc = $dial_prop->get(Definitions::SECONDARY_LOCATION."$priority");
	}
	my $dial_prefix = $dial_prop->get(Definitions::PREFIX."$priority");

	my ($country, $state, $city, $number ) = split(";",$loc);

	my $phoneNumber = $dial_prefix.$number;

	open DIALOUT, "+>".Definitions::DIAL_FILE or die $!;
	print DIALOUT "\'\'\n";
	print DIALOUT "$reset_string\n";
	print DIALOUT "OK\n";
	print DIALOUT "$init_string\n";
	print DIALOUT "OK\n";
	print DIALOUT "atdt$phoneNumber\n";
	print DIALOUT "CONNECT\n";
	close DIALOUT;

	&dialout($account, $ttyport, "ip");

	return 0;
}

################################################################################

sub getTTYPort() {
	my ($priority) = @_;	
	my $dial_prop = new DialPropertyManager;
	$dial_prop->read( Definitions::DIAL_PROFILE_FILE );
	my $ttyport = $dial_prop->get(Definitions::TTYPORT.$priority);
	if ( $ttyport eq "" ) {
		$ttyport = "null";
	}
    return ( $ttyport );
}

1;
