#!/usr/bin/perl

use Locale::Country;

sub text_linker {
	# subrutin of printing texts
	undef $kirak;
	undef $po;
	@l_temp=split /\*\*\*/, $_[0];
	foreach (@l_temp) {
		$po++;
		$kirak.="$_$_[$po]";
	}
	return $kirak;
}

sub replace_country {
	%replacements = (
		"Lebannon", "Lebanon",
		"Czech Rep.", "Czech Republic",
		"Hong-Kong", "Hong Kong",
		"UAE", "United Arab Emirates",
		"Island", "Iceland",
		"UK", "United Kingdom",
		"Serbia-M.", "Serbia and Montenegro",
		"Finlan", "Finland",
		"St Vincent And The Grenadines", "Saint Vincent and the Grenadines",
		"Moldavia", "Moldova",
		"Macau, China", "China",	# Macau already in the provider name
		"Laos", "Lao People's Democratic Republic",
		"Serbia- Montenegro", "Serbia and Montenegro"

	);
	my $s = shift;

	$replaced = $replacements{$s};
	if ($replaced) {
		$s = $replaced;
	}
	$retval = country2code($s);
	$retval or return "unknown";
	return $retval;
}

print "<gprs-operators>\n";

open (FILE, "<apn");
#open (FILE, "<small-apn");
while ($read_line=<FILE>) {
	$elsochar=substr ($read_line, 0, 1);
	if ($elsochar ne "#" && $elsochar ne " " && $elsochar && $elsochar ne "\n") {
		@apn_tomb=split/\t/,$read_line;
		if ($apn_tomb[0] eq "0") { next };

		%apn_subs = (
			"31", "Dialog GSM (post-paid) (Sri Lanka)",
			"279", "Dialog GSM (pre-paid) (Sri Lanka)",
			"153", "T-Mobile (VoiceStream) (USA)",
			"162", "Telekom Srbija (default) (Serbia-M.)",
			"163", "Telekom Srbija (via MMS) (Serbia-M.)",
			"164", "Telekom Srbija (via wap) (Serbia-M.)",
			"178", "TIM (waptim) (Italy)",
			"190", "VIPNET (PRO) (Croatia)",
			"191", "VIPNET (START) (Croatia)",
			"237", "VIPNET (3G) (Croatia)",
			"263", "Djuice (Ukraine)"
		);
		if ($apn_subs{$apn_tomb[0]}) {
			$apn_tomb[1] = $apn_subs{$apn_tomb[0]};
		}

		foreach (@apn_tomb) {chomp $_}
		if (!$apn_tomb[3]) {$apn_tomb[3]="???"}
		if (!$apn_tomb[4]) {$apn_tomb[4]="???"}
		$apn_tomb[1]=~s /&/&amp;/;
		$apn_tomb[3]=~s /\(\*\)/anything/;
		$apn_tomb[4]=~s /\(\*\)/anything/;
		$apn_tomb[3]=~s /\(\*{2}\)//;
		$apn_tomb[4]=~s /\(\*{2}\)//;
		$apn_tomb[3]=~s /\(\*{3}\)/text_linker($cl[87])/e;
		$apn_tomb[4]=~s /\(\*{3}\)/text_linker($cl[87])/e;
		$apn_tomb[3]=~s /\(\*{4}\)/text_linker($cl[87])/e;
		$apn_tomb[4]=~s /\(\*{4}\)/text_linker($cl[87])/e;

		$apn_tomb[2]=~ s/\s+$//;

		@identifier=split/\(/,$apn_tomb[1];
		$name = $identifier[0];
		$name =~ s/\s+$//;

		$country = $identifier[2];
		$country =~s /\)//;
		$country =~ s/\s+$//;

		$type = $identifier[1];
		$type =~s /\)//;
		$type =~ s/\s+$//;

		if ($country eq "") {
			$country = $type;
			$type = "";
		}

		$new_country = replace_country($country);
		if ($new_country eq "unknown") {
			($type, $country) = ($country, $type);
			$new_country = replace_country($country);

			$new_country ne "unknown" or die "Unknown countries '$type' and '$country' for id $apn_tomb[0]";
		}
		$country = $new_country;

		print "\t<operator>\n";
		print "\t\t<id>$apn_tomb[0]</id>\n";
		print "\t\t<name>$name</name>\n";
		print "\t\t<country>$country</country>\n";
		if ($type ne "") {
			print "\t\t<type>$type</type>\n";
		}
		if ($apn_tomb[2] ne "") {
			print "\t\t<apn>$apn_tomb[2]</apn>\n";
		}
		if ($apn_tomb[3] ne "anything") {
			print "\t\t<username>$apn_tomb[3]</username>\n";
		}
		if ($apn_tomb[4] ne "anything") {
			print "\t\t<password>$apn_tomb[4]</password>\n";
		}
		print "\t\t<dns-servers>\n";
		print "\t\t\t<dns>$apn_tomb[5]</dns>\n";
		if ($apn_tomb[6] ne "") {
			print "\t\t\t<dns>$apn_tomb[6]</dns>\n";
		}
		print "\t\t</dns-servers>\n";
		print "\t</operator>\n";
	}
}

print "</gprs-operators>\n";
