# Copyright (c) 2003
# 	Riverbank Computing Limited <info@riverbankcomputing.co.uk>
# 
# This file is part of SIP.
# 
# This copy of SIP 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, or (at your option) any later
# version.
# 
# SIP is supplied 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
# SIP; see the file LICENSE.  If not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# This module is intended to be used by the build/installation scripts of
# extension modules created with SIP.  It provides information about file
# locations, version numbers etc., and provides some utility functions.


class SIPConfig:
    def __init__(self,pkg,license):
        """Initialise an instance of the SIPConfig class.

        pkg is the short name of the package.
        license is the software license of the package and must be
        compatible with the SIP license.  Defined values are "GPL",
        "commercial", "educational", "non-commercial".

        The following patches are exported:
            BLX_CONFIG          the qmake CONFIG values of qt, thread,
                                dll, plugin, debug and release
            BLX_DEFINES         the required values of qmake's DEFINES
            BLX_INCLUDEPATH     the required values of qmake's INCLUDEPATH
                                to locate the SIP, Python and Qt include
                                files
            BLX_LIBS            the required values of qmake's LIBS to link
                                the SIP, Python and Qt libraries

            BLX_DEF_BINDIR      the default directory for binaries
            BLX_DEF_MODDIR      the default directory for modules
            BLX_DEF_SIPDIR      the default directory for .sip files

            BLX_PY_VERSION      the Python version as a 3 byte hexadecimal
                                value, eg. v1.5.2 is 0x010502

            BLX_QT_VERSION      the Qt version as a 3 byte hexadecimal
                                value, eg. v2.3.1 is 0x020301, or 0 if Qt
                                support is disabled

            BLX_SIP_BIN         the full path name of the sip binary
            BLX_SIP_LICENSE     the SIP license
            BLX_SIP_VERSION     the SIP version as a 3 byte hexadecimal
                                value, eg. v3.6 is 0x030600
        """
        # Initialise the patch dictionary with those that we are exporting.
        self.patches = {
            "BLX_CONFIG":       "@BLX_CONFIG@",
            "BLX_DEFINES":      "@BLX_DEFINES@",
            "BLX_INCLUDEPATH":  "@BLX_INCLUDEPATH@",
            "BLX_LIBS":         "@BLX_LIBS@",

            "BLX_DEF_BINDIR":   "@BLX_DEF_BINDIR@",
            "BLX_DEF_MODDIR":   "@BLX_DEF_MODDIR@",
            "BLX_DEF_SIPDIR":   "@BLX_DEF_SIPDIR@",

            "BLX_PY_VERSION":   @BLX_PY_VERSION@,

            "BLX_QT_VERSION":   @BLX_QT_VERSION@,

            "BLX_SIP_BIN":      "@BLX_SIP_BIN@",
            "BLX_SIP_LICENSE":  "@BLX_SIP_LICENSE@",
            "BLX_SIP_VERSION":  @BLX_SIP_VERSION@
        }

        self.package = pkg

        _set_script()

        if license != "@BLX_SIP_LICENSE@":
            error("%s and the installed copy of SIP have incompatible licenses." % (self.package))

    def __getattr__(self,name):
        """Internal: Get the value of a patch as if it was an instance
        attribute.
        """
        try:
            return self.patches[name]
        except KeyError:
            pass

        return self.__dict__[name]

    def __setattr__(self,name,val):
        """Internal: Set the value of a patch as if it was an instance
        attribute.
        """
        if name[:3] == "BL_" or name[:4] == "BLX_":
            self.patches[name] = val
        else:
            self.__dict__[name] = val

    def create_config(self,cfgname):
        """Create a configuration module based on a template file, the current
        patches, and any bootstrap code marked as such in the
        build/installation script.  Bootstrap code is delimited by the
        comments "#<BootstrapCode>" and "</BootstrapCode>".

        cfgname is the name of the file to create. The template file has
        the same name but with ".in" appended.
        """
        _create_config(cfgname,self.patches)

    def create_makefile(self,proname,icmds = None):
        """Create a Makefile (and an intermediary project file) based on a
        project file template and the current patches.

        proname is the name of the project file. The template file has the
        same name but with ".in" appended.
        icmds is an optional list of install commands.
        """
        _create_makefile(proname,"@BLI_MFGBIN@",self.patches,icmds)
