#!/bin/sh

# Configure some things for GKrellM when make is run.
# This configure is run automatically so no need to run it by hand.
#
# Copyright (C) 2003-2009  Bill Wilson
set -e

for i
do
	if [ "$i" = "--without-gnutls" ]
	then
		without_gnutls=yes
	fi
	if [ "$i" = "--without-ssl" ]
	then
		without_ssl=yes
	fi
	if [ "$i" = "--without-libsensors" ]
	then
		without_libsensors=yes
	fi
	if [ "$i" = "--without-ntlm" ]
	then
		without_ntlm=yes
	fi
done

rm -f configure.h configure.mk configure.log

touch configure.h
touch configure.mk
exec 5>./configure.log

CC=${CC-gcc}
PKG_CONFIG=${PKG_CONFIG-pkg-config}

echo "CC    : ${CC}" 1>& 5
echo "CFLAGS: ${CFLAGS}" 1>& 5
echo "PKG_CONFIG: ${PKG_CONFIG}" 1>& 5

rm -f test test.exe test.o test.c

if [ "$without_ssl" != "yes" ]
then
echo -n "Checking for OpenSSL... "
echo "Checking for OpenSSL... " 1>& 5

OPENSSL_INCLUDE="$(${PKG_CONFIG} openssl --cflags 2>& 5 || true)"
OPENSSL_LIBS="$(${PKG_CONFIG} openssl --libs 2>& 5 || true)"
if [ -z "$OPENSSL_LIBS" ]
then
	echo "OpenSSL not found via pkg-config, using hardcoded library names" 1>& 5
	OPENSSL_LIBS="-lssl -lcrypto"
fi

cat << EOF > test.c
#include <openssl/ssl.h>

int main()
	{
	const SSL_METHOD	*ssl_method  = NULL;

	SSLeay_add_ssl_algorithms();
	SSL_load_error_strings();
	if ((ssl_method = SSLv23_client_method()) == NULL)
		return 1;
	return 0;
	}
EOF

$CC ${CFLAGS} ${OPENSSL_INCLUDE} -c test.c -o test.o 2>& 5 || true
$CC test.o -o test ${LINK_FLAGS} ${OPENSSL_LIBS} 2>& 5 || true

if [ -x ./test ]
then
	echo "Found"
	echo 'Defining HAVE_SSL' 1>& 5
	echo '#define HAVE_SSL 1' >> configure.h
	echo 'HAVE_SSL=1' >> configure.mk
	echo "SSL_LIBS=${OPENSSL_LIBS}" >> configure.mk
	echo "SSL_INCLUDE=${OPENSSL_INCLUDE}" >> configure.mk
	without_gnutls="yes"
else
	echo "Not found"
	echo "OpenSSL not found..." 1>& 5
fi
fi
# end of ssl check

rm -f test test.exe test.o test.c

if [ "$without_gnutls" != "yes" ]
then
echo -n "Checking for gnutls... "
echo "Checking for gnutls... " 1>& 5

GNUTLS_INCLUDE="$(${PKG_CONFIG} gnutls --cflags 2>& 5 || true)"
GNUTLS_LIBS="$(${PKG_CONFIG} gnutls --libs 2>& 5 || true) -lgnutls-openssl"

# GnuTLS >= 2.12 does not need explicit gcrypt linking anymore
gcrypt_h=""
if $(${PKG_CONFIG} --max-version=2.11.99 gnutls 2>& 5 || false); then
	GNUTLS_LIBS="$GNUTLS_LIBS -lgcrypt"
	gcrypt_h="#include <gcrypt.h>"
fi

cat << EOF > test.c
#include <gnutls/openssl.h>
$gcrypt_h
#include <errno.h>

int main()
	{
	SSL_METHOD	*ssl_method  = NULL;

	gnutls_global_init();

	SSLeay_add_ssl_algorithms();
	SSL_load_error_strings();
	if ((ssl_method = SSLv23_client_method()) == NULL)
		return 1;
	return 0;
	}
EOF

${CC} ${CFLAGS} ${GNUTLS_INCLUDE} -c test.c -o test.o 2>& 5 || true
${CC} test.o -o test ${LINK_FLAGS} ${GNUTLS_LIBS} 2>& 5 || true

if [ -x ./test ]
then
	echo "Found"
	echo 'Defining HAVE_GNUTLS' 1>& 5
	echo '#define HAVE_GNUTLS 1' >> configure.h
	echo '#define HAVE_SSL 1' >> configure.h
	echo 'HAVE_SSL=1' >> configure.mk
	echo "SSL_LIBS=${GNUTLS_LIBS}" >> configure.mk
	echo "SSL_INCLUDE=${GNUTLS_INCLUDE}" >> configure.mk
else
	echo "Not found"
	echo "GnuTLS not found..." 1>& 5
fi
fi
# end of gnutls check


rm -f test test.exe test.o test.c

if [ "$without_ntlm" != "yes" ]
then
echo -n "Checking for libntlm... "
echo "Checking for libntlm... " 1>& 5

PKG_NTLM_INCLUDE=$(${PKG_CONFIG} libntlm --cflags 2>& 5 || true)
PKG_NTLM_LIBS=$(${PKG_CONFIG} libntlm --libs 2>& 5 || true)

cat << EOF > test.c
#include <ntlm.h>

int main()
	{
	tSmbNtlmAuthRequest	request;

	buildSmbNtlmAuthRequest(&request, "username", NULL);
	return 0;
	}
EOF

${CC} ${CFLAGS} ${PKG_NTLM_INCLUDE} -c test.c -o test.o 2>& 5 || true
${CC} test.o -o test ${LINK_FLAGS} ${PKG_NTLM_LIBS} 2>& 5 || true

if [ -x ./test ]
then
	echo "Found"
	echo 'Defining HAVE_NTLM' 1>& 5
	echo '#define HAVE_NTLM 1' >> configure.h
	echo 'HAVE_NTLM=1' >> configure.mk
	echo "NTLM_LIBS=${PKG_NTLM_LIBS}" >> configure.mk
	echo "NTLM_INCLUDE=${PKG_NTLM_INCLUDE}" >> configure.mk
else
	echo "Not found"
	echo "Not found, mail check will not have ntlm support..." 1>& 5
fi
fi
# end of ntlm check


rm -f test test.exe test.o test.c

if [ "$without_libsensors" != "yes" ]
then
echo -n "Checking for libsensors... "
echo "Checking for libsensors... " 1>& 5

cat << EOF > test.c
#include <stdio.h>
#include <sensors/sensors.h>

int main()
	{
#if SENSORS_API_VERSION < 0x400 /* libsensors 3 code */
	FILE	*f;

	f = fopen("/etc/sensors.conf", "r");
	if (!f)
		return 1;
	if (sensors_init(f) != 0)
		return 1;
	fclose(f);
	return 0;
#else /* libsensors 4 code */
	if (sensors_init(NULL) != 0)
		return 1;
	return 0;
#endif
	}
EOF

${CC} ${CFLAGS} -c test.c -o test.o 2>& 5 || true
${CC} test.o -o test ${LINK_FLAGS} -lsensors 2>& 5 || true

if [ -x ./test ]
then
	echo "Found"
	echo 'Defining HAVE_LIBSENSORS' 1>& 5
	echo '#define HAVE_LIBSENSORS 1' >> configure.h
	echo 'HAVE_LIBSENSORS=1' >> configure.mk
	echo 'SENSORS_LIBS="-lsensors"' >> configure.mk
else
	echo "Not found"
	echo "Not found, sensors will not have libsensors support..." 1>& 5
fi
fi
# end of libsensors check


rm -f test test.exe test.o test.c

exit 0
