#! /bin/sh
#
# configure
#
# Configuration script for POCO.
#
# Invoke configure --help for help.
#

showhelp()
{
cat << ENDHELP
usage: configure {options}
Configure the POCO C++ Libraries.

Options:
  --help
    Display this help screen.

  --config=<config_name>
    Use the given build configuration.
    Available configurations are:

$(ls -C "$base"/build/config/)

  --prefix=<install_prefix>
    Use the given install directory for make install.
    Default is /usr/local.

  --no-prefix
    Do not use /usr/local as default install directory and do not
    define POCO_PREFIX.

  --stdcxx-base=<apache_stdcxx_install_prefix>
    If (and only if) the $base/build/config selected with --config
    uses the Apache stdcxx library, then apache_stdcxx_install_prefix
    specifies the base directory where stdcxx is installed.

  --no-tests
    Do not build testsuites.

  --no-samples
    Do not build samples.

  --minimal
    Build only Foundation, XML, JSON, Util and Net.

  --typical (default)
    Build only Foundation, XML, JSON, Util, Net, Crypto, NetSSL, Data/SQLite and Zip.

  --everything
    Build everything.

  --no-wstring
    Compile with -DPOCO_NO_WSTRING.
    Useful if your C++ compiler does not support std::wstring
    (such as uClibc-based systems).

  --no-fpenvironment
    Compile with -DPOCO_NO_FPENVIRONMENT.
    Useful if your C++ compiler has incomplete floating-point support
    (such as uClibc-based systems).

  --no-sharedmemory
    Compile with -DPOCO_NO_SHAREDMEMORY.
    For systems that don't support shared memory API's,
    like uClibc-based Linux systems.

  --no-sharedlibs
    Compile with -DPOCO_NO_SHAREDLIBS.
    For systems that don't support shared library loading.

  --no-ipv6
    Compile with -DPOCO_NET_NO_IPv6.
    For systems that don't support IPv6.

  --no-soo
    Compile with -DPOCO_NO_SOO.
    Disables small object optimization.

  --no-sqlparser
    Compile with -DPOCO_DATA_NO_SQL_PARSER
    Disables compilation of the SQLParser.

  --mssql-bigstring
    Compile with -DPOCO_DATA_SQL_SERVER_BIG_STRINGS=1
    Enables strings over 8000 bytes on MS SQL Server.

  --sqlite-fts
    Compile with -DPOCO_DATA_SQLITE_FTS.
    Compile SQLite with Full Text Search support.

  --sqlite-thread-safe=<value>
    Compile with -DSQLITE_THREADSAFE=<value>.
    Valid values are:
		- 0 single-thread, no thread safety
		- 1 serial access, highest thread safety
		- 2 multi-threaded, without session sharing support


  --omit=<component>{,<component>}
    Do not build the specified component(s).
    Example: --omit=Data/MySQL,Data/ODBC,Zip

  --include-path=<path>
    Add search path for header files.

  --library-path=<path>
    Add search path for library files.

  --odbc-lib=<path>
    Specify the directory where ODBC library is located.

  --odbc-include=<path>
    Specify the directory where ODBC header files are located.

  --mssql-include=<path>
    Specify the directory where MS SQL Server ODBC header files are located.

  --mysql-lib=<path>
    Specify the directory where MySQL library is located.

  --mysql-include=<path>
    Specify the directory where MySQL header files are located.

  --pgsql-lib=<path>
    Specify the directory where PostgreSQL library is located.

  --pgsql-include=<path>
    Specify the directory where PostgreSQL header files are located.

  --cflags=<flags>
    Pass additional flags to compiler.
    Example: --cflags=-wall

  --ldflags=<flags>
    Pass additional flags to linker.
    Example: --ldflags=-latomic

  --poquito
    Omit a few features for smaller codesize when linking
    statically for embedded targets.

  --unbundled
    Use system-provided zlib, pcre, expat and sqlite instead of
    bundled ones.

  --trace
    Enable stack trace. Implicitly disables stripping in release build.

  --static
    Build static libraries. Overrides default mode, which
    depends upon target. Can be specified together
    with --shared to build both.

  --shared
    Build shared libraries. Overrides default mode, which
    depends upon target. Can be specified together
    with --static to build both.

ENDHELP
}

# save cwd
build=$(pwd)
# get directory where we are located
cd "$(dirname "$0")" || exit
base=$(pwd)
cd "$build" || exit

tests=1
samples=1
noprefix=0
flags=""
ldflags=""
includepath=""
librarypath=""
odbclib=""
odbcinclude=""
mssqlinclude=""
unbundled=""
trace=""
static=""
shared=""
nosqlparser=
mssqlbigstring=
omitMinimal="Crypto NetSSL_OpenSSL Zip Data Data/SQLite Data/ODBC Data/MySQL Data/PostgreSQL MongoDB Redis PDF DNSSD DNSSD/Avahi DNSSD/Bonjour CppParser PageCompiler"
omitTypical="Data/ODBC Data/MySQL Data/PostgreSQL MongoDB Redis PDF DNSSD DNSSD/Avahi DNSSD/Bonjour CppParser"
omit=$omitTypical
# parse arguments
while [ $# -ge 1 ]; do
	case "$1" in
	--config=*)
		config="$(echo "${1}" | awk '{print substr($0,10)}')" ;;

	--prefix=*)
		prefix="$(echo "${1}" | awk '{print substr($0,10)}')" ;;

	--no-prefix)
		noprefix=1 ;;

	--stdcxx-base=*)
		stdcxx_base="$(echo "${1}" | awk '{print substr($0,15)}')" ;;

	--omit=*)
		omit="$(echo "${1}" | awk '{print substr($0,8)}' | tr ',;' '  ')" ;;

	--include-path=*)
		includepath="$(echo "${1}" | awk '{print substr($0,16)}' | tr ',;' '  ')" ;;

	--library-path=*)
		librarypath="$(echo "${1}" | awk '{print substr($0,16)}' | tr ',;' '  ')" ;;

	--odbc-lib=*)
		odbclib="$(echo "${1}" | awk '{print substr($0,12)}')" ;;

	--odbc-include=*)
		odbcinclude="$(echo "${1}" | awk '{print substr($0,16)}')" ;;

	--mssql-include=*)
		mssqlinclude="$(echo "${1}" | awk '{print substr($0,16)}')" ;;

	--mysql-lib=*)
		mysqllib="$(echo "${1}" | awk '{print substr($0,13)}')" ;;

	--mysql-include=*)
		mysqlinclude="$(echo "${1}" | awk '{print substr($0,17)}')" ;;

	--pgsql-lib=*)
		pgsqllib="$(echo "${1}" | awk '{print substr($0,13)}')" ;;

	--pgsql-include=*)
		pgsqlinclude="$(echo "${1}" | awk '{print substr($0,17)}')" ;;

	--cflags=*)
		flags="$flags $(echo "${1}" | awk '{print substr($0,10)}')" ;;

	--ldflags=*)
		ldflags="$ldflags $(echo "${1}" | awk '{print substr($0,11)}')" ;;

	--no-samples)
		samples="" ;;

	--no-tests)
		tests="" ;;

	--no-wstring)
		flags="$flags -DPOCO_NO_WSTRING" ;;

	--no-fpenvironment)
		flags="$flags -DPOCO_NO_FPENVIRONMENT" ;;

	--no-sharedmemory)
		flags="$flags -DPOCO_NO_SHAREDMEMORY" ;;

	--no-sharedlibs)
		flags="$flags -DPOCO_NO_SHAREDLIBS" ;;

	--no-ipv6)
		flags="$flags -DPOCO_NET_NO_IPv6" ;;

	--no-soo)
		flags="$flags -DPOCO_NO_SOO" ;;

	--no-sqlparser)
		flags="$flags -DPOCO_DATA_NO_SQL_PARSER"
		nosqlparser=1
		;;

	--mssql-bigstring)
		flags="$flags -DPOCO_DATA_SQL_SERVER_BIG_STRINGS=1"
		mssqlbigstring=1
		;;

	--sqlite-thread-safe=*)
		flags="$flags -DSQLITE_THREADSAFE=$(echo "${1}" | awk '{print substr($0,22)}')" ;;

	--sqlite-fts)
		flags="$flags -DSQLITE_ENABLE_FTS5" ;;

	--poquito)
		flags="$flags -DPOCO_NO_FILECHANNEL -DPOCO_NO_SPLITTERCHANNEL -DPOCO_NO_SYSLOGCHANNEL -DPOCO_UTIL_NO_INIFILECONFIGURATION -DPOCO_UTIL_NO_JSONCONFIGURATION -DPOCO_UTIL_NO_XMLCONFIGURATION" ;;

	--unbundled)
		flags="$flags -DPOCO_UNBUNDLED"
		unbundled=1
		;;

	--trace)
		flags="$flags -DPOCO_ENABLE_TRACE"
		trace=1
		;;

	--minimal)
		omit=$omitMinimal ;;

	--typical)
		omit=$omitTypical ;;

	--everything)
		omit="" ;;

	--static)
		static=1 ;;

	--shared)
		shared=1 ;;

	--help)
 		showhelp
 		exit 0
		;;

	*)
		showhelp
		exit 1
		;;
	esac

 	shift
done
# autodetect build environment
# ...special cases for CYGWIN or MinGW
if [ "$config" = "" ] ; then
	config=$(uname)
	case "$config" in
	CYGWIN*)
		config=CYGWIN ;;
	MINGW*)
		config=MinGW ;;
	esac
fi

if [ ! -f "$base/build/config/$config" ] ; then
	echo "Unknown configuration: $config"
	echo "Please use the --config option to specify another build configuration"
	echo "The following configurations are available:"
	ls "$base"/build/config
	exit 1
fi

if [ -z "$prefix" ] ; then
	if [ $noprefix -eq 0 ] ; then
		prefix=/usr/local
	fi
fi

# check for patches
if [ -d "$base"/patches/"$config" ] ; then
	echo "NOTE: There are patches for your configuration available. Please apply them before compiling."
fi

# copy Makefile to build dir
if [ "$base" != "$build" ] ; then
	cp "$base"/Makefile "$build"
fi

if [ -n "$static" ] && [ -n "$shared" ] ; then
	linkmode=BOTH
elif [ -n "$static" ] ; then
	linkmode=STATIC
elif [ -n "$shared" ] ; then
	linkmode=SHARED
else
	linkmode=""
fi

# create config.make
echo '# config.make generated by configure script' > "$build"/config.make
{
	echo "POCO_CONFIG = $config"
	echo "POCO_BASE = $base"
	echo "POCO_BUILD = $build"
	echo "POCO_FLAGS = $flags"
	echo "POCO_LDFLAGS = $ldflags"
} >> "$build"/config.make

if [ -n "$prefix" ] ; then
	echo "POCO_PREFIX = $prefix" >>"$build"/config.make
fi
echo "OMIT = $omit" >>"$build"/config.make
if [ -n "$stdcxx_base" ] ; then
	echo "STDCXX_BASE = $stdcxx_base" >>"$build"/config.make
fi
if [ -n "$includepath" ] ; then
	echo "POCO_ADD_INCLUDE = $includepath" >>"$build"/config.make
fi
if [ -n "$librarypath" ] ; then
	echo "POCO_ADD_LIBRARY = $librarypath" >>"$build"/config.make
fi
if [ -n "$odbclib" ] ; then
	echo "POCO_ODBC_LIB = $odbclib" >>"$build"/config.make
fi
if [ -n "$odbcinclude" ] ; then
	echo "POCO_ODBC_INCLUDE = $odbcinclude" >>"$build"/config.make
fi
if [ -n "$mssqlinclude" ] ; then
	echo "POCO_MSSQL_INCLUDE = $mssqlinclude" >>"$build"/config.make
fi
if [ -n "$mysqllib" ] ; then
	echo "POCO_MYSQL_LIB = $mysqllib" >>"$build"/config.make
fi
if [ -n "$mysqlinclude" ] ; then
	echo "POCO_MYSQL_INCLUDE = $mysqlinclude" >>"$build"/config.make
fi
if [ -n "$pgsqllib" ] ; then
	echo "POCO_PGSQL_LIB = $pgsqllib" >>"$build"/config.make
fi
if [ -n "$pgsqlinclude" ] ; then
	echo "POCO_PGSQL_INCLUDE = $pgsqlinclude" >>"$build"/config.make
fi
if [ -n "$unbundled" ] ; then
	echo "POCO_UNBUNDLED = 1" >>"$build"/config.make
fi
if [ -n "$trace" ] ; then
	echo "POCO_ENABLE_TRACE = 1" >>"$build"/config.make
fi
if [ -n "$linkmode" ] ; then
	echo "LINKMODE = $linkmode" >>"$build"/config.make
fi
if [ -n "$nosqlparser" ] ; then
	echo "POCO_DATA_NO_SQL_PARSER = $nosqlparser" >>"$build"/config.make
fi
if [ -n "$mssqlbigstring" ] ; then
	echo "POCO_DATA_SQL_SERVER_BIG_STRINGS = $mssqlbigstring" >>"$build"/config.make
fi

cat <<__EOF__ >>"$build"/config.make
export POCO_CONFIG
export POCO_BASE
export POCO_BUILD
export POCO_FLAGS
export POCO_LDFLAGS
__EOF__

if [ -n "$prefix" ] ; then
	echo "export POCO_PREFIX" >>"$build"/config.make
fi
if [ -n "$stdcxx_base" ] ; then
	echo "export STDCXX_BASE" >>"$build"/config.make
fi
if [ -n "$includepath" ] ; then
	echo "export POCO_ADD_INCLUDE" >>"$build"/config.make
fi
if [ -n "$librarypath" ] ; then
	echo "export POCO_ADD_LIBRARY" >>"$build"/config.make
fi
if [ -n "$odbclib" ] ; then
	echo "export POCO_ODBC_LIB" >>"$build"/config.make
fi
if [ -n "$odbcinclude" ] ; then
	echo "export POCO_ODBC_INCLUDE" >>"$build"/config.make
fi
if [ -n "$mssqlinclude" ] ; then
	echo "export POCO_MSSQL_INCLUDE" >>"$build"/config.make
fi
if [ -n "$mysqllib" ] ; then
	echo "export POCO_MYSQL_LIB" >>"$build"/config.make
fi
if [ -n "$mysqlinclude" ] ; then
	echo "export POCO_MYSQL_INCLUDE" >>"$build"/config.make
fi
if [ -n "$pgsqllib" ] ; then
	echo "export POCO_PGSQL_LIB" >>"$build"/config.make
fi
if [ -n "$pgsqlinclude" ] ; then
	echo "export POCO_PGSQL_INCLUDE" >>"$build"/config.make
fi
if [ -n "$unbundled" ] ; then
	echo "export POCO_UNBUNDLED" >>"$build"/config.make
fi
if [ -n "$trace" ] ; then
	echo "export POCO_ENABLE_TRACE" >>"$build"/config.make
fi
if [ -n "$linkmode" ] ; then
	echo "export LINKMODE" >>"$build"/config.make
fi
if [ -n "$nosqlparser" ] ; then
	echo "export POCO_DATA_NO_SQL_PARSER" >>"$build"/config.make
fi
if [ -n "$mssqlbigstring" ] ; then
	echo "POCO_DATA_SQL_SERVER_BIG_STRINGS=$mssqlbigstring" >>"$build"/config.make
fi
# create config.build
echo '# config.build generated by configure script' >"$build"/config.build
cat <<__EOF__ >>"$build"/config.build
TESTS = $tests
SAMPLES = $samples
__EOF__

echo "Configured for $config"
echo "======================"
echo
cat "$build"/config.make
echo
cat "$build"/config.build
echo
echo "======================"
