#!/usr/bin/perl -w

my $nw1_pajek = $ARGV[0];
my $nw2_pajek = $ARGV[1];
my $pajekFile = $ARGV[2];

@nw1_nodes = &readNW_nodes($nw1_pajek);
print "NW1: @nw1_nodes\n";

@nw2_nodes = &readNW_nodes($nw2_pajek);
print "NW2: @nw2_nodes\n";

&find_comm_and_diff_nodes();
print "Intersect: @intersect\n";
print "Diff : @diff\n";

@nw1_labels = &find_nw_labels($nw1_pajek);
$nw1_labels_cnt = @nw1_labels;
print "NW1: Nodes: $nw1_labels_cnt\n";

@nw2_labels = &find_nw_labels($nw2_pajek);
$nw2_labels_cnt = @nw2_labels;

@new_nw2_labels = ();
foreach(@nw2_labels) {
    chomp($_);
    my ($f, $s) = split(" ", $_);
    my $num = $f + $nw1_labels_cnt;
    my $new_lbl = "$num $s";
    push(@new_nw2_labels, $new_lbl);
}

print ">> @nw1_labels\n";
print ">> @new_nw2_labels\n";


foreach $inst(@intersect) {
    $new_edge = "";
    foreach(@nw1_labels) {
	my ($f, $s) = split(" ", $_);
	$s =~s/^\s+//;
	$s =~s/\s$//;

	if ($s=~/\b$inst\b/) {
	    $new_edge = "$f ";
	    last;
	}
    }

    foreach(@new_nw2_labels) {
	my ($f, $s) = split(" ", $_);
	$s =~s/^\s+//;
	$s =~s/\s$//;

	if ($s=~/\b$inst\b/) {
	    $new_edge .= "$f";
	    last;
	}
    }
    
    print "NEW EDGE: $new_edge\n";
    push(@new_edges, $new_edge);

}

@nw1_arcs = &readNW_arcs($nw1_pajek);
print "-- @nw1_arcs\n";
@nw2_arcs = &readNW_arcs($nw2_pajek);
print "-- @nw2_arcs\n";

&print_pajekFile();



############## Sub-routines ###################
sub print_pajekFile() {
 
    $ver_ctr = $nw1_labels_cnt + $nw2_labels_cnt;
    
    open(OUT, ">$pajekFile");
    print OUT"*Vertices $ver_ctr\n";
    foreach(@nw1_labels) {
        print OUT"$_ ic Orange bc Mahogany\n";
    }

    foreach(@new_nw2_labels) {
	print OUT"$_  ic Mahogany bc Orange\n";
    }

    print OUT"*Arcs\n";
    foreach(@nw1_arcs) {
        chomp($_);
        print OUT"$_  1 c Green\n";
     } 

    foreach(@nw2_arcs) {
        chomp($_);
        my ($f, $s) = split(" ", $_);
        $f = $f + $nw1_labels_cnt;
        $s = $s + $nw1_labels_cnt;
        print OUT"$f $s 1 c Red\n";
    }

    foreach(@new_edges) {
         chomp($_);
          print OUT"$_ 1 c Black\n";
    }
    close(OUT);
  

}

sub readNW_arcs() {
    my $file = $_[0];
    print "><FileName: $file\n";

    my @arcs = ();
    my $flag = 0;
    open(IN, $file);
    while(<IN>) {
        chomp($_);
        if ($_ =~/Arcs/) {
            $flag = 1;
        }
	
        if ($flag) {
            push(@arcs, $_);
        }
    }
    close(IN);
    shift(@arcs);
    return(@arcs);
}




sub find_nw_labels() {
    my $file = $_[0];
    print "--> FileName: $file\n";
    
    my @nodes = ();
    open(IN, $file);
    while(<IN>) {
        chomp($_);
        if ($_ =~/Arcs/) {
            last;
        }else {
	    push(@nodes, $_);
	}
    }
    close(IN);
    shift(@nodes);
    return(@nodes);
}


sub find_comm_and_diff_nodes() {
    foreach $e(@nw1_nodes, @nw2_nodes) { $count{$e}++ }
    foreach $e (keys %count) {
	if ($count{$e} == 2) {
	    push (@intersect, $e);
	} else {
	    push (@diff, $e);
	}
    }
}


sub readNW_nodes() {
    my $file = $_[0];
    print "><FileName: $file\n";
    
    my @nodes = ();
    open(IN, $file);
    while(<IN>) {
        chomp($_);
        if ($_ =~/Arcs/) {
            last;
        }else {
	    my ($f, $s) = split(" ", $_);
	    $s =~s/\"//g;
	    $s =~s/^\s+//;
	    $s =~s/\s$//;
	    push(@nodes, $s);
	}
    }
    close(IN);
    shift(@nodes);
    return(@nodes);
}
