#!/usr/local/bin/perl -w
#
$l = 0 ; $r = 20 ; $start = 10 ; 
$T = 1200 ;
$t = 1 ; # initial time 
$plot = 0 ;  # attempt to plot directly via stdout piped to gnuplot  
$plotviafile = 1 ; # issue a list of commands to a file for later gnuplot
$period = 20 ; 
$size = "0.8" ; 
$pause = 1 ; 
$file = "hist" ; 
$gnucom = "gnucom" ; 
$nokey = 0 ; 
$maxnuminplot = 7 ; # how many graphs to plot simultanesouly
# $colourcycle = 7 ; # how many colours to cycle through
#
$pbmplot = 0 ; 
$pbmfile = "Aps.pgm" ; 
$screen = 0 ; 
$pbmy = 599 ; 
$barperiod = 50 ; # pbm figure has horizontal bars to mark time. 
$seed = 0 ;
#
#
# usage  demo.p | gnuplot 
# ok, run separate gnuplot
#  How to override min and max values with command line
eval "\$$1=\$2" while $ARGV[0]=~ /^(\w+)=(.*)/ && shift;
if ( $seed ) {srand ( $seed ) ; }
#
if ( $pbmplot ) { # note I actually write a pgm file .
    if ( $screen ) {
	$border = 2 ; 
	$track = 3 ; 
	$backgr = 0 ; 
	$maxpbm = 3 ; 
	$bars = 1 ;
    } # good choices for screen, giving white on black
    else {
	$border = 1 ; 
	$track = 0 ; 
	$backgr = 3 ; 
	$maxpbm = 3 ; 
	$bars = 2 ;
    } 
    open (PBM,"> $pbmfile");
    $pbmx = $r-$l+1 + 2 ; # 2 for border 
    if ( $T < $pbmy ) {$pbmy =  $T  ; }
    $pbmy += 2 ; # border 
    print PBM "P2\n# CREATOR: demo.p DJCM 97 02 15\n" ;
    print PBM "$pbmx $pbmy\n" ; 
    print PBM "$maxpbm\n" ; 
# top line 
	print PBM  "$border " ; 
	for ( $i = $l ; $i <= $r ; $i ++ ) {	 print PBM  "$border " ;  }
	print PBM  "$border " ; 
	print PBM  "\n" ;
}

# $firsttime = 1 ; # whether it's the first time of plotting
$numberinplot = 0 ; 

if ( $plotviafile ) {
    open ( GNUCOM , "> $gnucom" ) ; 
#    print GNUCOM "set term X\n" ; 
    if ( $size ) { print GNUCOM "set size $size,$size\n" ; }
#     print GNUCOM "set yrange [0:]\n" ; 
    if ( $nokey ) {
	print GNUCOM "set nokey\n" ; 
	print GNUCOM "set xrange [-0.5:20.5]\n" ; 
    } else {
	print GNUCOM "set xrange [-0.5:23.5]\n" ; 
    }
#   print GNUCOM "plot '$file' u 1:2 w boxes\n" ; 
   print GNUCOM "set noyzeroaxis\n" ; 
}
if ( $plot ) {
    print STDOUT "set term X\n" ; 
    print STDOUT "set size 0.6,0.6\n" ;
    print STDOUT "set yrange [0:]\n" ; 
    print STDOUT "set xrange [-0.5:20.5]\n" ; 
#    print STDOUT "plot '$file' u 1:2 w boxes\n" ; 
}
for ( $i = $l ; $i <= $r ; $i ++ ) {  $c[$i] = 0 ; 
}
for ( $x = $start ; $t <= $T ; $t ++ ) {
    $x += (  rand() > 0.5 ) ? 1 : -1 ; 
    if ( $x > $r ) { $x = $r ; }
    if ( $x < $l ) { $x = $l ; }
    print STDERR  "|" ; 
    for ( $i = $l ; $i < $x ; $i ++ ) {	 print STDERR  " " ;  }
    print STDERR  "*" ; 
    for ( $i ++ ; $i <= $r ; $i ++ ) {	 print STDERR  " " ;  }
    print STDERR  "|" ; 
    if ( $x == $r || $x == $l ) {print STDERR  " Bonk!";}
    print STDERR  "\n" ;
    if ( $pbmplot ) {
	if ( $barperiod ) {
	    $thisbackgr = (!($t % $barperiod )) ? $bars : $backgr ;
	}
	print PBM  "$border " ; 
	for ( $i = $l ; $i < $x ; $i ++ ) {	 print PBM  "$thisbackgr " ;  }
	print PBM  "$track " ; 
	for ( $i ++ ; $i <= $r ; $i ++ ) {	 print PBM  "$thisbackgr " ;  }
	print PBM  "$border " ; 
	print PBM  "\n" ;
    }
    if ( $plot || $plotviafile  ) { $c[$x] ++ ; } 
    if ( !($t % $period ) ) { 
	print STDERR  "    $t iterations " ; 
	if ( $plot || $plotviafile ) {
	    $thisfile = $file.".".$t ;  
	    open ( OUT , "> $thisfile" ) ;
	    for (  $i = $l , $cmax = 0 ; $i <= $r ; $i ++ ) {  
		print  OUT  "$i $c[$i]\n" ; 
		if ( $c[$i] > $cmax ) { $cmax = $c[$i] ; }
	    }
	    close(OUT);
	    $numberinplot ++ ; 
	    # construct a plot command that plots the oldest on top. 
	    # plot newguy, older, older, oldest (dumped)
	    # but lets make the oldest guy always be a special colour
	    $thiscolour = ($numberinplot-1) % $maxnuminplot + 1 ; # in 1-8
	    $thisguystring = "'$thisfile' u 1:2 t '$t' w boxes $thiscolour" ;
	    $guystring[$thiscolour] = $thisguystring ; 
	    $oldest = $numberinplot - $maxnuminplot + 1 ;
	    if ( $oldest <= 0 ) { $oldest = 1 ; }
	    else { $oldest = ($oldest-1) % $maxnuminplot + 1 ; }
	    $guystring[$oldest] =~ s/xes\s./xes 8/ ; 
	    $plotstring = "plot " ; 
	    for ( $i = $numberinplot , $c = $thiscolour , $j = 1 ;
		 $i >= 1 && $j <= $maxnuminplot ;
		 $j ++ , $i-- , $c -- ) {
		if ( $c <= 0 ) { $c = $maxnuminplot ; }
		if ( !($j == 1 ) ) {
		    $plotstring .= "," ;
		}
		$plotstring .=  $guystring[$c] ;
	    }
	    
#
#      old style ---------
#
#	    if ( $maxnuminplot && ( $numberinplot > $maxnuminplot ) ) {
#		$firsttime = 1 ; 
#		$numberinplot = 1 ; 
#	    }
#	    if ( $firsttime ) { $plotc = "plot" ; $firsttime = 0 ; }
#	    else { $plotc = "replot" ; }
#	    if ( $plot ) { 
#		print STDOUT "set title '$t iterations'\n" ; 
#		print STDOUT "$plotc '$thisfile' u 1:2 t '$t'  w boxes\n" ;  
#		print STDOUT "pause 1\n" ;  
#	    }
#	    if ( $plotviafile ) { 
#		print GNUCOM "set title '$t iterations'\n" ; 
#		print GNUCOM "$plotc '$thisfile' u 1:2  t '$t' w boxes\n" ;  
#		print GNUCOM "pause 1\n" ;  
#	    }
	    $cmax ++ ; # bit more space
	    if ( $plot ) { 
		print STDOUT "set title '$t iterations'\n" ; 
		printf STDOUT "set yrange [0:%d]\n" , $cmax ; 
   print GNUCOM "set arrow 1 from 20.5,0 to 20.5,$cmax nohead\n" ; 
		print STDOUT "$plotstring\n" ; 
		print STDOUT "pause 1\n" ;  
	    }
	    if ( $plotviafile ) { 
		print GNUCOM "set title '$t iterations'\n" ; 
   print GNUCOM "set arrow 1 from 20.5,0 to 20.5,$cmax nohead\n" ; 
		printf GNUCOM "set yrange [0:%d]\n" , $cmax ; 
		print GNUCOM "$plotstring\n" ; 
		print GNUCOM "pause 1\n" ;  
	    }
# 	    flush(STDOUT);
# 	    system "gnuplot $gnucom\n" ; 
	}
	if( $pause ) {$_ = <STDIN> ;  }
	else { print STDERR "\n" ; }
    }
}
print STDERR "\n# gnuplot \n load '$gnucom' \n" ;
print STDERR "\n# gnuplot \n load 'gnucom40' \n" ;
if ( $pbmplot ) {
# bottom line 
	print PBM  "$border " ; 
	for ( $i = $l ; $i <= $r ; $i ++ ) {	 print PBM  "$border " ;  }
	print PBM  "$border " ; 
	print PBM  "\n" ;
    close ( PBM ) ; 
#    print STDERR "\n# xv $pbmfile\n" ;
}
    print STDERR "\n# xv $pbmfile\n" ;

 

