package Geo::IP::Reverse;

use 5.018002;
use strict;
use warnings;

require Exporter;
require Text::CSV_XS;

our @ISA = qw(Exporter);

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration	use Geo::IP::Reverse ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
	
) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
	reverse
);

our $VERSION = '0.01';


# Preloaded methods go here.
sub reverse {
	my $csvFile;
	my $postalCode;
	
	my @output;

	if ($ARGV[0] =~ /^[\d-]+$/) { #if zip code
		$postalCode = $ARGV[0];
		$csvFile = "GeoLite2-City-Blocks-IPv4.csv";
	}

	else { #then argv[1]
		$csvFile = $ARGV[0]; #THIS SHOULD BE THE BLOCK FILE
		$postalCode = $ARGV[1];
	}

	
	my $startTime = time;


	#straight from meta cpan

	my $csv = Text::CSV_XS->new ( { binary => 1 } )  # should set binary attribute.
			or die "Cannot use CSV: ".Text::CSV_XS->error_diag ();

	open my $fh, "<$csvFile" or die $!;

	#printing ip array if matching postalCode


	my $resultCount = 0;
	while ( my $row = $csv->getline( $fh ) ) {
		$row->[6] =~ /$postalCode/ or next; # 3rd field should match
		push ( @output, $row->[0] );
		$resultCount++;
	}
	$csv->eof or $csv->error_diag();
	close $fh;
	return \@output;
}

1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

Geo::IP::Reverse - Perl extension for 

=head1 SYNOPSIS

  use Geo::IP::Reverse;
  blah blah blah

=head1 DESCRIPTION

Stub documentation for Geo::IP::Reverse, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.

Blah blah blah.

=head2 EXPORT

None by default.



=head1 SEE ALSO

Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.

If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.

=head1 AUTHOR

John Kennedy, E<lt>john@apple.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2015 by John Kennedy

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.18.2 or,
at your option, any later version of Perl 5 you may have available.


=cut
