# run all the Autoplot jython scripts in this folder.
#
# tests should output test_NAME.txt, or test_NAME.png test_NAME.stdout.txt is created automatically.

import subprocess
import os 
import sys
import glob, sys, time

def _runone( t0, i ):
  #subprocess.call( ['echo', '%s' % i ] )
  JAVA='/usr/local/jdk-8/bin/java'   
  AUTOPLOT=os.environ['AUTOPLOT']
 
  ID= i.rsplit('/',1)[1]

  #JAVA_VM_ARGS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=12345,suspend=n"
  JAVA_VM_ARGS=""

  if len(JAVA_VM_ARGS)==0:
     cmd= [ JAVA, '-Djava.awt.headless=true', '-Dfile.encoding=UTF-8', '-cp', AUTOPLOT, '-DAUTOPLOT_DATA=/usr/share/tomcat/autoplot_data', 'org.autoplot.AutoplotUI', '--script', '%s' % i ]
  else:
     cmd= [ JAVA, '-Djava.awt.headless=true', '-Dfile.encoding=UTF-8' ] + JAVA_VM_ARGS.split(' ')+['-cp', AUTOPLOT, '-DAUTOPLOT_DATA=/usr/share/tomcat/autoplot_data', 'org.autoplot.AutoplotUI', '--script', '%s' % i ]
  print( '%.1fs %s' % ( time.time()-t0, ' '.join(cmd) ) )

  print() 
  sys.stdout.flush()
  
  STDERR_= open( 'test037_%s.stderr.txt' % ID, 'w' )

  proc= subprocess.Popen( cmd, stderr=STDERR_, stdout=subprocess.PIPE )

  STDOUT_= open( 'test037_%s.stdout.txt' % ID, 'w' )
  for line in iter(proc.stdout.readline,''):
     STDOUT_.write( str(line) )
  STDOUT_.close()
  STDERR_.close()

t00= time.time()

for i in glob.glob('/home/jbf/ct/hudson/script/test037/[a-z0-9]*.jy'):
   print( '== %s ==' % i )
   sys.stdout.flush()
   t0= time.time()
   print( 'started test %s at %.1f seconds' % ( i, t0-t00 )  )
   _runone( t0, i )
   print( 'completed test %s in %.1f seconds' % ( i, ( time.time()-t0 ) ) )
   print( '')
