#!/usr/bin/env python

import os, sys, difflib
from optparse import OptionParser

testcases = { 'p1_t1' : 'cat data/tobermory | src/p1.py -n 0',
              'p1_t2' : 'cat data/tobermory | src/p1.py -n 1',
              'p1_t3' : 'cat data/tobermory | src/p1.py -n 2',
              'p1_t4' : 'cat data/tobermory | src/p1.py -n 3 -k 15',
              'p1_t5' : 'cat data/tobermory | src/p1.py -n 50',
              'p2_t1' : 'src/p2.py -h',
              'p2_t2' : 'src/p2.py -f data/tokillamock -s "if he\'s not dead"',
              'p2_t3' : 'src/p2.py -f data/monty_python -s "The Pythons"',
              'p2_t4' : 'src/p2.py -s "rats" -f data/tobermory',
              'p2_t5' : 'src/p2.py -i -s "rats" -f data/tobermory',
              'p3_t1' : 'src/p3.py data/cc data/cc_bkp',
              'p3_t2' : 'src/p3.py data/india data/india_bkp' 
            }

test_index = sorted(testcases.keys())

parser = OptionParser()
parser.add_option("-p", "--problem", dest="problem", type="int", 
                  default="-1", help = "problem number")
parser.add_option("-t", "--testcase", dest="testcase", type="int", 
                  default="-1", help = "test case number")

(options, args) = parser.parse_args()

if options.problem > 0 :

    search_str = 'p' + str(options.problem)

    if options.testcase > 0 :
        search_str += '_t' + str(options.testcase)

    new_index = [x for x in test_index if search_str in x]

    if not new_index :
        sys.stderr.write("Invalid problem no. and/or testcase no.\n")
        sys.exit(1)

    test_index = new_index


for test in test_index :
    
    try :
        outfile = '/tmp/' + test
        reffile = 'output/' + test + '.out'
        command = testcases[test] + ' > ' + outfile
    
        print '\n', 70*'-', '\n\n', 'Running : ', command
        os.system(testcases[test] + ' > ' + outfile)
    
        print 'Computing diff. between ' + outfile + ' and ' + reffile
        os.system('colordiff -EbB ' + outfile + ' ' + reffile)

    except :
        print "Error "
