#!/bin/sh
#
# $Id: configure,v 1.55 2007/07/03 11:54:38 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/kl/cont/gb/html/makl.html    "
    echo "           for download, installation and configuration information. "
    echo "====================================================================="
    exit 1
fi

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

makl_args_init "$@"

# Custom arguments ...
makl_args_def "enable_debug"    "" "" "enables debugging"
makl_args_def "enable_profile"  "" "" "enables profiling"
makl_args_def "enable_warns"    "" "" "set CFLAGS for extra warnings"
makl_args_def "no_hmap"         "" "" "disable hmap module"
makl_args_def "no_config"       "" "" "disable config module"
makl_args_def "no_net"          "" "" "disable net module"
makl_args_def "no_env"          "" "" "disable env module"
makl_args_def "no_ipv6"         "" "" "disable IPv6 support"
makl_args_def "no_unixsock"     "" "" "disable Unix socket support"
# ... and their handlers
__makl_enable_debug ()   
{ 
    makl_set_var_mk    "LIBU_DEBUG" ;
    makl_append_var_mk "CFLAGS" "-g -DDEBUG" ; 
}
__makl_enable_profile () { makl_append_var_mk "CFLAGS" "-pg" ; }
__makl_enable_warns ()
{
    makl_append_var_mk "CFLAGS" "-std=c99 -W -Wall -Wstrict-prototypes "    \
                                "-Wmissing-prototypes -Wpointer-arith "     \
                                "-Wno-uninitialized -Wreturn-type "         \
                                "-Wwrite-strings -Wswitch -Wshadow "        \
                                "-Wwrite-strings -Wunreachable-code "       \
                                "-Wunused -Wcast-qual -pedantic "
}
__makl_no_hmap ()           { makl_set_var "NO_HMAP" ; }
__makl_no_config ()         { makl_set_var "NO_CONFIG" ; }
__makl_no_net ()            { makl_set_var "NO_NET" ; }
__makl_no_env ()            { makl_set_var "NO_ENV" ; }
__makl_no_ipv6 ()           { makl_set_var "NO_IPV6" ; }
__makl_no_unixsock ()       { makl_set_var "NO_UNIXSOCK" ; }

# 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   "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

# check for IPv4, IPv6 and Unix socket support
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

# Handle command-line arguments
makl_args_handle "$@"

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
