# mass-build - Easily Build Software Involving a Large Amount of Source Repositories
# Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import os, shutil, subprocess, multiprocessing

'''A build system must have a "build" method with parameters "reconfigure" and "waitAfterConfig".'''

# Compile, build, and install cmake projects:
class CMake:
    def __init__(self, sourceFolder, buildFolder, config):
        self.sourceFolder = os.path.abspath(sourceFolder)
        self.buildFolder = os.path.abspath(buildFolder)
        self.config = config
    
    def setEnv(self, name, val):
        '''Set the given environment variable, return old value'''
        oldVal = os.getenv(name)
        os.putenv(name, val)
        return oldVal
    
    def prependDirToEnv(self, name, dir, default):
        '''Prepends the given directory to the environment variable. If the variable is empty, dir isprepended to the default.
        Returns the old value.'''
        oldVal = os.getenv(name)
        oldPaths = default if oldVal is None else oldVal
        os.putenv(name, dir+':'+oldPaths)
        return oldVal
    
    def restoreEnv(self, name, oldVal):
        '''Restore environment variable to previous value'''
        if oldVal is None:
            os.unsetenv(name)
        else:
            os.putenv(name, oldVal)
    
    def build(self, reconfigure, waitAfterConfig):
        # Make sure we have a build directory
        if not os.path.exists(self.buildFolder): os.makedirs(self.buildFolder)
        os.chdir(self.buildFolder)
        # In case of reconfiguration, delete cache file if it exists
        cacheFile = 'CMakeCache.txt'
        if os.path.exists(cacheFile) and reconfigure: os.remove(cacheFile)
        # Run cmake, in the proper environment, then restore old environment
        oldPKGConfigPath = self.setEnv('PKG_CONFIG_PATH', os.path.join(self.config['installDir'], 'lib', 'pkgconfig'))
        oldCMakePrefixPath = self.setEnv('CMAKE_PREFIX_PATH', self.config['installDir'])
        oldXDGDataDirs = self.prependDirToEnv('XDG_DATA_DIRS', os.path.join(self.config['installDir'], 'share'), '/usr/share')
        oldXDGConfigDirs = self.prependDirToEnv('XDG_CONFIG_DIRS', os.path.join(self.config['installDir'], 'etc', 'xdg'), '/etc/xdg')
        subprocess.check_call(['cmake', self.sourceFolder, '-DCMAKE_BUILD_TYPE='+self.config['buildType'],
            '-DCMAKE_INSTALL_PREFIX='+self.config['installDir']]+self.config.get('cmakeParameters', []))
        self.restoreEnv('PKG_CONFIG_PATH', oldPKGConfigPath)
        self.restoreEnv('CMAKE_PREFIX_PATH', oldCMakePrefixPath)
        self.restoreEnv('XDG_DATA_DIRS', oldXDGDataDirs)
        self.restoreEnv('XDG_CONFIG_DIRS', oldXDGConfigDirs)
        # if asked to do so, wait
        if waitAfterConfig:
            input('Configuration done. Hit "Enter" to build the project. ')
        # run compilation
        jobs = multiprocessing.cpu_count()+1
        subprocess.check_call(self.config.get('buildCmdPrefix', []) + ['make', '-j'+str(jobs)])
        # run installation
        subprocess.check_call(self.config.get('installCmdPrefix', []) + ['make', 'install', '-j'+str((jobs+1)//2)]) # jobs/2, rounded up

# if auto-debuild is available, provide a wrapper for it
try:
    import auto_debuild
    class AutoDebuild:
        def __init__(self, sourceFolder, buildFolder, config, vcs):
            self.sourceFolder = os.path.abspath(sourceFolder)
            self.buildFolder = os.path.abspath(buildFolder)
            self.debFolder = os.path.abspath(config['debDir'])
            self.config = config
            self.vcs = vcs

        def build(self, reconfigure, waitAfterConfig): # reconfigure is ignored (we always do a reconfiguration)
            # get version name
            versionName = self.config['versionName'] if 'versionName' in self.config else self.vcs.version()
            if versionName is None:
                raise Exception("VCS did not provide us with a proper version number, please provide one manually")
            # create auto-debuild configuration
            autoDebuildConfig = {
                'sourceName': self.config['name'],
                'buildSystem': self.config['buildSystem'],
                'debDir': self.debFolder,
                'buildDir': self.buildFolder,
                'name': self.config['debName'],
                'email': self.config['debEMail'],
                'version': versionName + self.config.get('versionSuffix', ''),
                'waitAfterConfig': waitAfterConfig,
            }
            # copy some more optional configuration
            for option in ('epoch', 'dbgPackage', 'section', 'withPython2', 'withSIP', 'binarySkipFiles', 'binaryInstallFiles',
                    'buildDepends', 'binaryDepends', 'binaryShims', 'binaryRecommends', 'binaryProvides', 'binaryConflicts', 'binaryBreaks',
                    'binaryReplaces', 'binaryBreaksReplaces',
                    'alternatives', 'cmakeParameters', 'automakeParameters', 'autogen'):
                if option in self.config:
                    autoDebuildConfig[option] = self.config[option]
            # create Debian files
            os.chdir(self.sourceFolder)
            auto_debuild.deleteDebianFolder()
            files = auto_debuild.createDebianFiles(autoDebuildConfig)
            # build package(s)
            auto_debuild.buildDebianPackage(autoDebuildConfig)
            # install package(s)
            if self.config.get('debInstall', True):
                subprocess.check_call(['sudo', 'dpkg', '--install'] + files)

except ImportError:
    #print "auto_debuild not found, disabling auto-debuild system"
    pass
