"""
setup script for pyrostex

example use; building extensions:
    python setup.py build_ext --inplace --asserts
"""
import os
import platform
import time

from distutils.core import setup, Extension
from Cython.Build import cythonize
from sys import argv
from zipfile import ZipFile
from subprocess import call

from settings import FLAGS_PXI_PATH, PLANET_GEN_DIR, PLANET_ZIP, \
    PLANET_GEN_EXE, PLANET_GEN_PATH

# parse custom arguments
flags = {  # names flags that will be looked for
    'debug',  # indicates whether maps, debug messages should be printed
    'asserts'  # indicates whether asserts are included in generated C(++)
}

# read flags from argv
flags = {flag: '--' + flag in argv for flag in flags}
# remove custom flags from argv so that setup doesn't get confused
[argv.remove('--' + flag) for flag in flags if '--' + flag in argv]

# extract planet.zip if needed
if not os.path.exists(PLANET_GEN_DIR):
    print('Extracting \'planet\'.zip')
    os.mkdir(PLANET_GEN_DIR)
    # extract files
    with ZipFile(PLANET_ZIP, 'r') as zip_f:
        zip_f.extractall(PLANET_GEN_DIR)

# if planet generator has not been built yet, do that.
if not os.path.exists(PLANET_GEN_PATH):
    # build
    print('Building \'planet\' from sources')
    # store old directory so we can move back later
    old_path = os.path.realpath(os.curdir)
    os.chdir(PLANET_GEN_DIR)  # change directory
    # final call should look something like 'gcc planet.c -o planet -lm -O3'
    call([
        'gcc',
        'planet.c',
        '-o', PLANET_GEN_EXE,
        '-lm',
        '-O3'
    ])
    os.chdir(old_path)  # move back to whatever previous directory was

# create flags.pxi
with open(FLAGS_PXI_PATH, 'w+') as flags_pxi:
    flags_pxi.write("""
# THIS IS A FILE GENERATED BY setup.py
# CHANGES MADE TO THIS FILE WILL BE OVERWRITTEN
    
DEF DEBUG = {debug}  # indicates whether maps, debug messages should be printed
DEF ASSERTS = {asserts}  # indicates whether asserts are included
"""
                    .format_map(flags))

# run setup
setup(
    name='pyrostex',
    ext_modules=cythonize(
        [
            Extension(
                name='pyrostex.map',
                sources=['pyrostex/map.pyx'],
                extra_compile_args=["-ffast-math", "-Ofast"]
            ),
            Extension(
                name='pyrostex.height',
                sources=['pyrostex/height.pyx'],
                extra_compile_args=["-ffast-math", "-Ofast"]
            ),
            Extension(
                name='pyrostex.temp',
                sources=['pyrostex/temp.pyx'],
                extra_compile_args=["-ffast-math", "-Ofast"]
            ),
            Extension(
                name='pyrostex.wind',
                sources=['pyrostex/wind.pyx'],
                language='c++',
                extra_compile_args=["-ffast-math", "-Ofast"]
            ),
            Extension(
                name='pyrostex.v_map',
                sources=['pyrostex/v_map.pyx'],
                extra_compile_args=["-ffast-math", "-Ofast"],
            ),
            Extension(
                name='pyrostex.noise.noise',
                sources=[
                    'pyrostex/noise/noise.pyx',
                    'pyrostex/noise/FastNoise.cpp'
                ],
                language="c++",
                extra_compile_args=["-ffast-math", "-Ofast"],
            ),
        ],
    ),
)
