#! /usr/bin/env python

import sys
import re
import subprocess
import optparse
import tm5_test
import pdb


def main():

      """
      TM6 testing suite

      (1) create a tm5.rc from tm6.rc
      (2) compare TM6 with TM5
      (3) compare TM6 with 1, 4, and 10 processors [commented]
      (4) compare TM6 with extra restart [commented]

      Call:

      > test_tm6.py [-n] [-q]

      then tear the test down :

      > test_tm6.py -t

      There is two steps, because you may want to look at the models outputs
      before cleaning up (eg when a test is not successful). 
      
      """

      p = optparse.OptionParser()
      p.add_option("-t", "--teardown", action="store_true", dest="teardown",
                   help="cleanup test: remove restart and files in rundir")
      
      p.add_option("-n", "--new", action="store_true", dest="new",
                   help="recompile everything fresh (a la realclean)")

      p.add_option("-q", "--queue", action="store_true", dest="queue",
                   help="submit jobs to queue instead of foreground")

      options, args = p.parse_args()


      # 1st create a tm5.rc from tm6.rc
      #---------------------------------

      # to replace:
      tm6str=('my.source.CHEM : proj/chem/base/${my.tm6branch}',
              'my.source.BASE : base/${my.branch} base/${my.tm6branch}',
              'my.project.dir : ${my.scratch}/TM5/tm6',
              'my.defs_chem   : ')

      # with:
      tm5str=('my.source.CHEM : proj/chem/base/${my.branch}', 
              'my.source.BASE : base/${my.branch}',
              'my.project.dir : ${my.scratch}/TM5/standard',
              'my.defs_chem   : with_tracerorder')

      # simply use:
      regex = ( r"^my\.source\.CHEM", r"^my\.source\.BASE", r"^my.project.dir" )

      infile=open('tm6.rc', 'r')
      outfile=open('tm5.rc', 'w')

      for line in infile:
            seded=False
            for (klm,r) in enumerate(regex):
                  match = re.search(r, line)
                  if match:
                        outfile.write( tm5str[klm]+'\n' )
                        seded=True
            if not seded: outfile.write( line )

      infile.close()
      outfile.close()



      # compare TM6 and TM5
      #---------------------------------
      try:
            tm5_test.testtm(['tm6.rc', 'tm5.rc'], teardown=options.teardown,
                            new=options.new, queue=options.queue)
      except:
            print "FAIL : tm6 & tm5 are different"
            return 1
      
 
 
 
 
#      # compare results with different number of processors
#      #---------------------------------
#      try:
#            tm5_test.testtm(['tm6.rc'], teardown=options.teardown,
#                            new=options.new, queue=options.queue, tm6=True, mpi=True)
#      except:
#            print "FAIL : tm6 gives different results on different number of processors"
#            return 1
# 
 
 
 
#      # compare results with extra restart
#      #---------------------------------
#      try:
#            tm5_test.testtm(['tm6.rc'], teardown=options.teardown, new=options.new,
#                            queue=options.queue, restart=True)
#      except:
#            print "FAIL : tm6 gives different results with extra restart"
#            return 1
 
      return 0

if __name__ == '__main__':
      sys.exit(main())

