#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This file is autogenerated by Autocmake v1.0.0 http://autocmake.org
# Copyright (c) 2015-2020 by Radovan Bast, Roberto Di Remigio, Jonas Juselius, and contributors.

import os
import sys
assert sys.version_info >= (2, 6), 'Python >= 2.6 is required'

sys.path.insert(0, 'cmake')
from autocmake import configure
from autocmake.external import docopt

options = """
Usage:
  ./setup [options] [<builddir>]
  ./setup (-h | --help)

Options:
  --cxx=<CXX>                            C++ compiler [default: g++].
  --extra-cxx-flags=<EXTRA_CXXFLAGS>     Extra C++ compiler flags [default: ''].
  --coverage                             Enable code coverage [default: OFF].
  --static                               Build as static library [default: False].
  --xcmaxorder=<XCFUN_MAX_ORDER>         An integer greater than 3 [default: 6].
  --pybindings                           Enable Python interface [default: OFF].
  --type=<TYPE>                          Set the CMake build type (debug, release, relwithdebinfo, minsizerel) [default: debug].
  --generator=<STRING>                   Set the CMake build system generator [default: Unix Makefiles].
  --show                                 Show CMake command and exit.
  --cmake-executable=<CMAKE_EXECUTABLE>  Set the CMake executable [default: cmake].
  --cmake-options=<STRING>               Define options to CMake [default: ''].
  --prefix=<PATH>                        Set the install path for make install.
  <builddir>                             Build directory.
  -h --help                              Show this screen.
"""


def gen_cmake_command(options, arguments):
    """
    Generate CMake command based on options and arguments.
    """
    command = []
    command.append(arguments['--cmake-executable'])
    command.append('-DCMAKE_CXX_COMPILER={0} -DEXTRA_CXXFLAGS="{1}"'.format(arguments['--cxx'],
                                                                            arguments['--extra-cxx-flags']))
    command.append('-DENABLE_CODE_COVERAGE={0}'.format(arguments['--coverage']))
    command.append('-DBUILD_SHARED_LIBS={0}'.format(not arguments['--static']))
    command.append('-DXCFUN_MAX_ORDER="{0}"'.format(arguments['--xcmaxorder']))
    command.append('-DXCFUN_PYTHON_INTERFACE={0}'.format(arguments['--pybindings']))
    command.append('-DCMAKE_BUILD_TYPE={0}'.format(arguments['--type']))
    command.append('-G"{0}"'.format(arguments['--generator']))
    if arguments['--cmake-options'] != "''":
        command.append(arguments['--cmake-options'])
    if arguments['--prefix']:
        command.append('-DCMAKE_INSTALL_PREFIX="{0}"'.format(arguments['--prefix']))

    return ' '.join(command)


# parse command line args
try:
    arguments = docopt.docopt(options, argv=None)
except docopt.DocoptExit:
    sys.stderr.write('ERROR: bad input to {0}\n'.format(sys.argv[0]))
    sys.stderr.write(options)
    sys.exit(-1)

# use extensions to validate/post-process args
if configure.module_exists('extensions'):
    import extensions
    arguments = extensions.postprocess_args(sys.argv, arguments)

root_directory = os.path.dirname(os.path.realpath(__file__))

build_path = arguments['<builddir>']

# create cmake command
cmake_command = '{0} -H{1}'.format(gen_cmake_command(options, arguments), root_directory)

# run cmake
configure.configure(root_directory, build_path, cmake_command, arguments)
