# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72D src/bos/usr/sbin/install/ecc/lib/ECC/Utils.pm 1.14.1.2 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2015 
# 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 Utils;

use strict;
use Getopt::Std;
use IPC::Open2;
use vars qw/ %opt /;
use lib '/usr/ecc/lib/ECC';
use PropertyManager;
use ServiceProviderManager;
use Definitions;

use constant ADDED_FILE => '/var/ecc/data/added_to_etc_hosts.lck';

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

sub showMessage() {
	my ($set_number, $msg_number, $message) = @_;

	my $dspcmd = "dspmsg -s $set_number ".Definitions::NLS_CATALOG." $msg_number $message";
	my $rc = system($dspcmd);
	return $rc;
}

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

sub getJava() {
  if ( -d "/usr/java7_64" && -d "/usr/java7_64/bin" ) {
      return Definitions::JAVA7_64
  }
  elsif ( -d "/usr/java7" && -d "/usr/java7/bin" ) {
      return Definitions::JAVA7
  }
  else {
      return Definitions::JAVA6   
  }
}

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

sub execute() {
	my ($lcmd) = @_;
	my $rc = 99;
	open TRACEOUT, "+>> ".Definitions::TRACE_FILE or die $!;
	my $now = localtime time;
	print TRACEOUT "$now:  cmd = $lcmd\n";
	$rc = system("$lcmd");
	print TRACEOUT "$now:  result = $rc\n";
	close TRACEOUT;
	return $rc;
}

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

sub getAccount() {
	my ($country) = @_;
	return &ServiceProviderManager::RetrieveID($country);
}

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

sub getPathType() {
	my ($priority) = @_;
	 
    my $ecc_prop = new PropertyManager;
    $ecc_prop->read( Definitions::eccConnectPropertiesFile );

	if ( ($priority eq $ecc_prop->get(Definitions::DIRECT_PRIORITY)) &&
		($ecc_prop->get(Definitions::DIRECT_ENABLED) eq Definitions::YES) ) {
        return "DIRECT_INTERNET";
    }
    elsif ( $priority eq $ecc_prop->get(Definitions::PROXY_PRIORITY."$priority") ) {
        return "HTTP_PROXY";
    }
    elsif ( $priority eq $ecc_prop->get(Definitions::PROXY_PRIORITY."$priority") ) {
        return "HTTP_PROXY";
    }
    elsif ( $priority eq $ecc_prop->get(Definitions::PROXY_PRIORITY."$priority") ) {
        return "HTTP_PROXY";
    }
    elsif ( $priority eq $ecc_prop->get(Definitions::DIAL_PRIORITY."$priority") ) {
        return "DIAL";
    }
    elsif ( $priority eq $ecc_prop->get(Definitions::DIAL_PRIORITY."$priority") ) {
        return "DIAL";
    }
    elsif ( $priority eq $ecc_prop->get(Definitions::DIAL_PRIORITY."$priority") ) {
        return "DIAL";
    }
    else {
        return "NOT_CONFIGURED";
    }
}


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

sub testLANConnection() {
	my $result = 1;

	my $classpath = &java_classpath();
	my $cmd = &getJava()." -Djava.net.preferIPv6Addresses=true -cp $classpath com.ibm.esa.activation.ConnectivityServiceTest testLANConnection";

	my $pid = open2(\*RDR, \*WTR, $cmd);

	while (<RDR>) {
		$result = $_;
	}

	return $result;
}

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

sub testProxyConnection() {
	my ($proxyLocation, $proxyPort) = @_;
	my $result = 1;

	my $classpath = &java_classpath();
	my $cmd = &getJava()." -Djava.net.preferIPv6Addresses=true -cp $classpath com.ibm.esa.activation.ConnectivityServiceTest testProxyConnection $proxyLocation $proxyPort";
	my $pid = open2(\*RDR, \*WTR, $cmd);

	while (<RDR>) {
		$result = $_;
	}

	return $result;
}

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

sub testProxyConnectionSecure() {
	my ($proxyLocation, $proxyPort, $userid, $pwd) = @_;
	my $result = 1;

	my $classpath = &java_classpath();
	my $cmd = &getJava()." -Djava.net.preferIPv6Addresses=true -cp $classpath com.ibm.esa.activation.ConnectivityServiceTest testProxyConnectionSecure $proxyLocation $proxyPort $userid $pwd";
	my $pid = open2(\*RDR, \*WTR, $cmd);

	while (<RDR>) {
		$result = $_;
	}

	return $result;
}

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

sub HeartBeat() {
	my $result = 1;

	my $classpath = &java_classpath();
	my $cmd = &getJava()." -Djava.net.preferIPv6Addresses=true -cp $classpath com.ibm.esa.activation.ConnectivityServiceTest HeartBeat";
	my $pid = open2(\*RDR, \*WTR, $cmd);

	while (<RDR>) {
		$result = $_;
	}

	return $result;
}

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

sub java_classpath() {
        my $classpath="";

        my @ecc_jars = <\/usr\/ecc\/lib\/*.jar>;

        foreach my $file (@ecc_jars) {
                $classpath="$classpath:$file";
        }

        return $classpath;
}

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

sub add_ETC_HOSTS() {
	my ($ip, $host) = @_;

	open ETCHOSTS, "< /etc/hosts";
	my @lines = <ETCHOSTS>;
	my $found = 0;
	my $entry = "$ip\t$host\n";

	foreach (@lines) {
		if ($_ =~ m/www.ibm.com/) {
			$found = 1;
		}	
	}
	close ETCHOSTS;

	if ( ! $found ) {
		open ETCHOSTS, "> /etc/hosts";
		foreach (@lines) {
			print ETCHOSTS $_;
		}
		print ETCHOSTS "$entry";
		close ETCHOSTS;
		system("touch ".ADDED_FILE);
	}

	return 0;
}

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

sub remove_ETC_HOSTS() {
	if (-f ADDED_FILE) {
		open ETCHOSTS, "< /etc/hosts";
		my @lines = <ETCHOSTS>;
		close ETCHOSTS;
		
		open ETCHOSTS, "> /etc/hosts";
		foreach (@lines) {
			if ($_ !~ m/www.ibm.com/) {
				print ETCHOSTS $_;
			}
		}	
		close ETCHOSTS;
		system("rm ".ADDED_FILE);
	}

	return 0;
}

1;
