#!/bin/sh 
#
# $Id: configure,v 1.60 2008/01/03 14:21:48 tho Exp $

export makl_conf_h="include/u/libu_conf.h"

# Be sure MAKL_DIR is there.
if [ -z "${MAKL_DIR}" ]; then
    echo "====================================================================="
    echo "  WARNING: it seems that MaKL is not installed/configured in your    "
    echo "           environment.                                              "
    echo "           See http://www.koanlogic.com/makl/index.html              "
    echo "           for download, installation and configuration information. "
    echo "====================================================================="
    exit 1
fi

# MaKL preamble
. ${MAKL_DIR}/cf/makl.init

makl_args_init "$@"

# command line options (see relevant mk_* files in build/)
. build/mk_cflags
. build/mk_modules
. build/mk_support

# General settings (version is read from the VERSION file)
makl_pkg_name "libu"
makl_pkg_version

# Set variables depending on host OS
target=`makl_target_name`
case ${target} in
    *mingw* | *MINGW*)
        makl_set_var_h "OS_WIN"
        # use the hex value to avoid backspace escaping madness
        makl_set_var_h "U_PATH_SEPARATOR" "0x5c"
        ;;
    *)
        makl_set_var_h "OS_UNIX"
        makl_set_var_h "U_PATH_SEPARATOR" "'/'"
        if [ `echo $target | grep darwin` ]; then
            makl_set_var_mk "OS_DARWIN" "true"
            makl_set_var_mk "SHLIB_LDFLAGS" \
                "-undefined suppress -flat_namespace"
        fi
        ;;
esac

# C Flags
makl_append_var_mk "CFLAGS" "-DHAVE_CONF_H -I`pwd`/include"

# Binary dependencies
makl_optional           1    "featx"   "doxygen"    "PATH_DOXYGEN"

## Function dependencies
makl_checktype          0   "pid_t"     "<sys/types.h>" 
makl_checktype          0   "ssize_t"   "<sys/types.h>" 
makl_checktype          0   "in_addr_t"         \
                            "<sys/types.h>"     \
                            "<sys/socket.h>"    \
                            "<netinet/in.h>"    \
                            "<arpa/inet.h>"
makl_checkfunc          0   "getpid"
makl_checkfunc          0   "link"
makl_checkfunc          0   "unlink"
makl_checkfunc          0   "syslog"
makl_checkfunc          0   "sleep"
makl_checkfunc          0   "strtok_r"
makl_checkfunc          0   "fnmatch"
makl_checkfunc          0   "timegm"
makl_checkfunc          0   "strsep"
makl_checkfunc          0   "strlcpy"
makl_checkfunc          0   "strlcat"
makl_checkfunc          0   "gettimeofday"
makl_checkfunc          0   "daemon"
makl_checkfunc          0   "mkstemps"
makl_func_strerror_r    0
makl_checkheader        0   "unistd"    "<unistd.h>"
makl_checkheader        0   "stdlib"    "<stdlib.h>"
makl_checkheader        0   "paths"     "<paths.h>"
makl_checkheader        0   "sysuio"    "<sys/types.h>" "<sys/uio.h>"
makl_checkheader        0   "strings"   "<strings.h>"
makl_checktmzone        0
makl_checkextvar        0   "optarg"
makl_checkextvar        0   "optind"
makl_checkinline        0
if [ $? -ne 0 ]; then
    makl_append_var_mk "CFLAGS" "-Dinline=\"\""
fi

# Handle command-line arguments
makl_args_handle "$@"

# check for IPv4, IPv6 and Unix socket support 
# these checks are disabled when --no_net is requested in which case NO_IPV6
# and NO_UNIXSOCK variables are asserted by default
if [ -z "`makl_get_var_h "NO_NET"`" ]
then
    if [ -z "`makl_get_var_h "OS_WIN"`" ]; then
        makl_checktype      1   "struct sockaddr_in"    \
            "<sys/types.h> <sys/socket.h> <netinet/in.h>" 
    fi

    # check for IPv6 support
    makl_checktype          0   "struct sockaddr_in6"   \
        "<sys/types.h> <sys/socket.h> <netinet/in.h>" 
    if [ $? -ne 0 ]; then
        makl_set_var "NO_IPV6" 
    fi

    # check for Unix socket support
    makl_checktype          0   "struct sockaddr_un"    \
        "<sys/types.h> <sys/socket.h> <netinet/in.h> <sys/un.h>" 
    if [ $? -ne 0 ]; then
        makl_set_var "NO_UNIXSOCK" 
    fi
else
    # user supplied the --no_net argument
    makl_set_var "NO_UNIXSOCK"
    makl_set_var "NO_IPV6"
fi

if [ -z "`makl_get_var_mk "LIBU_DEBUG"`" ]; then
    makl_append_var_mk "CFLAGS" "-O2"
fi

# Install headers in a private subdir
makl_set_var "INCDIR" "`makl_get_var_mk \"INCDIR\"`/u"

# MaKL closing
. ${MAKL_DIR}/cf/makl.term
