# Copyright 2013-2022 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

# ----------------------------------------------------------------------------
# If you submit this package back to Spack as a pull request,
# please first remove this boilerplate and all FIXME comments.
#
# This is a template package file for Spack.  We've put "FIXME"
# next to all the things you'll want to change. Once you've handled
# them, you can save this file and test your package like this:
#
#     spack install dwd-icon-tools
#
# You can edit this file again by typing:
#
#     spack edit dwd-icon-tools
#
# See the Spack documentation for more information on packaging.
# ----------------------------------------------------------------------------

from spack.package import *
from pathlib import Path
import os
from urllib.parse import urlparse


class DwdIconTools(Package):
    """DWD Icon Tools"""

    homepage = "https://gitlab.dkrz.de/dwd-sw/dwd_icon_tools.git"
    # maintainers("oriol.tinto")

    git = "https://gitlab.dkrz.de/dwd-sw/dwd_icon_tools.git"

    token = os.environ.get('SPACK_ICON_NWP_GITTOKEN')
    if token is not None:
        parsed = urlparse(git)
        user = 'token'
        password = token
        git = "{}://{}:{}@{}/{}".format(parsed.scheme, user, password, parsed.netloc, parsed.path)

    version("2.5.3", branch="icontools-2.5.3")
    version("2.5.2", branch="icontools-2.5.2")

    depends_on("netcdf-c")
    depends_on("eccodes")
    depends_on("libxml2")
    #
    # def patch(self):
    #     """
    #     Because of the lack of access rights to the original submodule repositories,
    #     we patch the gitmodules file to point to a different mirror.
    #     """
    #     git_submodules_file = Path().cwd() / ".gitmodules"
    #     git_mirror = "git@gitlab.lrz.de:dkrz-mirror"
    #     git_modules_patch = f"""
    #     [submodule "externals/libcdi"]
    #     path = externals/libcdi
    #     url = {git_mirror}/libcdi.git
    #     """
    #
    #     # Replace the content of the original file with the patch
    #     with git_submodules_file.open("w") as out_f:
    #         out_f.write(git_modules_patch)
    #
    #     # Run git submodule update
    #     git = which("git")
    #     git("submodule", "update", "--init", "--recursive")


    def setup_build_environment(self, env):
        spec = self.spec
        # Some environment variables to set
        env_variables_to_set = {
#           "CC": spec["mpi"].mpicc,
#           "FC": spec["mpi"].mpifc,
#           "F77": spec["mpi"].mpif77,
            "CXXFLAGS": "-O2 -g -fopenmp -Wunused -DNOMPI",
            "FCFLAGS": "-I/usr/include --std=f2008 -O2 -g -cpp -fopenmp -fbounds-check -Wunused -DNOMPI",
            "LIBS": f"-L{spec['libxml2'].prefix.lib} -leccodes -lgfortran -lhdf5 -lxml2",
        }
        for variable, value in env_variables_to_set.items():
                env.set(variable, value)



    def install(self, spec, prefix):
        options = [
            f"--prefix={prefix}",
            "--enable-grib2",
            "--enable-iso-c-interface",
            f"--with-netcdf={spec['netcdf-c'].prefix}"
        ]

        configure(*options)
        make()
        make("install")