#!/usr/bin/perl

use strict;

package         geo_cc;
require         Exporter;

our @ISA = ("Exporter");
our @EXPORT = qw(get_cc_list get_all_ccs get_cc_cont %cchash cc_hash %country_subregions);


### ccgroups - hash that maps which cc's are near others 

my %ccgroups = (
	ad => 'fr-es-de-nl-be',
	ae => 'sa-om-ye-qa-bh-kw-iq-il-it-ch-de',
	af => 'pk-tm-tj-uz-in-iq-sa-bd-kg-ae-om-ye-th-az-ge',
	al => 'gr-it-mk-me-rs-ba-bg-hr-ro-si-tr',
	am => 'ge-az-ru-tr-ua-il-bg-ro-hu-at-it-de',
	ar => 'uy-cl-py-br-bo-pe-ec-co-ve-gf',
	at => 'de-ch-lu-fr-cz-sk-hu-it-nl',
	au => 'nz-id-sg-my-tw-hk-kr-jp',
	az => 'am-ge-ru-tr-ua-il-bg-ro-hu-at-it-de',
	ba => 'hr-si-it-hu-rs-ro-bg',
	bd => 'in-cn-hk-sg-vn',
	be => 'fr-nl-de-lu-gb',
	bg => 'ro-gr-tr-it-md-hu-sk-cz-at-ch',
	bn => 'my-id-sg-th-ph-au',
	bo => 'pe-py-cl-ar-br-uy-co-ec-ve-gf-us',
	br => 'pe-uy-py-ar-bo-cl-gf-ve-co-ec-us',
	bs => 'us-ca',
	bw => 'za-zw-mz-na-zm-tz-ke-ng-mg-eg-il-tr-es-in',
	by => 'ua-pl-lt-lv-ru-de-ee-fi-ro',
	c1 => 'c1',
	c2 => 'c2',
	c3 => 'c3',
	ca => 'us-is',
	cd => 'ao-ug-rw-tz-ke-zw-bw-za-ng-ne-mg-tn',
	ch => 'de-fr-it-at-be-nl',
	cl => 'pe-br-ar-ec-py-uy-co-cr-us',
	cm => 'ng-za-ug-ke-tz-zw-bw-pt-es',
	cn => 'tw-hk-kr-sg-jp-mn-ru',
	co => 'ec-ve-pe-br-gf-pa-cr-ni-bo-cl',
	cr => 'pa-ni-co-ec-ve-br-mx-pe-us',
	cu => 've-co-ec-gf-pr-pe-br-us',
	cy => 'gr-tr-il-lb-sy-jo-eg-bg-ro',
	cz => 'sk-pl-de-at-nl',
	de => 'ch-fr-at-nl-be-dk-gb',
	dk => 'de-se-nl-no-fi',
	do => 'pr-ht-ve-br-co-gf-us',
	dz => 'ma-tn-ly-es-pt-fr-it',
	ec => 'co-ve-pe-br-pa-cr-bo-gf-cl-ni',
	ee => 'fi-lv-lt-ru-pl-se-no',
	eg => 'il-it-gr-tr-sa-iq-es-pt',
	es => 'pt-fr-it-de-gb-nl',
	et => 'ke-ug-tz-ye-zw-za-bw-tn-sa-mu',
	fi => 'se-dk-no-de-nl-ee',
	fr => 'de-nl-ch-be-es',
	gb => 'ie-nl-be-fr-de',
	ge => 'am-az-tr-ru-ua-bg-ro-md-hu-sk-at',
	gf => 'sr-br-gy-ve-co-pe-ec-bo-py-ar-cl',
	gh => 'gn-ng-ug-za-ke-tz-zw-bw-gb',
	gl => 'is-ca-us',
	gn => 'sn-gh-ng-za-ug-ke-tz-zw-bw-pt-es-fr',
	gr => 'tr-bg-it-ro-hu',
	gt => 'hn-sv-mx-ni-cr-pa-co-ec-us',
	hk => 'cn-tw-ph-vn-th',
	hn => 'gt-ni-sv-mx-cr-pa-co-ec-us',
	hr => 'si-ba-hu-rs-me-it-at-de',
	hu => 'at-ro-sk-si-ba',
	id => 'sg-my-ph-au-vn',
	ie => 'gb-be-nl-de-fr',
	il => 'it-tr-gr-bg-ro-hu',
	in => 'bd-vn-hk-sg-cn-lk',
	iq => 'tr-kw-sa-jo-sy-bh-qa-ae-il-bg-ro-hu-at-it-de',
	ir => 'iq-af-pk-tr-kw-sa-om-jo-sy-bh-qa-ae-il-bg-ro-hu',
	is => 'dk-gb-no-se-nl-de',
	it => 'ch-at-fr-de-lu-si',
	jo => 'sa-iq-sy-eg-tr-cy-kw-gr-bg-ro',
	jp => 'sg-cn-kr-tw-ru',
	ke => 'tz-ug-et-zw-za-bw-tn-mu',
	kg => 'kz-uz-tj-cn-tm-ru-mn-pk-az',
	kh => 'th-vn-my-sg-id',
	kr => 'jp-cn-tw-hk-sg-ru',
	kw => 'sa-iq-ae-tr-jo-il-am-az-ge-ru',
	kz => 'ru-uz-kg-tj-tm-cn-mn-ro-de',
	la => 'th-vn-my-sg-hk-cn',
	lb => 'il-sy-jo-cy-tr-iq-eg-gr',
	lk => 'in-bd-pk-th-sg-vn',
	lt => 'lv-pl-se-ee-by-de-fi-no',
	lu => 'de-nl-be-fr',
	lv => 'lt-ee-fi-se-ru-by-pl-de',
	ly => 'eg-tn-dz-it-gr-fr-es-il-tr-sa-iq',
	ma => 'dz-es-pt-ad-fr-it-de-be-nl',
	mc => 'fr-it-ch-de-es-pt-lu-be-nl',
	md => 'ro-ua-bg-si',
	me => 'ba-rs-al-mk-gr-bg-ro-hr-si-it-at',
	mg => 'mz-tz-zw-za-bw-ke-ug',
	mk => 'bg-gr-rs-al-me-ba-hr-ro',
	ml => 'gn-sn-gh-ng-ug-za-bw-ke-tz-zw-es-pt',
	mm => 'th-bd-in-cn-hk-my-sg-vn-id',
	mn => 'cn-ru-kz-hk',
	mt => 'it-fr-es-si-at-ch-de',
	mx => 'us-pa',
	my => 'id-sg-au-th-vn',
	mz => 'zw-za-tz-bw-ke-mg-ng-eg-il-tr-es-in',
	na => 'za-bw-ao-zw-mz-mg-tz-ke-ug-fr',
	nc => 'au-tw',
	ne => 'ng-ml-gh-gn-sn-ug-ke-za-tz-zw-bw-it-es',
	ng => 'gh-sn-za-ug-ke-tz-zw-bw-pt-es-fr',
	ni => 'hn-cr-gt-sv-mx-pa-co-ec-us',
	nl => 'de-dk-gb-be-fr',
	no => 'se-fi-dk-de-nl',
	np => 'in-bd-cn-th-sg-vn',
	nz => 'au-sg-jp-tw',
	om => 'sa-ae-ye-qa-bh-kw-iq-il-ch-it-de',
	pa => 'co-ec-mx-us',
	pe => 'co-ec-cl-br-bo-py-ve-ar-gf',
	ph => 'hk-vn-my-sg-id-au',
	pk => 'in-bd-af-tj-uz-kg-th-om-iq-sa',
	pl => 'cz-sk-de-ua-se-lt-nl-lv-ee-fi',
	pm => 'ca-us-gl-is',
	pr => 'do-ht-ve-gf-us',
	pt => 'es-fr-de-gb',
	py => 'ar-br-bo-uy-cl-pe-ec-co-ve-gf-us',
	qa => 'bh-ae-sa-kw-om-iq-ye-il-tr-eg-gr-it',
	ro => 'hu-bg-at-de',
	rs => 'mk-bg-ro-hu-hr-al',
	ru => 'fi-se-ee-lt-lv-by-ua-cn-de-kr-jp',
	rw => 'ug-ke-tz-cd-ao-zw-bw-za-ng-ne-mg',
	sa => 'qa-om-kw-ae-iq-jo-il-eg-tr-cy-gr-bg-ro-hu-sk',
	se => 'no-fi-dk-de-nl',
	sg => 'my-id-bn-th-ph-au',
	si => 'it-at-hr-hu-de',
	sk => 'cz-hu-pl-md-at-de',
	sn => 'gn-ng-pt-es-tn-za-ug-ke-tz-bw-zw-gb',
	so => 'et-ke-tz-ug-rw-cd-mz-eg-il-tr',
	sv => 'hn-gt-ni-mx-cr-pa-co-us',
	sy => 'il-lb-jo-tr-iq-sa-eg-gr-bg-ro',
	td => 'ng-ne-ml-gh-gn-sn-ug-ke-tn-tz-zw-bw-it-za-es',
	tg => 'gh-gn-ng-ne-sn-za-ug-ke-tz-bw-zw-gb',
	th => 'cn-vn-my-sg-ph-hk',
	tj => 'uz-kg-kz-tm-af-pk-in-ru-cn-az-mn',
	tm => 'uz-kz-az-ge-tj-am-ru-af-tr-in',
	tn => 'dz-ly-it-eg-mt-fr-es',
	tr => 'gr-it-de-bg-ro',
	tw => 'cn-hk-kr-sg-jp',
	tz => 'ke-ug-zw-bw-za-eg-tn-tr-it-fr',
	ua => 'pl-ro-cz-de-md',
	ug => 'ke-tz-zw-bw-za-eg-tn',
	us => '',
	uy => 'ar-br-py-cl-bo-pe-ec-co-ve-gf-us',
	uz => 'kz-tm-az-tj-ru-ge-kg-cn-af-pk-in',
	ve => 'co-ec-gf-pe-br-us',
	vn => 'la-th-ph-sg-my-hk-cn',
	ye => 'sa-om-ae-qa-bh-kw-iq-il-it-de',
	za => 'bw-zw-tz-ke-ug-ng-pt-es-in',
	zm => 'zw-bw-za-ao-tz-cd-ke-ug',
	zw => 'za-bw-mz-tz-ke-ng-mg-eg-il-tr-es-in'
);


### get_cc_list - returns an array of country code listings in order to
### extract from database.

sub get_cc_list {

my $country=$_[0];
my $cclist;


$cclist .= $country;
$cclist .= "-";

if (exists $ccgroups{$country})
	{
	$cclist .=  $ccgroups{$country};
	}

my @ccs = split('-',$cclist);

return @ccs;

}

### get_all_ccs - returns an array of all counry codes we have separate
### mirrorlist files for 

sub get_all_ccs {

my @ccs;
my $key;


foreach $key( keys(%ccgroups))
	{
	push(@ccs,$key);
	}

return @ccs;
}	


### get_cc_cont - returns the continent each cc is in - used for mapping
### centos mirrors

sub get_cc_cont {

my $cc = $_[0];

my %cccont = (

		ad => 'eu',
		af => 'ap',
		ae => 'af',
		al => 'eu',
		am => 'eu',
		ar => 'sa',
                at => 'eu',
                au => 'oc',
                az => 'eu',
                ba => 'eu',
                bd => 'ap',
                be => 'eu',
                bg => 'eu',
		bn => 'ap',
		bo => 'sa',
                br => 'sa',
		bs => 'us',
		bw => 'af',
		by => 'eu',
                c1 => 'c1',
                c2 => 'eu',
                c3 => 'ap',
                c4 => 'us',
                ca => 'us',
		cd => 'af',
                ch => 'eu',
                cl => 'sa',
                co => 'sa',
		cm => 'af',
                cn => 'ap',
		cr => 'sa',
		cy => 'eu',
                cz => 'eu',
                de => 'eu',
                dk => 'eu',
		do => 'us',
		dz => 'af',
                ec => 'sa',
		ee => 'eu',
		eg => 'af',
                es => 'eu',
		et => 'af',
                fi => 'eu',
                fr => 'eu',
                gb => 'eu',
                ge => 'eu',
		gf => 'sa',
		gh => 'af',
                gl => 'us',
		gn => 'af',
                gr => 'eu',
		gt => 'sa',
		hk => 'ap',
		hn => 'sa',
                hu => 'eu',
                hr => 'eu',
		id => 'ap',
                ie => 'eu',
                il => 'ap',
                in => 'ap',
		iq => 'ap',
		ir => 'ap',
                is => 'eu',
                it => 'eu',
		jo => 'af',
                jp => 'ap',
		ke => 'af',
		kg => 'ap',
		kh => 'ap',
                kr => 'ap',
		kw => 'ap',
                kz => 'ap',
		la => 'ap',
		lb => 'af',
                lk => 'ap',
                lt => 'eu',
                lu => 'eu',
		lv => 'eu',
		ly => 'af',
		ma => 'af',
		mc => 'eu',
                md => 'eu',
		me => 'eu',
		mg => 'af',
                mk => 'eu',
		ml => 'af',
		mm => 'ap',
                mn => 'ap',
		mt => 'eu',
                mx => 'us',
		my => 'ap',
		mz => 'af',
		na => 'af',
		nc => 'oc',
		ne => 'af',
		ng => 'af',
		ni => 'af',
                nl => 'eu',
                no => 'eu',
		np => 'ap',
                nz => 'oc',
                om => 'ap',
                pa => 'sa',
                pe => 'sa',
                ph => 'ap',
		pk => 'ap',
                pl => 'eu',
                pr => 'us',
                pt => 'eu',
		py => 'sa',
		qa => 'af',
                ro => 'eu',
                rs => 'eu',
                ru => 'eu',
		rw => 'af',
                sa => 'ap',
                se => 'eu',
                sg => 'ap',
                si => 'eu',
                sk => 'eu',
		sn => 'af',
		so => 'af',
		sv => 'sa',
		sy => 'eu',
		td => 'af',
		tg => 'af',
		th => 'ap',
		tj => 'ap',
		tm => 'ap',
		tn => 'af',
                tr => 'eu',
                tw => 'ap',
		tz => 'af',
                ua => 'eu',
		ug => 'af',
                us => 'us',
		uy => 'sa',
		uz => 'ap',
		ve => 'sa',
                vn => 'ap',
		ye => 'af',
                za => 'af',
		zm => 'af',
		zw => 'af'
		);

return $cccont{$cc};

}


my %cchash=(
        A2 => "C1",
        AD => "ES",
        AE => "IL",
        AF => "IN",
        AG => "US",
        AI => "US",
        AL => "GR",
        AM => "AM",
        AN => "US",
        AO => "ZA",
        AP => "C3",
        AQ => "C1",
        AR => "AR",
        AS => "AU",
        AT => "AT",
        AU => "AU",
        AW => "CR",
        AZ => "AZ",
        BA => "BA",
        BB => "BR",
        BD => "BD",
        BE => "BE",
        BF => "ZA",
        BG => "BG",
        BH => "IL",
        BI => "ZA",
        BJ => "ZA",
        BM => "US",
        BN => "IL",
        BO => "CL",
        BR => "BR",
        BS => "US",
        BT => "CN",
        BV => "ZA",
        BW => "ZA",
        BY => "BY",
        BZ => "CR",
        CA => "CA",
        CD => "ZA",
        CF => "ZA",
        CG => "ZA",
        CH => "CH",
        CI => "ZA",
        CK => "NZ",
        CL => "CL",
        CM => "ZA",
        CN => "CN",
        CO => "CO",
        CR => "CR",
        CS => "SI",
        CU => "PR",
        CV => "ES",
        CY => "GR",
        CZ => "CZ",
        DE => "DE",
        DJ => "IL",
        DK => "DK",
        DM => "PR",
        DO => "PR",
        DZ => "IL",
        EC => "EC",
        EE => "EE",
        EG => "EG",
        ER => "IL",
        ES => "ES",
        ET => "IL",
        EU => "C2",
        FI => "FI",
        FJ => "AU",
        FK => "AR",
        FM => "PH",
        FO => "NO",
        FR => "FR",
        GA => "ZA",
        GB => "GB",
        GD => "PR",
        GE => "GE",
        GF => "BR",
        GH => "ZA",
        GI => "ES",
        GL => "GL",
        GM => "ZA",
        GN => "ZA",
        GP => "PR",
        GQ => "ZA",
        GR => "GR",
        GT => "CR",
        GU => "SG",
        GW => "ZA",
        GY => "BR",
        HK => "HK",
        HM => "AU",
        HN => "CR",
        HR => "HR",
        HT => "PR",
        HU => "HU",
        ID => "ID",
        IE => "IE",
        IL => "IL",
        IN => "IN",
        IO => "SG",
        IQ => "IQ",
        IR => "TR",
        IS => "IS",
        IT => "IT",
        JM => "PR",
        JO => "IL",
        JP => "JP",
        KE => "KE",
        KG => "CN",
        KH => "KH",
        KI => "C1",
        KM => "ZA",
        KN => "PR",
        KP => "KR",
        KR => "KR",
        KW => "KW",
        KY => "PR",
        KZ => "KZ",
        LA => "CN",
        LB => "IL",
        LC => "PR",
        LI => "CH",
        LK => "LK",
        LR => "ZA",
        LS => "ZA",
        LT => "LT",
        LU => "LU",
        LV => "LV",
        LY => "IT",
        MA => "ES",
        MC => "FR",
        MD => "MD",
        MG => "ZA",
        MH => "AU",
        MK => "MK",
        ML => "ES",
        MM => "IN",
        MN => "MN",
        MO => "HK",
        MP => "PH",
        MQ => "PR",
        MR => "ES",
        MS => "PR",
        MT => "IT",
        MU => "ZA",
        MV => "IN",
        MW => "ZA",
        MX => "MX",
        MY => "MY",
        MZ => "ZA",
        NA => "ZA",
        NC => "NC",
        NE => "ZA",
        NF => "AU",
        NG => "NG",
        NI => "CR",
        NL => "NL",
        NO => "NO",
        NP => "IN",
        NR => "AU",
        NU => "AU",
        NZ => "NZ",
        OM => "OM",
        PA => "PA",
        PE => "PE",
        PF => "AU",
        PG => "AU",
        PH => "PH",
        PK => "PK",
        PL => "PL",
        PR => "PR",
        PS => "IL",
        PT => "PT",
        PW => "PH",
        PY => "CL",
        QA => "IL",
        RE => "ZA",
        RO => "RO",
        RS => "RS",
        RU => "RU",
        RW => "ZA",
        SA => "SA",
        SB => "AU",
        SC => "IN",
        SD => "IL",
        SE => "SE",
        SG => "SG",
        SI => "SI",
        SK => "SK",
        SL => "ZA",
        SM => "IT",
        SN => "ZA",
        SO => "ZA",
        SR => "BR",
        ST => "ZA",
        SV => "US",
        SY => "IL",
        SZ => "ZA",
        TC => "PR",
        TD => "ZA",
        TF => "ZA",
        TG => "ZA",
        TH => "TH",
        TJ => "KZ",
        TK => "AU",
        TM => "KZ",
        TN => "IT",
        TO => "AU",
        TR => "TR",
        TT => "BR",
        TV => "AU",
        TW => "TW",
        TZ => "TZ",
        UA => "UA",
        UG => "UG",
        UM => "US",
        US => "US",
        UY => "CL",
        UZ => "UZ",
        VA => "IT",
        VC => "PR",
        VE => "CL",
        VG => "PR",
        VI => "PR",
        VN => "VN",
        VU => "AU",
        WF => "AU",
        WS => "AU",
        YE => "IL",
        YT => "ZA",
        ZA => "ZA",
        ZM => "ZA",
        ZW => "ZW",
	);

sub cc_hash {
	my $cc = $_[0];

	if (exists ($cchash{$cc}))
		{
		return $cchash{$cc};
		}
	else
		{
		return "C1";
		}
	}

our %country_subregions = (
	AB => "ca",
	BC => "ca",
	MB => "ca",
	NB => "ca",
	NL => "ca",
	NS => "ca",
	NT => "ca",
	NU => "ca",
	ON => "ca",
	PE => "ca",
	QC => "ca",
	SK => "ca",
	YT => "ca",

	AK => "us",
	AL => "us",
	AR => "us",
	AZ => "us",
	CA => "us",
	CO => "us",
	CT => "us",
	DC => "us",
	DE => "us",
	FL => "us",
	GA => "us",
	HI => "us",
	IA => "us",
	ID => "us",
	IL => "us",
	IN => "us",
	KS => "us",
	KY => "us",
	LA => "us",
	MA => "us",
	MD => "us",
	ME => "us",
	MI => "us",
	MN => "us",
	MO => "us",
	MS => "us",
	MT => "us",
	NC => "us",
	ND => "us",
	NE => "us",
	NH => "us",
	NJ => "us",
	NM => "us",
	NV => "us",
	NY => "us",
	OH => "us",
	OK => "us",
	OR => "us",
	PA => "us",
	RI => "us",
	SC => "us",
	SD => "us",
	TN => "us",
	TX => "us",
	UT => "us",
	VA => "us",
	VT => "us",
	WA => "us",
	WI => "us",
	WV => "us",
	WY => "us"
);
