#!/usr/bin/perl -w
use strict;
use File::Basename;
use Getopt::Std;
my $PROGRAM = basename $0;
my $USAGE=
"Usage: cat CLUSTER_LINK | $PROGRAM -i CLUSTER -o SUB_DIR
";

use DomRefine::Read;
use DomRefine::General;
use DomRefine::Score;
use DomRefine::Refine;

### Settings ###
my %OPT;
getopts('i:o:m:', \%OPT);

my $CLUSTER;
if ($OPT{i}) {
    $CLUSTER = "$OPT{i}";
} else {
    die;
}
my $SUB_DIR;
if ($OPT{o}) {
    $SUB_DIR = "$OPT{o}";
} else {
    die;
}
my $MARK = $OPT{m} || "";

my $TMP_OUTPUT = define_tmp_file("$PROGRAM.output");
END {
    remove_tmp_file($TMP_OUTPUT);
}

system "cp $CLUSTER $TMP_OUTPUT";

### Main ###
my %MODIFIED = ();
my $count = 0;
-t and die $USAGE;
while (<STDIN>) {
    chomp;
    my ($cluster_set) = split;
    my @cluster = split(/[-,\s]/, $cluster_set);
    if (@cluster != 2) {
	die;
    }

    $count ++;
    print STDERR "($count) $_\n";
    if (check_if_cluster_modified(@cluster)) {
	next;
    }
    if (check_if_cluster_not_exist(@cluster)) {
	next;
    }

    ### replace ###
    my $dclst_new = `cat $SUB_DIR/$cluster_set.out`;
    $dclst_new =~ s/^(\S+) /${1}$MARK /gm;
    my $dclst_others = extract_dclst_compl($TMP_OUTPUT, @cluster);
    save_contents($dclst_others . $dclst_new, $TMP_OUTPUT);

    check_cluster_modified(@cluster);
    print_modification_log(@cluster);
}

system "cat $TMP_OUTPUT";

################################################################################
### Functions ##################################################################
################################################################################
sub print_modification_log {
    my @cluster = @_;

    my @modified = ();
    for (my $i=0; $i<@cluster; $i++) {
	$modified[$i] = "$cluster[$i]$MARK";
    }
    print STDERR join("-", @cluster), "\tmodified_to\t", join("-", @modified), "\n";
}

sub check_cluster_modified {
    my @cluster = @_;

    for (my $i=0; $i<@cluster; $i++) {
	$MODIFIED{$cluster[$i]} = 1;
    }
}

sub check_if_cluster_modified {
    my @cluster = @_;

    for (my $i=0; $i<@cluster; $i++) {
	if ($MODIFIED{$cluster[$i]}) {
	    print STDERR "$cluster[$i] is already changed\n";
	    return 1;
	}
    }

    return 0;
}

sub check_if_cluster_not_exist {
    my @cluster = @_;

    for (my $i=0; $i<@cluster; $i++) {
	my $dclst = extract_dclst($TMP_OUTPUT, $cluster[$i]);
	if ($dclst eq "") {
	    print STDERR "$cluster[$i] do not exist\n";
	    return 1;
	}
    }

    return 0;
}
