#!/usr/local/bin/python3.13
import os


def configure(conf):
    AR32 = ['i386', 'i586', 'i686']
    AR64 = ['x86_64', 'amd64']
    TC = ['mingw32', 'mingw32msvc', 'w64-mingw32']

    found = False

    for a in AR32:
        for t in TC:
            if conf.find_program(a + '-' + t + '-gcc', var='WINEXE_CC_WIN32'):
                found = True
                break
        if found:
            conf.DEFINE('HAVE_WINEXE_CC_WIN32', 1);
            break

    found = False

    for a in AR64:
        for t in TC:
            if conf.find_program(a + '-' + t + '-gcc', var='WINEXE_CC_WIN64'):
                found = True
                break
        if found:
            conf.DEFINE('HAVE_WINEXE_CC_WIN64', 1);
            break

    source_date_epoch = os.environ.get('SOURCE_DATE_EPOCH')
    if source_date_epoch is None:
        # We use the version to make up the timestamp that will be
        # embedded in winexe.exe, to keep the build reproducible.
        #
        # This is less evil than it sounds. According to Raymond Chen in
        # https://devblogs.microsoft.com/oldnewthing/20180103-00/?p=97705
        # since Windows 10 the timestamp has been randomised.
        #
        # The purpose of the timestamp in Windows PE files seems to be
        # to make spotting ABI changes in DLLs quicker, for which a
        # random number is just as good as a real time. The timestamp
        # in .exe files is not used.
        import samba_version
        v = samba_version.load_version(env=conf.env)
        version = (v.MAJOR << 16) | (v.MINOR << 8) | v.RELEASE
        source_date_epoch = str(version)

    conf.env.SOURCE_DATE_EPOCH = source_date_epoch
    conf.DEFINE("WINEXE_LDFLAGS",
                "-s -Wall -Wl,-Bstatic -Wl,-Bdynamic -luserenv")
