#!/usr/bin/python
#
# configure script for libLensFun
#

import ac

# Define project name
ac.PROJ = "lensfun"
# Define version number (major.minor.release)
ac.VERSION = "0.2.4"
# Project home page
ac.HOMEPAGE = "http://lensfun.berlios.de"

# A directory where configure will find the required libs on systems
# without pkg-config (e.g. windows) (only glib-2.0 needed so far)
ac.OPTIONS.append (
    [ None, "sdkdir", "DIR", "global SDKDIR; SDKDIR = optarg",
      "Specify the directory with additional libraries\n"
      "for platforms without pkg-config." ])
ac.ENVARS.append (
    [ "SDKDIR", "./glib-2.0", "The directory for additional libraries (default: ./glib-2.0)" ])

ac.start ()

if ac.TARGET [0] == "windows":
    # On Windows we don't have pkg-config so we just have to hope
    # that user has a installed SDK somewhere where we can find it ...
    ac.PKGCONFIG ["glib-2.0"] = {
        "modversion": "2.0",
        "cflags": ac.TOOLKIT.include (ac.SDKDIR + "/include/glib-2.0") + " " +
                  ac.TOOLKIT.include (ac.SDKDIR + "/lib/glib-2.0/include"),
        "libs": ac.TOOLKIT.linklib ("glib-2.0", ac.SDKDIR + "/lib")
    }
    ac.PKGCONFIG ["libpng"] = {
        "modversion": "1.0",
        "cflags": ac.TOOLKIT.include (ac.SDKDIR + "/include"),
        "libs": ac.TOOLKIT.linklib ("png", ac.SDKDIR + "/lib")
    }

# Check for tools
ac.check_program ("GNU Make", "make --version", ".*?([0-9\.]+).*", "3.81", True)
if ac.check_program ("makedep", "makedep -V", ".*Version.*?([0-9\.]+).*", "0.1.0"):
    ac.add_config_mak ("MAKEDEP", "makedep")
    makedep = "yes"
else:
    makedep = "no"

if ac.check_program ("Doxygen", "doxygen --version", "([0-9\.]+)", "1.5.0"):
    ac.add_config_mak ("DOXYGEN", "doxygen")
    doxygen = "yes"
else:
    doxygen = "no"

ac.pkgconfig_check_library ("glib-2.0", "2.0",
    "this is the low-level library that libLensFun relies on")

if ac.pkgconfig_check_library ("libpng", "1.0"):
    # Check for zlib: first check for the standard libz.a name,
    # and then check for the bastard windoz's "zlib.lib".
    if not ac.check_library ("zlib", None, None, None, "", ac.TOOLKIT.linklib ("z")):
        print "Re-trying with another library name ..."
        ac.check_library ("zlib", None, None, None, "", ac.TOOLKIT.linklib ("zlib"))

# On Windows we don't have regex.h, so use a copy from glibc instead.
if not ac.check_header ("regex.h"):
    ac.add_config_mak ("NEED_REGEX")

ac.add_config_mak ("TOOLKIT", ac.COMPILER.upper ())
ac.add_config_mak ("CFLAGS", ac.CFLAGS)
ac.add_config_mak ("CXXFLAGS", ac.CXXFLAGS)
ac.add_config_mak ("LDFLAGS", ac.LDFLAGS)
ac.add_config_mak ("LDLIBS", ac.LIBS)
ac.add_config_mak ("MODE", ac.MODE)
ac.add_config_mak ("GENFILES", "config.mak build/PKGBUILD " +
    "build/lensfun.spec include/config.h")

ac.update_file ("include/config.h", "\n".join (ac.CONFIG_H) + "\n")
ac.update_file ("config.mak", "\n".join (ac.CONFIG_MAK) + "\n")

ac.substmacros ("build/PKGBUILD.in")
ac.substmacros ("build/lensfun.spec.in")

print "--------------------------------------------------------------------"
print "Default build mode:                 " + ac.MODE
print "Build doxygen documentation:        " + doxygen
print "Use the pre-installed makedep:      " + makedep
print "Target directory for binaries:      " + ac.BINDIR
print "Target directory for data files:    " + ac.DATADIR
print "Target directory for libraries:     " + ac.LIBDIR
print "Target directory for include files: " + ac.INCLUDEDIR
print "Target directory for documentation: " + ac.DOCDIR
print "--------------------------------------------------------------------"
