#!/usr/bin/python

import traceback
import os, sys, subprocess, glob

sys.path.append('/usr/share/rect-tests/')

from rectparams import *

curdir = os.path.abspath(os.curdir)

try:
    if len(sys.argv) not in (2, 3) or sys.argv[1] == '--help' or sys.argv[1] == 'help':
        print 'Usage %s <testname>' % sys.argv[0]
        print '<testname> must be in next list:'
        print '    ',
        print configs
    elif  sys.argv[1] not in configs:
        rectrun = datadir+'rect-run'
        try:
            f1 = glob.glob(os.path.join(curdir,'rect-run'))
            if (f1 == []):
                rectrun = datadir+'rect-run'
            for f in glob.glob(os.path.join(curdir,'rect-run')):
                if not (f == ""):
                    rectrun = f
        except:
            print "\'rect-run\' was not found\n"
            traceback.print_exc()
        try:
            subprocess.call([rectrun, sys.argv[1]])
        except:
            traceback.print_exc()
    elif (len(sys.argv) == 3) and (sys.argv[1] in configs):
        rectrun = datadir+'rect-run'
        filename = confdir+'config_'+sys.argv[1]+'.yaml'
        try:
            f1 = glob.glob(os.path.join(curdir,'rect-run'))
            if (f1 == []):
                rectrun = datadir+'rect-run'
            for f in glob.glob(os.path.join(curdir,'rect-run')):
                if not (f == ""):
                    rectrun = f
        except:
            print "\'rect-run\' was not found\n"
            traceback.print_exc()
        try:
            f1 = glob.glob(os.path.join(curdir,'config_'+sys.argv[1]+'.yaml'))
            if (f1 == []):
                filename = confdir+'config_'+sys.argv[1]+'.yaml'
            for f in glob.glob(os.path.join(curdir,'config_'+sys.argv[1]+'.yaml')):
                if not (f == ""):
                    filename = f

        except:
            print "\'config_%s.yaml\' was not found\n" % sys.argv[1]
            traceback.print_exc()
        try:
            subprocess.call([rectrun, filename, sys.argv[2]])
        except:
            traceback.print_exc()
    else:
        rectrun = datadir+'rect-run'
        filename = confdir+'config_'+sys.argv[1]+'.yaml'
        try:
            for f in glob.glob(os.path.join(curdir,'rect-run')):
                if not (f == ""):
                    rectrun = f
                else:
                    rectrun = datadir+'rect-run'

        except:
            print "\'rect-run\' was not found\n"
            traceback.print_exc()

        try:
            for f in glob.glob(os.path.join(curdir,'config_'+sys.argv[1]+'.yaml')):
                if not (f == ""):
                    filename = f
                else:
                    filename = confdir+'config_'+sys.argv[1]+'.yaml'
        except:
            print "\'config_%s.yaml\' was not found\n" % sys.argv[1]
            traceback.print_exc()
        try:
            subprocess.call([rectrun, filename])
        except:
            traceback.print_exc()

except:
    print 'Error in rect-tests'
    traceback.print_exc()

