#!/usr/bin/tclsh
if {$argc < 9} {
    puts "Usage: rnets <nodes file> <inp> <path> <sqrt(no subnet/node)> <no_conn/link> <time> <seed> <decr> <port>"
        exit
}

set cont 0
if {$argc > 9} {set cont [lindex $argv 9]}
exec touch start

set name [lindex $argv 0]
set inp [lindex $argv 1] 
set path [lindex $argv 2]   
set ns [lindex $argv 3]
set ncon [lindex $argv 4]
set tim [lindex $argv 5]
set seed [lindex $argv 6]
set decr [lindex $argv 7]
set port [lindex $argv 8]
set delay 0.3
set f [open $name r]

gets $f nproc


exec cp $name tmp.nodes.tab


for {set i 0} {$i<$nproc} {incr i 1} {
	gets $f node
	lappend nodelist $node
}
close $f

set f [open "$inp.cfg" r]
gets $f line
close $f
set flags [split $line ,]
set hist [lsearch $flags H]
set out  [lsearch $flags O]
set vout [lsearch $flags V]
set link [lsearch $flags L]
set stim [lsearch $flags S]

if {($hist==-1) && ($out==-1) && ($vout==-1)} {
	puts { no output generated }
	exit
} 
set proc [expr sqrt($nproc)]


set filelist1 {par syn neu}
if {$link>-1} {
	set filelist1 "$filelist1 lin lout"
}
#exec rm -f tmp.link 
if {$stim>-1} {
	set filelist1 "$filelist1 stim"
}

set filelist2  "$filelist1 cfg"


if {$cont < 1} {
    puts  "$filelist2"
puts "mknode $inp tmp $ns $proc $seed" 
exec ./mknode $inp tmp $ns $proc $seed >rnet.log
puts "mklink $inp [expr $proc*$ns] $seed" 
exec ./mklink $inp [expr $proc*$ns] $seed >>rnet.log
puts "sublink tmp $ncon $delay $seed" 
exec ./sublink tmp $ncon $delay $seed >>rnet.log
puts  "crl tmp $nproc" 
exec ./crl tmp $nproc >>rnet.log



puts copy
      
for {set i 0} {$i<$nproc} {incr i 1} {
	set node [lindex $nodelist $i]
	puts $node
        exec rsh -n $node rm -rf $path
        exec rsh -n $node mkdir $path
	exec rcp ./tmp.conn.tab "$node:$path/"
	exec rcp ./tmp.nodes.tab "$node:$path/"
	exec rcp ./netclustwacnmdadiff "$node:$path/"
	foreach ext $filelist2 {
		exec rcp "./tmp.$ext.$i" "$node:$path/"
	      	}
	foreach ext $filelist1 {
		exec rcp "./tmp.$ext.$i" "$node:$path/"
	        exec rm -f "./tmp.$ext.$i"
		}
}

#exec rm -f  ./tmp.nodes.tab ./tmp.conn.tab


}
for {set i 0} {$i<$nproc} {incr i 1} {
        set node [lindex $nodelist $i]
 
    while { ![catch { exec rsh -n $node netstat -n -A inet | grep $port } ]} {
        puts "...waiting for port in $node"
        after 1000
        }
}


puts run
exec touch run
for {set i 0} {$i<$nproc-1} {incr i 1} {
	set node [lindex $nodelist $i]
	puts $node

#	exec rsh -n $node "$path/netclustwacnmdadiff" tmp $tim $decr & 
       
	exec rsh -n $node /bin/nice -n3 "$path/netclustwacnmdadiff" tmp $tim $decr $port & 

}
set node [lindex $nodelist $i]
puts $node
exec rsh -n $node /bin/nice -n3  "$path/netclustwacnmdadiff" tmp $tim  $decr $port >@stdout  
#exec rsh -n $node "$path/netclustwacnmdadiff" tmp $tim  $decr >@stdout  
exec touch end
puts "computation time: [expr [file mtime end]-[file mtime run]] sec"

for {set i 0} {$i<$nproc} {incr i 1} {
        set node [lindex $nodelist $i]
    while {  ![catch { exec rsh -n $node stat $tmp/done } ]} {
        puts "...waiting for $node"
        after 5000
        }
}

puts copyout
set f [open "nclust.log" w]
for {set i 0} {$i<$nproc} {incr i 1} {
	set node [lindex $nodelist $i]
	puts $node
        exec rcp -r "$node:$path/" .
	exec rsh -n $node cat netclust.log >>nclust.log
}

#foreach ext $filelist2 {
#                exec rm -f "$path/tmp.$ext.$i"
#                }



if {$hist >-1} {
puts  "jhist tmp $inp" 
exec ./jhist tmp $inp >>rnet.log
}
puts  "mkhist histo_$inp tmp.node 1"
exec	./mkhist histo_$inp tmp.node 1 >>rnet.log
#if {$vout >-1} {
#exec ./jvout1 tmp $inp $proc >>rnet.log
#}
#exec ./linknet $inp tmp $proc >>rnet.log
puts clean
#eval exec rm -f [glob "./tmp.*.*"]    
	if {$hist>-1} {
#	    eval exec rm -f [ glob "./histo_tmp.*"]
	}
	#if {$vout>-1} {
	#   eval exec rm -f [glob "./V_tmp.out.*"]
	#}

exec touch end
puts "total time: [expr [file mtime end]-[file mtime start]] sec"