#!/usr/bin/python3

import argparse
import C_build_script.make as make
import shutil
import os

from pathlib import Path

def main():

    argparser = argparse.ArgumentParser()
    argparser.add_argument("-s", action='store', type=str, metavar="SOURCE", help="Source file to generate executable from")
    argparser.add_argument("-d", action='store_true', help="Sets build to debug mode")
    argparser.add_argument("-c", action='store_true', help="Cleans all output files")
    args = argparser.parse_args()

    if args.c:
        if os.path.exists("bin"):
            print("Removing 'bin/'")
            shutil.rmtree("bin")
        if os.path.exists("objects"):
            print("Removing 'objects/'")
            shutil.rmtree("objects")
        if os.path.exists("objects-debug"):
            print("Removing 'objects-debug/'")
            shutil.rmtree("objects-debug")

    if args.s:
        config = {
            "COMPILER":     "clang++",
            "LINKER_FLAGS": "-pthread",
            "EXE_DIR":      "./bin",
            "SOURCE_MAIN":  Path(args.s).name,
            "SOURCE_DIR":   "./src/",
            "SOURCE_EXT":   "cpp",
            "HEADER_DIR":   "./include/",
            "HEADER_EXT":   "hpp",
            "OBJECT_EXT":   "o",
            "OTHER_INCLUDES": "",
            "RESOURCES": []
        }

        if args.d:
            config["COMPILER_FLAGS"]    = "-std=c++17 -Wall -Wextra -Wpedantic -g -DDEBUG"
            config["OBJECT_DIR"]        = "./objects-debug/"
            config["EXE_FILE"]          = Path(args.s).stem + "-debug"
        else:
            config["COMPILER_FLAGS"]    = "-std=c++17 -Wall -Wextra -Wpedantic -O3"
            config["OBJECT_DIR"]        = "./objects/"
            config["EXE_FILE"]          = Path(args.s).stem

        make.parse_dict(config)
        make.execute("build")


if __name__ == "__main__":
    main()