#!/usr/bin/perl -w
use strict;

my $bndFile=shift;
if (!$bndFile) {printf "Missing bnd file, OutProb2InitCond.pl <file.bnd> <filetraj.csv> <linenumber>\n";exit;}
my $probtrajFile=shift;
if (!$probtrajFile) {printf "Missing bnd file, OutProb2InitCond.pl <file.bnd> <filetraj.csv> <linenumber>\n";exit;}
my $linenumber=shift;
if (!$linenumber) {printf "Missing bnd file, OutProb2InitCond.pl <file.bnd> <filetraj.csv> <linenumber>\n";exit;}

open(BND,$bndFile) or die "cannot find .bnd file";
my @NodeList;
while(<BND>)
{
    if ( (/node/) || (/Node/))
    {
	s/node//;
	s/Node//;
	s/\{.*//;
	s/\s+//g;
	@NodeList=(@NodeList,$_);
    }
}
close(BND);
open(TRJF,$probtrajFile) or die "cannot find trajectory file";
for (my $i=0;$i <= $linenumber;$i++){$_=<TRJF> or die "traj file is empty";} #header of probtraj file is not consider
close(TRJF);
my @LineList=split(/\t/);

my $LineInitCond="[".$NodeList[0];
foreach (@NodeList[1..$#NodeList]){$LineInitCond=$LineInitCond.",".$_;}
$LineInitCond=$LineInitCond."].istate =";
for (my $index=0;$index<=$#LineList;$index++) 
{
    $_=$LineList[$index];  
#    if (/[A-Z,a-z]/) 
    if ((/^[A-Z,a-z]/) || (/nil/))
    {
	$LineInitCond=$LineInitCond.$LineList[$index+1]." [";
#	if (/$NodeList[0]/) {$LineInitCond=$LineInitCond."1";}
	if (($_ eq $NodeList[0]) || /^$NodeList[0] / || / $NodeList[0] / || / $NodeList[0]$/) 
	{$LineInitCond=$LineInitCond."1";}

	else {$LineInitCond=$LineInitCond."0";}
	foreach my $Node (@NodeList[1..$#NodeList])
	{
	 $LineInitCond=$LineInitCond.",";
	 if (($_ eq $Node) || /^$Node / || / $Node / || / $Node$/) {$LineInitCond=$LineInitCond."1";}
	else {$LineInitCond=$LineInitCond."0";}
	}
	$LineInitCond=$LineInitCond."] , ";	
    }
}
chop($LineInitCond);
chop($LineInitCond);
$LineInitCond=$LineInitCond.";\n";
printf $LineInitCond;
