#!/usr/bin/perl -w

#
#  stable_hitlists_red
#  Copyright (C) 2010 by USC/ISI
#  $Id$
#  
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License,
#  version 2, as published by the Free Software Foundation.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#  
#  
#  This code was originally written by Xun Fan <xunfan@isi.edu>.
#

use Fsdb::IO::Reader;
use Fsdb::IO::Writer;

#my $input_stream = new Fsdb::IO::Reader(-fh => \*STDIN, -fscode => 't');

# my $length = 8; # The history length

my $output_stream = new Fsdb::IO::Writer(-fh => \*STDOUT, -fscode => 't', -cols => [qw(prefix hex_ip score ip)]);
my $current_ip;
my $current_score = 0;
my $current_prefix;
#my $cuthis;
#my $score;
my $str_ip;
my $count;
#my $c_prefix_mask = ~(2**8 - 1);

#my $power_sum_16 = 3.380729;
#use Bit::Vector;
#use Bit::Vector::String;

sub hexiptoip{
	my ($hexip) = @_;
	my $ip1 = hex(substr($hexip, 0, 2));
	my $ip2 = hex(substr($hexip, 2, 2));
	my $ip3 = hex(substr($hexip, 4, 2));
	my $ip4 = hex(substr($hexip, 6, 2));
	return "$ip1.$ip2.$ip3.$ip4";
}

while(<>){
	my($prefix, $hex_ip, $score, $ip) = split;
	last if (!defined($prefix)); # EOF
	if (!defined($current_prefix)){
		$current_ip = $hex_ip;
		$current_score = $score;
		$current_prefix = $prefix;
		$count = 1;
	}else{
		if($current_prefix eq $prefix){  # same prefix exists both in new and old hitlists
			$count = 2; # used to distinguish between overlap record and single record
			if ($ip eq "0"){    # this line of record belongs to old hitlists
				# If the new score is 34 greater than old score, than we switch to new record
				if ($current_score - $score >= 34){
					$str_ip = hexiptoip($current_ip);
					$output_stream->write_row($current_prefix, $current_ip, $current_score, $str_ip);
				}
				else{
					$str_ip = hexiptoip($hex_ip);
					$output_stream->write_row($prefix, $hex_ip, $score, $str_ip);
				}
			}
			else{   # this line belongs to new hitlists
				if ($score - $current_score >= 34){
					$str_ip = hexiptoip($hex_ip);
					$output_stream->write_row($prefix, $hex_ip, $score, $str_ip);
				}
				else{
					$str_ip = hexiptoip($current_ip);
					$output_stream->write_row($current_prefix, $current_ip, $current_score, $str_ip);
				}
			}
		}else{
			if ($count == 1){
				$str_ip = hexiptoip($current_ip);
				$output_stream->write_row($current_prefix, $current_ip, $current_score, $str_ip);
				$current_ip = $hex_ip;
				$current_score = $score;
				$current_prefix = $prefix;
			}
			else{
				$current_ip = $hex_ip;
				$current_score = $score;
				$current_prefix = $prefix;
				$count = 1;
			}
		}
	}
}

if ($count == 1){
	$str_ip = hexiptoip($current_ip);
	$output_stream->write_row($current_prefix, $current_ip, $current_score, $str_ip);
}
