#!/usr/bin/env python

import argparse
import os
import subprocess
import tempfile


def is_valid_and_executable_file(parser, arg):
    if not os.path.isfile(arg):
        parser.error("'%s' does not exist or is not a file." % arg)
    else:
        return os.path.abspath(arg)

parser = argparse.ArgumentParser(description='Hello.')
parser.add_argument('File', metavar='File',
                    type=lambda x: is_valid_and_executable_file(parser, x),
                    help='The CSV file from starexec.')
parser.add_argument('-v', action='store_true',
                    help='Display not only the number of uniques but also the concrete problem names.')

args = parser.parse_args()


# solver -> (config -> status -> [problem])

verbose = args.v
inputFile = args.File
results = {}

with open(inputFile,'r') as f:
  for line in f:
      line = line.rstrip()
      if not line: continue
      values = line.split(',')
      problem = values[1]
      prover = values[3]
      config = values[5]
      status = values[11]
      if (status == "--"): status = "Timeout"
      
      if (prover == "solver") and (config == "configuration") and (status == "result") and (problem == "benchmark"): continue
      
      if (prover in results):
        proverEntry = results[prover]
        if (config in proverEntry):
          configEntry = proverEntry[config]
          if (status in configEntry):
            configEntry[status].append(problem)
          else:
            configEntry[status] = [problem]
        else:
          proverEntry[config] = {status: [problem]}
      else:
        results[prover] = {config : { status : [problem] }}
            
for prover in results.keys():
  for config in results[prover].keys():
    print("-------------------")
    print(prover + " (" + config + ")")
    print("-------------------")
    for status,problems in results[prover][config].items():
      print(status + ": " + str(len(problems)))
      if (verbose):
        print(problems)
    print()

