# Copyright (C) 2006-2011 G.P. Halkes
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

EXTENSIONS="c verbose_compile gettext"

SWITCHES="+unicode +dwfilter"

COMPILERULE='$(CC) $(CFLAGS) $(GETTEXTFLAGS) $(ICUFLAGS) -c -o $@ $<'
LINKRULE='$(CC) $(CFLAGS) $(LDFLAGS) -o .config .config.o $(LDLIBS) $(GETTEXTLIBS) $(ICULIBS)'

DEFAULT_LINGUAS="de es fr nl"
[ -f config.pkg.langpack ] && . config.pkg.langpack

USER_HELP="  --without-unicode  Disable Unicode support

Environment variables:
  LINGUAS            List of languages to install. Available languages are:
    ${DEFAULT_LINGUAS}"

checkfunction() {
	clean .config.o

	CHECKFOR="$1"
	CODE="$2"
	shift 2
	{
		for INCLUDE
		do
			echo "#include ${INCLUDE}"
		done
		cat <<EOF
int main(int argc, char *argv[]) {
	${CODE}
	return 0;
}
EOF
	} > .config.c

	clean .config.o
	test_link "${CHECKFOR}"
}

config() {
	# Test for all required functionality

	cat > .config.c <<EOF
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[]) {
	int fd;
	char buffer[10];
	fd = mkstemp("fooXXXXXX");
	fd = open("foo", O_RDONLY);
	read(fd, buffer, 10);
	lseek(fd, 0, SEEK_SET);
	write(fd, buffer, 10);
	close(fd);
	umask(~S_IRWXU);
	snprintf(buffer, 10, "%s", "foo");
	return 0;
}
EOF

	clean .config.o

	test_link "required functions" || {
		check_message_result "Testing required functions seperatly for debugging purposes"

		checkfunction "mkstemp" 'int fd; fd = mkstemp("fooXXXXXX");' "<stdlib.h>"
		checkfunction "open" 'int fd; fd = open("foo", O_RDONLY);' "<unistd.h>" "<fcntl.h>"
		checkfunction "read" 'int fd; char buffer[10]; read(fd, buffer, 10);' "<unistd.h>"
		checkfunction "lseek" "int fd; lseek(fd, 0, SEEK_SET);" "<unistd.h>"
		checkfunction "write" "int fd; char buffer[10]; write(fd, buffer, 10);" "<unistd.h>"
		checkfunction "close" 'int fd; close(fd);' "<unistd.h>"
		checkfunction "umask" 'umask(~S_IRWXU);' "<sys/stat.h>"
		checkfunction "snprintf" 'char buffer[10]; snprintf(buffer, 10, "%s", "foo");' "<stdio.h>"
		check_message_result "!! A required function is not available."
		exit 1
	}

	if [ "yes" = "${with_dwfilter}" ] ; then
		cat > .config.c <<EOF
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
	pid_t pid = fork();
	execvp("test", NULL);
}
EOF
		clean .config.o

		test_link "required functions for dwfilter" || {
			checkfunction "fork" 'pid_t pid = fork();' "<unistd.h>" "<sys/types.h>"
			checkfunction "execvp" 'execvp("test", NULL);' "<unistd.h>"
			check_message_result "!! A required function for dwfilter is not availabe. Try configuring with --without-dwfilter"
			exit 1
		}
		DWFILTER="DWFILTER=yes"
	else
		DWFILTER="DWFILTER="
	fi

	checkfunction "strdup" "strdup(\"foo\");" "<string.h>" || CFLAGS="${CFLAGS} -DNO_STRDUP"

	if [ "yes" = "${with_gettext}" ] || [ "yes" = "${with_unicode}" ] ; then
		checkfunction "setlocale" "setlocale(LC_ALL, \"\");" "<locale.h>" || {
			check_message_result "!! Gettext and Unicode support unavailable. Try configuring with --without-gettext --without-unicode"
			exit 1
		}
	fi

	if [ "yes" = "${with_unicode}" ] ; then
		{
      check_message "Checking for ICU pkg-config... "
			{
				echo "Running pkg-config --cflags --libs icu-uc" >> config.log
				if pkg-config --cflags --libs icu-uc 2>&1 > /dev/null ; then
					check_message_result "yes"
					true
				else
					check_message_result "no"
					false
				fi
			} && {
				cat > .config.c <<EOF
#include <unicode/uchar.h>
#include <unicode/unorm2.h>
#include <unicode/ustring.h>

int main(int argc, char *argv[]) {
	UChar32 c;
	UChar *a, *b;
	UErrorCode error = U_ZERO_ERROR;

	u_isUWhiteSpace(c);
	U_GET_GC_MASK(c);
	u_strFoldCase(a, 10, b, 10, U_FOLD_CASE_DEFAULT, &error);
	unorm2_normalize(unorm2_getNFDInstance(&error), a, 10, b, 10, &error);
	u_getIntPropertyValue(c, UCHAR_GRAPHEME_CLUSTER_BREAK);
	return 0;
}
EOF
				clean .config.o
				test_link "functions in libicu" ICUFLAGS="\`pkg-config --cflags icu-uc\`" ICULIBS="\`pkg-config --libs icu-uc\`"
			}
		} || {
			check_message_result "!! Could not compile with Unicode support. Try configuring with --without-unicode."
			exit 1
		}

		ICUFLAGS="-DUSE_UNICODE \`pkg-config --cflags icu-uc\`"
		ICULIBS="\`pkg-config --libs icu-uc\`"
		if checkfunction "nl_langinfo" "nl_langinfo(CODESET);" "<langinfo.h>" ; then
			ICUFLAGS="${ICUFLAGS} -DUSE_NL_LANGINFO"
		fi
	fi

	create_makefile ${option_localedir:+"LOCALEDIR=${option_localedir}"} \
		"ICUFLAGS=${ICUFLAGS}" "ICULIBS=${ICULIBS}" $DWFILTER
}

sed_lines() {
	add_replace_settings "$@"
}
