#!/usr/bin/env python
import os
import multiprocessing
import sys
import subprocess
import datetime
import re

from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.test import test

import importlib.util

if sys.version_info[0] == 2:
    sys.exit('Sorry, Python 2.x is not supported')


def check_storm_compatible(storm_v_major, storm_v_minor, storm_v_patch):
    if storm_v_major < 1 or (storm_v_major == 1 and storm_v_minor < 1) or (storm_v_major == 1 and storm_v_minor == 1 and storm_v_patch < 0):
        sys.exit('Sorry, Storm version {}.{}.{} is not supported anymore!'.format(storm_v_major, storm_v_minor,
                                                                                  storm_v_patch))


def parse_storm_version(version_string):
    """
    Parses the version of storm.
    :param version_string:
    :return: Version as three-tuple.
    """
    elems = version_string.split(".")
    if len(elems) != 3:
        sys.exit('Storm version string is ill-formed: "{}"'.format(version_string))
    return int(elems[0]), int(elems[1]), int(elems[2])


def obtain_version():
    """
    Obtains the version as specified in stormpy.
    :return: Version of stormpy.
    """
    verstr = "unknown"
    try:
        verstrline = open('lib/stormpy/_version.py', "rt").read()
    except EnvironmentError:
        pass  # Okay, there is no version file.
    else:
        VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
        mo = re.search(VSRE, verstrline, re.M)
        if mo:
            verstr = mo.group(1)
        else:
            raise RuntimeError("unable to find version in stormpy/_version.py")
    return verstr


class CMakeExtension(Extension):
    def __init__(self, name, sourcedir='', subdir=''):
        Extension.__init__(self, name, sources=[])
        self.sourcedir = os.path.abspath(sourcedir)
        self.subdir = subdir


class CMakeBuild(build_ext):
    user_options = build_ext.user_options + [
        ('storm-dir=', None, 'Path to storm root (binary) location'),
        ('jobs=', 'j', 'Number of jobs to use for compiling'),
        ('debug', None, 'Build in Debug mode'),
    ]

    def extdir(self, extname):
        return os.path.abspath(os.path.dirname(self.get_ext_fullpath(extname)))

    def run(self):
        self.conf = None
        try:
            _ = subprocess.check_output(['cmake', '--version'])
        except OSError:
            raise RuntimeError("CMake must be installed to build the following extensions: " +
                               ", ".join(e.name for e in self.extensions))

        build_temp_version = self.build_temp + "-version"
        if not os.path.exists(build_temp_version):
            os.makedirs(build_temp_version)

        # Check cmake variable values
        cmake_args = []
        if self.storm_dir is not None:
            cmake_args = ['-Dstorm_DIR=' + self.storm_dir]
        output = subprocess.check_output(['cmake', os.path.abspath("cmake")] + cmake_args, cwd=build_temp_version)
        spec = importlib.util.spec_from_file_location("genconfig",
                                                      os.path.join(build_temp_version, 'generated/config.py'))
        self.conf = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(self.conf)

        # Check storm version
        storm_v_major, storm_v_minor, storm_v_patch = parse_storm_version(self.conf.STORM_VERSION)
        check_storm_compatible(storm_v_major, storm_v_minor, storm_v_patch)

        # Create dir
        lib_path = os.path.join(self.extdir("core"))
        if not os.path.exists(lib_path):
            os.makedirs(lib_path)

        for ext in self.extensions:
            if ext.name == "core":
                with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
                    f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))

                    f.write("import pycarl\n")
                    if self.conf.STORM_CLN_EA or self.conf.STORM_CLN_RF:
                        f.write("import pycarl.cln\n")
                    if not self.conf.STORM_CLN_EA or not self.conf.STORM_CLN_RF:
                        f.write("import pycarl.gmp\n")

                    if self.conf.STORM_CLN_EA:
                        f.write("Rational = pycarl.cln.Rational\n")
                    else:
                        f.write("Rational = pycarl.gmp.Rational\n")

                    if self.conf.STORM_CLN_RF:
                        rfpackage = "cln"
                    else:
                        rfpackage = "gmp"
                    f.write("RationalRF = pycarl.{}.Rational\n".format(rfpackage))
                    f.write("Polynomial = pycarl.{}.Polynomial\n".format(rfpackage))
                    f.write("FactorizedPolynomial = pycarl.{}.FactorizedPolynomial\n".format(rfpackage))
                    f.write("RationalFunction = pycarl.{}.RationalFunction\n".format(rfpackage))
                    f.write("FactorizedRationalFunction = pycarl.{}.FactorizedRationalFunction\n".format(rfpackage))
                    f.write("\n")
                    f.write("storm_with_pars = {}\n".format(self.conf.HAVE_STORM_PARS))
                    f.write("storm_with_dft = {}\n".format(self.conf.HAVE_STORM_DFT))

            elif ext.name == "info":
                with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
                    f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
                    f.write("storm_version = \"{}\"\n".format(self.conf.STORM_VERSION))
                    f.write("storm_cln_ea = {}\n".format(self.conf.STORM_CLN_EA))
                    f.write("storm_cln_rf = {}".format(self.conf.STORM_CLN_RF))
            elif ext.name == "pars":
                with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
                    f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
                    f.write("storm_with_pars = {}".format(self.conf.HAVE_STORM_PARS))
                if not self.conf.HAVE_STORM_PARS:
                    print("WARNING: storm-pars not found. No support for parametric analysis will be built.")
                    continue
            elif ext.name == "dft":
                with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
                    f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
                    f.write("storm_with_dft = {}".format(self.conf.HAVE_STORM_DFT))
                if not self.conf.HAVE_STORM_DFT:
                    print("WARNING: storm-dft not found. No support for DFTs will be built.")
                    continue
            self.build_extension(ext)

    def initialize_options(self):
        build_ext.initialize_options(self)
        self.storm_dir = None
        self.debug = False
        try:
            self.jobs = multiprocessing.cpu_count() if multiprocessing.cpu_count() is not None else 1
        except NotImplementedError:
            self.jobs = 1

    def finalize_options(self):
        if self.storm_dir is not None:
            print('The custom storm directory', self.storm_dir)
        build_ext.finalize_options(self)

    def build_extension(self, ext):
        extdir = self.extdir(ext.name)
        cmake_args = ['-DSTORMPY_LIB_DIR=' + extdir,
                      '-DPYTHON_EXECUTABLE=' + sys.executable]

        build_type = 'Debug' if self.debug else 'Release'
        build_args = ['--config', build_type]
        build_args += ['--', '-j{}'.format(self.jobs)]
        cmake_args += ['-DCMAKE_BUILD_TYPE=' + build_type]
        if self.conf.STORM_DIR is not None:
            cmake_args += ['-Dstorm_DIR=' + self.conf.STORM_DIR]
        if self.conf.HAVE_STORM_PARS:
            cmake_args += ['-DHAVE_STORM_PARS=ON']
        if self.conf.HAVE_STORM_DFT:
            cmake_args += ['-DHAVE_STORM_DFT=ON']

        env = os.environ.copy()
        env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
                                                              self.distribution.get_version())
        if not os.path.exists(self.build_temp):
            os.makedirs(self.build_temp)
        print("CMake args={}".format(cmake_args))
        # Call cmake
        subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
        subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=self.build_temp)


class PyTest(test):
    def run_tests(self):
        # import here, cause outside the eggs aren't loaded
        import pytest
        errno = pytest.main(['tests'])
        sys.exit(errno)


setup(
    name="stormpy",
    version=obtain_version(),
    author="M. Volk",
    author_email="matthias.volk@cs.rwth-aachen.de",
    maintainer="S. Junges",
    maintainer_email="sebastian.junges@cs.rwth-aachen.de",
    url="http://moves.rwth-aachen.de",
    description="stormpy - Python Bindings for Storm",
    packages=['stormpy', 'stormpy.info', 'stormpy.logic', 'stormpy.storage', 'stormpy.utility',
              'stormpy.pars', 'stormpy.dft'],
    package_dir={'': 'lib'},
    ext_package='stormpy',
    ext_modules=[CMakeExtension('core', subdir=''),
                 CMakeExtension('info', subdir='info'),
                 CMakeExtension('logic', subdir='logic'),
                 CMakeExtension('storage', subdir='storage'),
                 CMakeExtension('utility', subdir='utility'),
                 CMakeExtension('pars', subdir='pars'),
                 CMakeExtension('dft', subdir='dft'),
                 ],
    cmdclass={'build_ext': CMakeBuild, 'test': PyTest},
    zip_safe=False,
    install_requires=['pycarl>=2.0.1'],
    setup_requires=['pytest-runner'],
    tests_require=['pytest'],
)