#!/usr/bin/env python3

import shutil
import os
import shlex
import subprocess
import sys

env = dict(os.environ)

def read_conffile(fd):
    for line in fd:
        line = line.strip()
        if not line or line.startswith("#"): continue
        if '=' in line:
            key, val = line.split("=", 1)
            val = shlex.split(val)[0]
            if val.startswith("$"):
                val = env[val[1:]]
            env[key] = val

for conffile in ("./run-check.conf", ".git/run-check.conf"):
    if not os.path.exists(conffile): continue
    with open(conffile, "rt") as fd:
        read_conffile(fd)

subprocess.check_call(["make", "-C", "dballe"])
subprocess.check_call(["make", "-C", "dballe", "bench-run"])

for arg in sys.argv[1:]:
    if '=' in arg:
        key, val = arg.split("=", 1)
        env[key] = val

os.chdir("dballe")
os.execve("../extra/runtest", ["../extra/runtest", "bench-run"], env)

#if [ -d .svn ]
#then
#	SVNID=$(svn info | sed -nre 's/^Revision: //p')
#elif [ -d .git ]
#then
#	SVNID=$(git svn info | sed -nre 's/^Revision: //p')
#else
#	echo "No .svn or .git directory found" >&2
#	exit 1
#fi
#
#cd src
#eatmydata ../run-local ./benchmark > "bench/$SVNID-`hostname`-`date +%Y%m%d-%H%M%S`.csv"
