#!/usr/bin/env python3

import os
import sys
import stat
import shutil
import random
import subprocess


def build(algo_dir, template_dir, build_dir):
    # create a new directory for the build
    while build_dir is None:
        r = "%09d" % random.randint(0, 999999999)
        d = os.path.join("build", r)
        if not os.path.isdir(d):
            build_dir = d
    print("Building in %s" % build_dir)
   
    # copy all the files from the submitted algorithm into the build directory
    shutil.copytree(algo_dir, build_dir)

    # remove the test vectors generator if it is there
    c = os.path.join(build_dir, "genkat_aead.c")
    if os.path.exists(c):
        os.remove(c)

    # find all c and h files, since they will be added to the makefile
    hfiles = []
    cfiles = []
    for r, d, f in os.walk(build_dir):
        for file in f:
            if file.endswith(".c"):
                cfiles.append(file)
            elif file.endswith(".h"):
                hfiles.append(file)

    # copy all the files from the template directory into the build directory
    for f in os.listdir(template_dir):
        dst = os.path.join(build_dir, f)
        src = os.path.join(template_dir, f)
        if os.path.isfile(src) or os.path.islink(src):
            shutil.copy2(src, dst)
        elif os.path.isdir(src):
            shutil.copytree(src, dst)
        else:
            raise Exception("I don't know what %s is" % src)

    # prepare the environmental variables for the makefile
    env = os.environ
    env['SRC_FILES'] = ' '.join(cfiles)
    env['HDR_FILES'] = ' '.join(hfiles)

    # enter the directory and execute the makefile
    wd = os.getcwd()
    os.chdir(build_dir)
    try:
        if os.path.isfile('./configure'):
            p = subprocess.Popen(["./configure"])
            p.wait()
            assert p.returncode == 0

        p = subprocess.Popen(['make'])
        p.wait()
        assert p.returncode == 0

        if os.path.isfile('./cleanup'):
            p = subprocess.Popen(["./cleanup"])
            p.wait()
            assert p.returncode == 0


    finally:
        sys.stdout.flush()
        sys.stderr.flush()
        os.chdir(wd)

    # if execution arrives here, the build was successful
    return build_dir


# Find test vectors in directory or one of the parent directories
def find_test_vectors(d):
    kat = None
    while True:
        if d == '':
            raise Exception("Test vector not found")
        for f in os.listdir(d):
            if f.startswith("LWC_AEAD_KAT_") and f.endswith(".txt"):
                if kat is not None:
                    raise Exception("Multiple test vectors?")
                kat = f
        if kat is None:
            d = os.path.split(d)[0]
        else:
            break
    kat = os.path.join(d, kat)
    return kat


def main(argv):
    submissions_dir = "all-lwc-submission-files"
    template_dir = "templates/linux"
    include_list = None
    if len(argv) > 1:
        template_dir = argv[1]
    if len(argv) > 2:
        with open(argv[2], 'r') as includes:
            include_list = []
            for line in includes.readlines():
                include_list.append(line.strip())
    print("Using template %s" % template_dir)
    subs = os.listdir(submissions_dir)

    # get all the submissions by looking for files named "api.h"
    subfiles = []
    for submission in subs:
        implementations_dir = os.path.join(submissions_dir, submission, "Implementations", "crypto_aead")

        if not os.path.isdir(implementations_dir):
            continue

        if "NOT ACCEPTED" in implementations_dir:
            continue

        print()
        print("###  %s  ###" % submission)

        c = 0
        # r=root, d=directories, f = files
        for r, d, f in os.walk(implementations_dir):
            for file in f:
                if file == "api.h":
                    f = os.path.join(r, file)
                    subfiles.append(f)
                    c += 1

        if c == 0:
            raise Exception("No implementations found")

    if include_list is not None:
        print("Include list has %d entries" % len(include_list))

    files = []
    for f in subfiles:

        # Source directory d
        d = os.path.split(f)[0]
        assert os.path.isdir(d)
        print(d)

        # Test vectors file t
        t = find_test_vectors(d)
        print(t)

        # base name n
        pieces = f.split(os.sep)
        n = pieces[1] + "." + ".".join(pieces[4:-1])
        print(n)
        
        # if include_list was provided, skip elements not in the list
        if include_list is not None:
            if not n in include_list:
                continue

        # Put all in a tuple and count
        files.append((t, d, n))



    # For testing, we only do the first 1
    #files = files[:1]
    print("%d algorithms will be compiled" % len(files))

    # Clear the build directory as it is a leftover from the previous execution
    if os.path.isdir('build'):
        shutil.rmtree('build')
    os.mkdir('build')

    print()

    # Write a script that executes all the tests one after the other
    test_script_path = os.path.join("build", "test_all.sh")
    with open(test_script_path, 'w') as test_script:
        test_script.write("#!/bin/sh\n")
        test_script.write("mkdir -p logs\n")
        test_script.write("mkdir -p measurements\n")
        for i, (t, d, name) in enumerate(files):
            print()
            print(d)
            try:
                build_dir = os.path.join("build", name)
                b = build(d, template_dir, build_dir)
                test_script.write("\n\necho \"TEST NUMBER %03d: TESTING %s\"\n" % (i, d))
                test_script.write("python3 -u ./test.py %s %s 2> %s | tee %s\n" % (
                        t,
                        os.path.join(b, 'test'),
                        os.path.join(b, 'test_stderr.log'),
                        os.path.join(b, 'test_stdout.log'))
                )

                print("COMPILATION SUCCESS FOR %s" % d)
            except Exception as ex:
                print("COMPILATION FAILED FOR %s" % d)
                print(ex)

    st = os.stat(test_script_path)
    os.chmod(test_script_path, st.st_mode | stat.S_IEXEC)

    print()
    print()
    print("Now execute ' %s ' to start the test" % test_script_path)


if __name__ == "__main__":
    sys.exit(main(sys.argv))
