from easybuild.easyblocks.generic.binary import Binary
from easybuild.tools.filetools import write_file

import os.path


class EB_MPCFramework(Binary):
    """Support for building/installing the MPC Framework."""

    def make_module_extra(self):
        """Correctly prepend $PYTHONPATH and set environment variables for MPC Framework. Equivalent to mpcvars.sh."""

        txt = super(EB_MPCFramework, self).make_module_extra()

        txt += self.module_generator.set_environment(
            "MPC_INSTALL_DIR_PREFIX", self.installdir)
        txt += self.module_generator.set_environment(
            "MPC_VERSION", self.version)

        # Variables such as $PATH, $CPATH or $LD_LIBRARY_PATH are already set
        # by default by EasyBuild.
        txt += self.module_generator.prepend_paths(
            "PYTHONPATH", ['lib/python/', 'lib64/python/'])

        txt += self.module_generator.set_environment(
            "MPCFRAMEWORK_DIR", self.installdir + 'share/mpcframework/cmake/')

        return txt

    def post_install_step(self):
        """Override network rail configuration by adding an MPC configuration file"""

        write_file(os.path.join(self.installdir, "etc", "mpcframework", "rail.conf"),
                   "LOWCOMM_NETWORKING_RAILS_IBMPI_DEVICE=mlx5_0")