#!/sat-tools/bin/expect  <<< replace

proc  zconnect {}  {
 
    global user_prompt
    global ID

    eval "spawn rsh snowblind" << replace with machine you can run rsh
    set ID $spawn_id
    expect {
   	timeout {
            exit -1
	}
	eof {
	    puts "received an EOF?"
	    return -1
	}
       "$user_prompt" {
           puts " got prompt"
           return 0
       }      
    }

}  


proc send_cmd {} {
    global full_cmd
    global user_prompt
    global ID
    global start_time
    global lines

    send -i $ID "$full_cmd\r"
    puts "sending command"
    set COUNT 1
    set XCOUNT 0
    set TBYTES 0
    set current_time [clock seconds]
    #exp_internal 1
   # exp_internal -f $LOG 0 
       
     while 1 {    
       expect {
           -i $ID -re "(\[^\r]*)(\r\r\n|\r\n)" {
           set TBYTES [expr $TBYTES + [string length $expect_out(1,string)]]
           set CBYTES [string length $expect_out(1,string)]
      #     puts ">>$expect_out(1,string)"
           append lines "$expect_out(1,string)\n"
      #     puts "COUNT=$COUNT, TBYTES=$TBYTES"
      #     set run_time [expr $current_time - $start_time ]
      #     set hours [expr $run_time / 3600 ]
      #     set tmp [expr $run_time % 3600 ]
      #     set minutes [expr $tmp / 60 ]
      #     set tmp [ expr $tmp % 60 ]
   #        puts "runtime sec: $run_time or $hours:$minutes:$tmp h:m:s"
           incr COUNT
           incr XCOUNT
           if { $XCOUNT > 1000 } { 
              puts "*"
              set XCOUNT 0
           }
           continue
        }
        "$user_prompt" {
            puts "prompt"
            break
        }
        eof {
           puts "\n >>> hit eof"
           exit -1
        }

        full_buffer {
           puts "\n full buffer hit"
           exit -1
        }
     } 
   }   
} 
 
##
##
##  Main
##
##
 
set user_prompt %%%           <<< set prompt for rsh user
set LOG "ztest.log"
set lines ""
#set full_cmd "ypcat hosts"
set CURRENT_SPAWN [match_max]
set DZZ [match_max -d]
set sleepy_time_time 3

puts "CURRENT $CURRENT_SPAWN New $DZZ"
sleep 5


set full_cmd "cat /tmp/zypcat.out"   <<<< replace with big file at least 150,000 bytes
#set full_cmd "ls"

#set MAX [expr 4096 * 14] 
set MAX 2000                << this is the default

# 4096 
# 8192 
# 12288
# 20480
# 24276 57344

match_max -d  $MAX
match_max   $MAX 

set CURRENT_SPAWN [match_max]
set DZZ [match_max -d]
puts "CURRENT $CURRENT_SPAWN New $DZZ"
sleep 5
set ID 0

set YPCAT_COUNT 1
set start_time [clock seconds]

if { [zconnect] == 0 } {
    while {1} {
        puts "main while loop"
        
        send_cmd
        
        set current_time [clock seconds]
        set run_time [expr $current_time - $start_time ]
        set hours [expr $run_time / 3600 ]
        set tmp [expr $run_time % 3600 ]
        set minutes [expr $tmp / 60 ]
        set tmp [ expr $tmp % 60 ]
        puts "runtime sec: $run_time or $hours:$minutes:$tmp h:m:s"
        puts "sleeping $sleepy_time_time seconds"
        set DZZ [match_max -d]
        set CURRENT_SPAWN [match_max]
        puts "CURRENT $CURRENT_SPAWN New $DZZ YPCAT_COUNT $YPCAT_COUNT"
        incr YPCAT_COUNT
        set lines ""
        sleep $sleepy_time_time
    }
} else {
  puts "zconnect failed"
}
