#!/usr/bin/env python
# Waf build script for Samba 4's bundled Heimdal.

# Unless explicitly requested by the user (e.g.
# "./configure --bundled-libraries=!asn1_compile") this will always use the
# bundled Heimdal, even if a system heimdal was found. The reason
# for this is that our checks for the system heimdal are not accurate
# enough yet to know if it is usable (some bug fix might be missing,
# compile_et might not generate the expected code, etc).

import Logs, sys

conf.CHECK_TYPE('u_char', 'uint8_t')
conf.CHECK_TYPE('u_int32_t', 'uint32_t')

conf.CHECK_HEADERS('err.h')

conf.CHECK_HEADERS('ifaddrs.h')
conf.CHECK_HEADERS('''crypt.h errno.h inttypes.h netdb.h signal.h sys/bswap.h
                  sys/file.h sys/stropts.h sys/timeb.h sys/times.h sys/uio.h sys/un.h
              sys/utsname.h time.h timezone.h ttyname.h netinet/in.h
              netinet/in6.h netinet6/in6.h libintl.h''')

conf.CHECK_HEADERS('curses.h term.h termcap.h', together=True)

conf.CHECK_FUNCS('''atexit cgetent getprogname setprogname gethostname
                    putenv rcmd readv sendmsg setitimer strlwr strncasecmp
            strptime strsep strsep_copy    strtok_r strupr swab umask uname unsetenv
            closefrom err warn errx warnx flock writev''')

conf.CHECK_FUNCS_IN('hstrerror', 'resolv socket nsl', checklibc=True)
conf.CHECK_FUNCS_IN('''getnameinfo sendmsg socket getipnodebyname gethostent gethostent_r
                       sethostent endhostent getipnodebyaddr freehostent gethostbyname
                       gethostbyname_r gethostbyaddr''',
                    'socket nsl',
                    checklibc=True)

conf.CHECK_FUNCS_IN('dgettext gettext', 'intl', headers='libintl.h')

conf.CHECK_FUNCS('iruserok')

conf.CHECK_FUNCS('bswap16')
conf.CHECK_FUNCS('bswap32')

conf.CHECK_TYPE('struct winsize', define='HAVE_STRUCT_WINSIZE', headers='sys/termios.h sys/ioctl.h')
conf.CHECK_STRUCTURE_MEMBER('struct winsize', 'ws_xpixel',
                    define='HAVE_WS_XPIXEL', headers='sys/termios.h sys/ioctl.h')
conf.CHECK_STRUCTURE_MEMBER('struct winsize', 'ws_ypixel',
                    define='HAVE_WS_YPIXEL', headers='sys/termios.h sys/ioctl.h')
conf.DEFINE('HAVE_KRB_STRUCT_WINSIZE', 1)
conf.DEFINE('VOID_RETSIGTYPE', 1)

conf.CHECK_VARIABLE('h_errno', headers='netdb.h')

# strangely enough, we need it with another define too
conf.CHECK_DECLS('h_errno', headers='netdb.h')

conf.CHECK_FUNCS_IN('res_search res_nsearch res_ndestroy dns_search dn_expand', 'resolv',
                    checklibc=True, headers='netinet/in.h arpa/nameser.h resolv.h dns.h')
conf.CHECK_VARIABLE('_res', headers='netinet/in.h arpa/nameser.h resolv.h')
conf.CHECK_DECLS('_res', headers='netinet/in.h arpa/nameser.h resolv.h')
conf.CHECK_FUNCS_IN('openpty', 'util', checklibc=True, headers='pty.h util.h libutil.h')

conf.DEFINE('HAVE_KRB5',1)
conf.DEFINE('HAVE_GSSAPI',1)

conf.CHECK_FUNCS('dirfd', headers='dirent.h')
conf.CHECK_DECLS('dirfd', reverse=True, headers='dirent.h')
conf.CHECK_STRUCTURE_MEMBER('DIR', 'dd_fd', define='HAVE_DIR_DD_FD',  headers='dirent.h')

conf.DEFINE('SAMBA4_INTERNAL_HEIMDAL', 1)

if conf.CHECK_BUNDLED_SYSTEM('com_err', checkfunctions='com_right_r com_err', headers='com_err.h'):
    conf.define('USING_SYSTEM_COM_ERR', 1)

def check_system_heimdal_lib(name, functions='', headers='', onlyif=None):
    # Only use system library if the user requested the bundled one not be
    # used.
    if conf.LIB_MAY_BE_BUNDLED(name):
        return False
    setattr(conf.env, "CPPPATH_%s" % name.upper(), ["/usr/include/heimdal"])
    setattr(conf.env, "LIBPATH_%s" % name.upper(), ["/usr/lib/heimdal"])
    conf.CHECK_BUNDLED_SYSTEM(name, checkfunctions=functions, headers=headers,
        onlyif=onlyif)
    conf.define('USING_SYSTEM_%s' % name.upper(), 1)
    return True

def check_system_heimdal_binary(name):
    if conf.LIB_MAY_BE_BUNDLED(name):
        return False
    if not conf.find_program(name, var=name.upper()):
        return False
    conf.define('USING_SYSTEM_%s' % name.upper(), 1)
    return True

if check_system_heimdal_lib("roken", "rk_socket_set_reuseaddr", "roken.h"):
    conf.env.CPPPATH_ROKEN_HOSTCC = conf.env.CPPPATH_ROKEN
    conf.env.LIBPATH_ROKEN_HOSTCC = conf.env.LIBPATH_ROKEN
    conf.env.LIB_ROKEN_HOSTCC = "roken"
    conf.SET_TARGET_TYPE("ROKEN_HOSTCC", 'SYSLIB')
check_system_heimdal_lib("wind", "wind_stringprep", "wind.h", onlyif="roken")
check_system_heimdal_lib("hx509", "hx509_bitstring_print", "hx509.h", onlyif="roken wind")
check_system_heimdal_lib("asn1", "initialize_asn1_error_table", "asn1_err.h", onlyif="roken com_err")
check_system_heimdal_lib("heimbase", "heim_cmp", "heimbase.h", onlyif="roken")
check_system_heimdal_lib("hcrypto", "MD4_Init", "hcrypto/md4.h",
    onlyif="asn1 roken com_err")
check_system_heimdal_lib("krb5", "krb5_anyaddr", "krb5.h",
    onlyif="roken wind asn1 hx509 hcrypto com_err heimbase")
check_system_heimdal_lib("gssapi", "gss_oid_to_name", "gssapi.h",
    onlyif="hcrypto asn1 roken krb5 com_err wind")
check_system_heimdal_lib("heimntlm", "heim_ntlm_ntlmv2_key", "heimntlm.h",
    onlyif="roken hcrypto krb5")
check_system_heimdal_lib("hdb", "hdb_db_dir", "krb5.h hdb.h",
    onlyif="roken krb5 hcrypto com_err wind")
check_system_heimdal_lib("kdc", "kdc_log", "kdc.h",
    onlyif="roken krb5 hdb asn1 heimntlm hcrypto com_err wind heimbase")


# With the proper checks in place we should be able to build against the system libtommath.
# conf.CHECK_BUNDLED_SYSTEM('tommath', checkfunctions='mp_init', headers='tommath.h')
# conf.define('USING_SYSTEM_TOMMATH', 1)

check_system_heimdal_binary("compile_et")
check_system_heimdal_binary("asn1_compile")