"make mups on windows"
import os
import  sys
import  shutil
import  subprocess
import  glob

VCVER = sys.argv[1]
CMAKEDIR = "build_" + VCVER

MAIN_DIR = os.path.dirname(os.path.abspath(__file__))
BUILD_DIR = os.path.join(MAIN_DIR, CMAKEDIR)

os.environ["METIS_INSTALL"] = os.path.join(MAIN_DIR, "..", "metis-5.1.0", "install_win")
os.environ["BUILD_APPS"] = "0"

CONFIGS = ["Debug", "Release"]

def make_dir(adir):
    """ make a dir """
    if os.path.exists(adir):
        shutil.rmtree(adir)
    os.makedirs(adir)
    return adir

make_dir(BUILD_DIR)
os.chdir(BUILD_DIR)


subprocess.call(["cmake", "-G", "Visual Studio 14 2015 Win64", ".."])

for conf in CONFIGS:
    subprocess.call(["devenv", "MUMPS.sln", "/build", conf + "|x64"])

os.chdir("..")


INSTALL_DIR = os.path.join(MAIN_DIR, "install_win")
make_dir(INSTALL_DIR)

INSTALL_INCLUDE_DIR = make_dir(os.path.join(INSTALL_DIR, "include"))

INCLUDE_FILES = ["dmumps_c.h", "mumps_c_types.h", "mumps_compat.h"]
for f in INCLUDE_FILES:
    if os.path.isfile(f):
        shutil.copy2(f, INSTALL_INCLUDE_DIR)

FOLDER_FOR_INCLUDE = ["include", "libseq"]
for fold in FOLDER_FOR_INCLUDE:
    header_files = glob.iglob(os.path.join(fold, "*.h"))
    for f in header_files:
        if os.path.isfile(f):
            shutil.copy2(f, INSTALL_INCLUDE_DIR)

LIB_DIR = make_dir(os.path.join(INSTALL_DIR, "lib"))

WIN_64_DIR = make_dir(os.path.join(LIB_DIR, "Win64"))

EXTENSION = ['lib', 'dll', 'pdb']
for conf in CONFIGS:
    config_dir = make_dir(os.path.join(WIN_64_DIR, conf))
    for ext in EXTENSION:
        lib_files = glob.iglob(os.path.join(BUILD_DIR, "lib", "Win64", conf, '*.' + ext))
        for f in lib_files:
            shutil.copy(f, config_dir)

