##################################################
#
# ROOT module
#
##################################################
                                                                                                                                                            
# custom imports
from baseilc import BaseILC
from util import *


class ROOT(BaseILC):
    """ Responsible for the ROOT configuration process. """

    
    def __init__(self, userInput):
        BaseILC.__init__(self, userInput, "ROOT", "root")

        self.hasCMakeBuildSupport = False

        self.download.supportedTypes = [ "wget", "svn-export", "svn" ]

        self.reqfiles = [ ["lib/libCore.so", "lib64/libCore.so", "lib/libCore.dylib"], 
                          ["lib/libPhysics.so", "lib64/libPhysics.so", "lib/libPhysics.dylib"],
                          ["bin/root-config"] ]

        self.reqmodules_external = [ "GSL" ]


    def setMode(self, mode):
        BaseILC.setMode(self, mode)

        self.download.url = 'https://root.cern.ch/download/root_v%s.source.tar.gz' % self.version
        self.download.svnurl = 'http://root.cern.ch/svn/root'

        if( Version( self.version ) == 'HEAD' ):
            self.download.svnurl += '/trunk'
        else:
            self.download.svnurl += '/tags/v' + self.version.replace('.','-')


    def init(self):
        BaseILC.init(self)

        if( Version( self.version ) == 'HEAD' and self.download.type[:3] != 'svn' ):
            self.download.type="svn-export"


    def downloadSources(self):
        BaseILC.downloadSources(self)


    def cleanupInstall(self):
        BaseILC.cleanupInstall(self)
        os.chdir( self.installPath )
        os.system( "rm -rf ./" + self.name )


    def compile(self):
        gsl = self.parent.module("GSL")
        gsl_bindir = gsl.installPath + "/bin"
        gsl_libdir = gsl.installPath + "/lib"
        gsl_incdir = gsl.installPath + "/include"

        os.environ["LD_RUN_PATH"] = gsl_libdir

        trymakedir( self.installPath + "/../build-" + self.version )
        os.chdir( self.installPath + "/../build-" + self.version )

        if( self.rebuild ):
            tryunlink( "CMakeCache.txt" )

        self.envcmake['CMAKE_INSTALL_PREFIX'] = self.installPath
        self.envcmake['GSL_CONFIG_EXECUTABLE'] = gsl_bindir+'/gsl-config'

        self.envcmake.setdefault( 'gsl_shared',     'ON' )
        self.envcmake.setdefault( 'gdml',           'ON' )
        self.envcmake.setdefault( 'minuit2',        'ON' )
        self.envcmake.setdefault( 'roofit',         'ON' )
        self.envcmake.setdefault( 'unuran',         'ON' )
        self.envcmake.setdefault( 'xrootd',         'OFF' )
        self.envcmake.setdefault( 'builtin_xrootd', 'OFF' )
        self.envcmake.setdefault( 'fortran',        'OFF' )
        self.envcmake.setdefault( 'mysql',          'OFF' )

        if( os.system( self.genCMakeCmd() + " 2>&1 | tee -a " + self.logfile ) != 0 ):
            self.abort( "failed to configure!!" )
        if( os.system( "make ${MAKEOPTS} 2>&1 | tee -a " + self.logfile ) != 0 ):
            self.abort( "failed to compile!!" )
        if( os.system( "make install ${MAKEOPTS} 2>&1 | tee -a " + self.logfile ) != 0 ):
            self.abort( "failed to install!!" )



    def postCheckDeps(self):
        BaseILC.postCheckDeps(self)

        self.env["ROOTSYS"] = self.installPath
        self.envpath["PATH"].append( "$ROOTSYS/bin" )
        self.envpath["LD_LIBRARY_PATH"].append( "$ROOTSYS/lib" )
        self.envpath["PYTHONPATH"].append( "$ROOTSYS/lib" )

