#!/bin/sh
#
# $Id: configure,v 1.61 2006/12/19 14:41:00 tat Exp $

export makl_conf_h=klone_conf.h
export MAKL_DIR=`pwd`/makl

# before running configure the toolchain must be present
if [ ! -r ${MAKL_DIR}/etc/toolchain.cf ]; then
    echo
    echo " Run \"make toolchain\" (using GNU make) before \"./configure\"!"
    echo
    exit 1
fi

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

# Preprocess arguments
makl_args_init "$@"

# Custom arguments ...
makl_args_def   "enable_debug"      "" ""   "enables debugging of KLone app"
makl_args_def   "enable_libu_debug" "" ""   "enables debugging of LibU"
makl_args_def   "enable_libu_mini"  "" ""   "enables minimisation of LibU"
makl_args_def   "enable_profile"    "" ""   "enables profiling"
makl_args_def   "enable_sup_fs"     "" ""   "enables filesystem web access"
makl_args_def   "enable_warns"      "" ""   "set CFLAGS for extra warnings"
makl_args_def   "disable_cxx"       "" ""   "disable C++ support"

# ... and their handlers
__makl_enable_debug ()      { makl_set_var_mk     "KLONE_DEBUG" ;
                              makl_append_var_mk  "CFLAGS" "-g -DDEBUG" ; }
__makl_enable_libu_debug () { makl_set_var_mk     "LIBU_DEBUG" ; }
__makl_disable_libu_mini () { makl_unset_var_mk   "LIBU_MINI" ; }
__makl_enable_profile ()    { makl_append_var_mk  "CFLAGS" "-pg -a" ; }
__makl_enable_sup_fs ()     { makl_set_var_h      "ENABLE_SUP_FS" ; }
__makl_disable_cxx ()       { makl_unset_var      "USE_CXX" ; }
__makl_enable_warns ()        
{ 
    makl_append_var_mk "CFLAGS"                                     \
        "-W -Wall -Wstrict-prototypes -Wmissing-prototypes"         \
        "-Wpointer-arith -Wno-uninitialized -Wreturn-type"          \
        "-Wwrite-strings -Wswitch -Wshadow -Wwrite-strings"         \
        "-Wunreachable-code -Wunused -Wcast-qual"                   \
        "-pedantic"
        #"-std=c99"
}

# General settings
makl_pkg_name           "klone"
makl_pkg_version

# Source directory 
makl_set_var_mk         "SRCDIR"    "`pwd`"

# Add local 3rd party flags
makl_append_var_mk      "CFLAGS"    "-I`pwd`/libu/include/"

case `makl_target_name` in

    *mingw*)
        makl_set_var        "OS_WIN"
        makl_append_var_mk  "POST_LDADD"      "-lwsock32 -L/c/WINDOWS" 
        ;;

    *qnx*)
        makl_set_var        "OS_UNIX"
        makl_append_var_mk  "POST_LDADD"      "-lsocket"
        ;;

    *darwin*)
        # workaround to avoid circular dependency errors on Mac OS X
        makl_set_var        "PRE_LDADD" "-ldl"
        ;;                                                    

    *)
        makl_set_var        "OS_UNIX"
        ;;
esac

makl_append_var_mk "CFLAGS"   "-I`pwd` -DHAVE_CONF_H"


# pick up just what we need from LibU (unless --disable_libu_mini is set)
makl_set_var_mk "LIBU_MINI" "1"

# these enable .klx pages
makl_set_var_mk "USE_CXX"  "1"
makl_append_var_mk "CXXFLAGS" "\${CFLAGS}"

# Install headers in a private subdir
makl_append_var_mk "INCDIR"   "\${DESTDIR}/include/klone"

# check for stdint header
makl_checkheader        0   "stdint"    "<stdint.h>"
makl_checkheader        0   "getopt"    "<getopt.h>"
makl_checkheader        0   "unistd"    "<unistd.h>"

# Library dependencies
makl_optional           1   "lib"   "openssl"   ""  "-lssl -lcrypto"
makl_optional           1   "lib"   "z"

# Function dependencies
makl_checktype          0   "pid_t"     "<sys/types.h>" 
makl_checkfunc          0   "syslog"
makl_checkfunc          0   "mkstemp"

makl_checkinline        0
if [ $? -ne 0 ]; then
    makl_append_var_mk "CFLAGS" "-Dinline=\"\""
fi

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

# Handle command-line arguments
makl_args_handle "$@"

# Add include dirs into top-level CFLAGS
makl_add_var_mk "CFLAGS" "\$(LIBOPENSSL_CFLAGS)"
makl_add_var_mk "CFLAGS" "\$(LIBZ_CFLAGS)"

# if debug is not enabled add optimization flags
if [ -z "`makl_get_var_mk "KLONE_DEBUG"`" ]; then
    makl_append_var_mk "CFLAGS"   "-O2"
fi


. ${MAKL_DIR}/cf/makl.term
