AC_INIT()

AC_CONFIG_HEADER(common/c_config.h)
AC_CONFIG_AUX_DIR(script)

AC_CANONICAL_HOST
AC_CANONICAL_BUILD

# for CFLAGS="..." ./configure ...
AC_SUBST(CFLAGS)

if test -d ${top_srcdir-$srcdir}/mkf -a -d ${top_srcdir-$srcdir}/kiklib ; then
	SUBDIRS="kiklib mkf"
	
	MKF_CFLAGS='-I${top_builddir}/mkf/include'
	KIK_CFLAGS='-I${top_builddir}/kiklib/include'

	LMKF='${top_builddir}/mkf/lib/libmkf.la'
	LKIK='${top_builddir}/kiklib/src/libkik.la'
else
	LMKF="-lmkf"
	LKIK="-lkik"
fi

AC_SUBST(LMKF)
AC_SUBST(MKF_CFLAGS)
AC_SUBST(LKIK)
AC_SUBST(KIK_CFLAGS)

AC_PATH_TOOL(PKG_CONFIG, pkg-config)

AC_PROG_CC
AC_PROG_INSTALL

AC_C_INLINE

AC_LIBTOOL_WIN32_DLL
AC_LIBTOOL_DLOPEN
AC_ARG_WITH(libtool,
	[  --with-libtool@<:@=ARG@:>@    libtool path @<:@default=without@:>@],
	libtool=$with_libtool)
if test "${libtool}" != "" ; then
	LIBTOOL=${libtool}
else
	AM_PROG_LIBTOOL
	LIBTOOL='${top_builddir}/libtool'
fi
AC_SUBST(LIBTOOL)

AC_CHECK_LIB(socket,connect,SOCK_LIBS=-lsocket)
AC_SUBST(SOCK_LIBS)

AC_PATH_XTRA

AC_ARG_WITH(gui,
	[  --with-gui@<:@=ARG@:>@        gui library(xlib|win32)],
	gui=$with_gui)
	
AC_ARG_ENABLE(debug,
	[  --enable-debug          debug @<:@default=disabled@:>@],
	debug=$enable_debug)
if test "$debug" = "yes" ; then
	DEB_CFLAGS="-DDEBUG -DKIK_DEBUG"
fi
AC_SUBST(DEB_CFLAGS)
AC_SUBST(OPT_CFLAGS)

ML_PTY_OBJ="ml_pty.o"
ml_cv_is_posix="yes"

case "${host_os}" in
mingw32*)
	if test ! -f "/lib/libmsys-1.0.dll.a" ; then
		ML_PTY_OBJ="ml_pty_pipewin32.o"
		WIN32DIALOG_OBJ="x_connect_dialog.o"
		ml_cv_is_posix="no"
	fi

	if test "$gui" != "win32" ; then
		echo "$gui is not supported in ${host_os}. Use win32."
		gui="win32"
	fi
	;;
cygwin*)
	case "${CC} ${CFLAGS}" in
	*mno-cygwin*)
		ML_PTY_OBJ="ml_pty_pipewin32.o"
		WIN32DIALOG_OBJ="x_connect_dialog.o"
		ml_cv_is_posix="no"
		;;
	*)
		;;
	esac

	# $have_x is defined in AC_PATH_XTRA.
	if test "$have_x" = "yes" ; then
		if test "$gui" = "" ; then
			gui="xlib"
		elif test "$gui" != "xlib" -a "$gui" != "win32" ; then
			echo "$gui is not supported in ${host_os}. Use win32."
			gui="win32"
		fi
	else
		if test "$gui" != "" -a "$gui" != "win32" ; then
			echo "$gui is not supported in ${host_os}. Use win32."
		fi
		gui="win32"
	fi
	;;
*)
	if test "$gui" != "xlib" ; then
		echo "$gui is not supported in ${host_os}. Use xlib."
		gui="xlib"
	fi
	;;
esac

if test "$ml_cv_is_posix" = "yes" -o "$debug" = "yes" ; then
	SUBSYSTEM="-Wl,--subsystem,console"
fi

AC_SUBST(ML_PTY_OBJ)
AC_SUBST(WIN32DIALOG_OBJ)
AC_SUBST(SUBSYSTEM)

if test "$gui" = "win32" ; then
	WIN32TAG="_win32"
	GUI_CFLAGS="-DUSE_WIN32GUI"
	X_CFLAGS=""
	AC_SUBST(WIN32TAG)
	AC_SUBST(GUI_CFLAGS)
else
	# gui = xlib

	# Li18nux Xlib-I18N defines X_HAVE_UTF8_STRING but has no Xutf8LookupString.
	ldflags="$LDFLAGS" cflags="$CFLAGS"
	LDFLAGS="$LDFLAGS $X_PRE_LIBS $X_LIBS $X_EXTRA_LIBS" CFLAGS="$X_CFLAGS $CFLAGS"
	AC_CHECK_LIB(X11,Xutf8LookupString,XUTF8_CFLAGS="-DHAVE_XUTF8_LOOKUP_STRING")
	LDFLAGS="$ldflags" CFLAGS="$cflags"
	AC_SUBST(XUTF8_CFLAGS)
fi

AC_ARG_ENABLE(fribidi,
	[  --enable-fribidi        bidi @<:@default=disabled@:>@],
	fribidi=$enable_fribidi, fribidi="no")
if test "$fribidi" = "yes" ; then
	PKG_CHECK_MODULES(FRIBIDI, fribidi, have_fribidi=yes, have_fribidi=no)
	if test "$have_fribidi" = "yes" ; then
		FRIBIDI_CFLAGS="${FRIBIDI_CFLAGS} -DUSE_FRIBIDI"
	else
		AC_CHECK_PROG(fribidi_config,fribidi-config,yes,fribidi="no")
		if test "$fribidi_config" = "yes" ; then
			FRIBIDI_CFLAGS="`fribidi-config --cflags` -DUSE_FRIBIDI"
			FRIBIDI_LIBS="`fribidi-config --libs`"
		fi
	fi
fi
# "tr ..." is hack for cygwin. (removing \r which is output by win32 native pkg-config.)
FRIBIDI_CFLAGS="`echo ${FRIBIDI_CFLAGS} | tr -d '\r'`"
FRIBIDI_LIBS="`echo ${FRIBIDI_LIBS} | tr -d '\r'`"
AC_SUBST(FRIBIDI_CFLAGS)
AC_SUBST(FRIBIDI_LIBS)

# default value
IMAGELIB_NAME=none
AC_ARG_WITH(imagelib,
	[  --with-imagelib@<:@=ARG@:>@   image library (imlib|gdk-pixbuf) @<:@default=no@:>@],
	imagelib=$with_imagelib)
case ${imagelib} in
imlib)
	AC_CHECK_PROG(imlib_config,imlib-config,yes,no)
	if test "x$imlib_config" = "xno" ; then
		echo ""
		echo "** ERROR **"
		echo "Could not find imlib-config."
		echo ""
		exit 1
	fi
	IMAGELIB_CFLAGS="`imlib-config --cflags` -DUSE_IMLIB"
	IMAGELIB_LIBS="`imlib-config --libs`"
	IMAGELIB_NAME=imlib
	;;
imlib2)
	AC_CHECK_PROG(imlib2_config,imlib2-config,yes,no)
	if test "x$imlib2_config" = "xno" ; then
		echo ""
		echo "** ERROR **"
		echo "Could not find imlib2-config."
		echo ""
		exit 1
	fi
	IMAGELIB_CFLAGS="`imlib2-config --cflags` -DUSE_IMLIB"
	IMAGELIB_LIBS="`imlib2-config --libs`"
	IMAGELIB_NAME=imlib2
	;;
gdk-pixbuf)
	PKG_CHECK_MODULES(IMAGELIB, gdk-pixbuf-2.0, [
		IMAGELIB_NAME=gdk
		IMAGELIB_CFLAGS="${IMAGELIB_CFLAGS} -DUSE_GDK_PIXBUF"
	])
	if test "x$IMAGELIB_NAME" = "xnone" ; then
		AC_CHECK_PROG(pkg_config,gdk-pixbuf-config,yes)
		if test "$pkg_config" = "yes" ; then
			IMAGELIB_CFLAGS="`gdk-pixbuf-config --cflags` -DUSE_GDK_PIXBUF"
			IMAGELIB_LIBS="`gdk-pixbuf-config --libs`"
			IMAGELIB_NAME=gdk
		fi
	fi
	;;
gdk-pixbuf2)
	PKG_CHECK_MODULES(IMAGELIB, gdk-pixbuf-2.0, [
		IMAGELIB_NAME=gdk
		IMAGELIB_CFLAGS="${IMAGELIB_CFLAGS} -DUSE_GDK_PIXBUF"
	])
	;;
gdk-pixbuf1)
	AC_CHECK_PROG(gdk_pixbuf_config,gdk-pixbuf-config,yes)
	if test "$gdk_pixbuf_config" = "yes" ; then
		IMAGELIB_CFLAGS="`gdk-pixbuf-config --cflags` -DUSE_GDK_PIXBUF"
		IMAGELIB_LIBS="`gdk-pixbuf-config --libs`"
		IMAGELIB_NAME=gdk
	fi
	;;
*)
	if test "${imagelib}" != "" ; then
		echo ""
		echo "** ERROR **"
		echo "${imagelib} library is NOT supported."
		echo ""
		exit 1
	fi
	;;
esac
AC_SUBST(IMAGELIB_CFLAGS)
AC_SUBST(IMAGELIB_LIBS)
AC_SUBST(IMAGELIB_NAME)

# --enable-anti-alias is backward compatibility for --with-type-engines
AC_ARG_ENABLE(anti_alias,
	[  --enable-anti-alias     (obsoleted by --with-type-engines)anti alias @<:@default=disabled@:>@],
	anti_alias=$enable_anti_alias)
	
AC_ARG_WITH(type_engines,
	[  --with-type-engines     type engines(xcore, xft) @<:@default=xcore@:>@],
	type_engines=$with_type_engines)

if test "$gui" = "win32" ; then
	type_engines="xcore"
elif  test "$anti_alias" = "yes" ; then
	type_engines="xcore xft"
fi

if test "$type_engines" = ""; then
	type_engines="xcore"
fi

type_engines=`echo ${type_engines} | sed 's/,/ /g'`
for type_engine in ${type_engines} ; do
case ${type_engine} in
xft)
	PKG_CHECK_MODULES(XFT, xft, have_xft=yes, have_xft=no)
	if test "$have_xft" = "yes" ; then
		AA_CFLAGS="-DUSE_TYPE_XFT  ${XFT_CFLAGS} ${AA_CFLAGS}"
		AA_LIBS="${XFT_LIBS} ${AA_LIBS}"
	else
		AC_CHECK_PROG(xft_config,xft-config,yes)
		if test "$xft_config" = "yes" ; then
			AA_CFLAGS="`xft-config --cflags` -DUSE_TYPE_XFT ${AA_CFLAGS}"
			AA_LIBS="`xft-config --libs` ${AA_LIBS}"
		else
			AA_CFLAGS="-DUSE_TYPE_XFT ${AA_CFLAGS}"
			AA_LIBS="-lXft ${AA_LIBS}"
		fi
	fi
	type_engines_result="$type_engines_result xft"
	;;
xcore)
	AA_CFLAGS="-DUSE_TYPE_XCORE ${AA_CFLAGS}"
	type_engines_result="$type_engines_result xcore"
	;;
*)
	echo "${type_engine} is unknown type engine."
	;;
esac
done

if test "${AA_CFLAGS}" = "" ; then
	echo "no type engine specified."
	exit 1
fi

AC_SUBST(AA_CFLAGS)
AC_SUBST(AA_LIBS)
AC_SUBST(PROG)

AC_ARG_ENABLE(ind,
	[  --enable-ind            libind @<:@default=disabled@:>@],
	ind=$enable_ind, ind="no")
if test "$ind" = "yes" ; then
	AC_CHECK_LIB(ind,indian_init,
		[
		IND_CFLAGS="-DUSE_IND"
		IND_LIBS="-lind"
		], ind="no")
fi
AC_SUBST(IND_LIBS)
AC_SUBST(IND_CFLAGS)

AC_ARG_ENABLE(utmp,
	[  --enable-utmp           utmp (Experimental) @<:@default=disabled@:>@],
	utmp=$enable_utmp, utmp="no")
if test "$utmp" = "yes" ; then
	UTMP_CFLAGS="-DUSE_UTMP"
	has_utmp=`grep utmp /etc/group 2>/dev/null`
	if test "$has_utmp" ; then
		INSTALL_OPT="-m 2755 -g utmp"
	else
		INSTALL_OPT="-m 4755 -o root"
	fi
else
	INSTALL_OPT="-m 755"
fi
AC_SUBST(UTMP_CFLAGS)
AC_SUBST(INSTALL_OPT)

PKG_CHECK_MODULES(GTK, gtk+-2.0, have_gtk2=yes, have_gtk2=no)
if test "$have_gtk2" = "yes" ; then
	gtk_config=yes
else
	AC_CHECK_PROG(gtk_config,gtk-config,yes)
	if test "$gtk_config" = "yes" ; then
		GTK_CFLAGS="`gtk-config --cflags`"
		GTK_LIBS="`gtk-config --libs`"
	else
		# for Daichi Goto-san:)
		AC_CHECK_PROG(gtk_config,gtk12-config,yes)
		if test "$gtk_config" = "yes" ; then
			GTK_CFLAGS="`gtk12-config --cflags`"
			GTK_LIBS="`gtk12-config --libs`"
		fi
	fi
fi
# "tr ..." is hack for cygwin. (removing \r which is output by win32 native pkg-config.)
GTK_CFLAGS="`echo $GTK_CFLAGS | tr -d '\r'`"
GTK_LIBS="`echo $GTK_LIBS | tr -d '\r'`"
AC_SUBST(GTK_CFLAGS)
AC_SUBST(GTK_LIBS)

tools="mlclient,mlconfig,mlcc,mlterm-menu"
AC_ARG_WITH(tools,
	[  --with-tools@<:@=ARG@:>@      external tools @<:@default=mlclient,mlconfig,mlterm-menu,mlcc@:>@],
	[
	# If given --without-tools or --with-tools with no args.
	if test "${with_tools}" = "no" ; then
		tools=""
	elif test "${with_tools}" != "yes" ; then
		tools=${with_tools}
	fi
	])
tools=`echo ${tools} | sed 's/,/ /g'`
for tool in ${tools} ; do
case ${tool} in
mlconfig)
	if test "${gtk_config}" = "yes" ; then
		MAKE_DIRS="tool/mlconfig ${MAKE_DIRS}"
		OUTPUT_FILES="tool/mlconfig/Makefile tool/mlconfig/po/Makefile.in ${OUTPUT_FILES}"
		tools_result="$tools_result $tool"
	else
		echo ""
		echo "** WARNING  **"
		echo " couldn't compile mlconfig since headers/libraries of GTK are missing."
		echo ""
	fi
	;;
mlterm-menu)
	if test "${gtk_config}" = "yes" ; then
		MAKE_DIRS="contrib/tool/mlterm-menu ${MAKE_DIRS}"
		OUTPUT_FILES="contrib/tool/mlterm-menu/Makefile ${OUTPUT_FILES}"
		tools_result="$tools_result $tool"
	else
		echo ""
		echo "** WARNING  **"
		echo " couldn't compile mlterm-menu since headers/libraries of GTK are missing."
		echo ""
	fi
	;;
mlclient)
	MAKE_DIRS="tool/mlclient ${MAKE_DIRS}"
	OUTPUT_FILES="tool/mlclient/Makefile ${OUTPUT_FILES}"
	tools_result="$tools_result $tool"
	;;
w3mmlconfig)
	AC_PATH_PROG(PERL,perl,$PATH)
	if test "x$PERL" = x ; then
		echo ""
		echo "** WARNING  **"
		echo " couldn't find perl in PATH environment variable."
		echo ""
		PERL="/usr/local/bin/perl" # should be "/usr/bin/env perl"?
	fi
	MAKE_DIRS="tool/w3mmlconfig ${MAKE_DIRS}"
	OUTPUT_FILES="tool/w3mmlconfig/Makefile tool/w3mmlconfig/mlconfig.cgi tool/w3mmlconfig/w3mmlconfig ${OUTPUT_FILES}"
	tools_result="$tools_result $tool"
	;;
mlcc)
	if test "$ml_cv_is_posix" = "yes" ; then
		MAKE_DIRS="contrib/tool/mlcc ${MAKE_DIRS}"
		OUTPUT_FILES="contrib/tool/mlcc/Makefile ${OUTPUT_FILES}"
		SUBDIRS="${SUBDIRS} contrib/tool/mlcc"
		tools_result="$tools_result $tool"
	fi
	;;
mlmenu)
	if test "$gui" = "xlib" ; then
		MAKE_DIRS="tool/mlmenu ${MAKE_DIRS}"
		OUTPUT_FILES="tool/mlmenu/Makefile ${OUTPUT_FILES}"
		tools_result="$tools_result $tool"
	fi
	;;
mlterm-zoom)
	if test "$gui" = "xlib" ; then
		MAKE_DIRS="contrib/tool/mlterm-zoom ${MAKE_DIRS}"
		OUTPUT_FILES="contrib/tool/mlterm-zoom/Makefile ${OUTPUT_FILES}"
		tools_result="$tools_result $tool"
	fi
	;;
*)
	echo "${tool} is unknown tool."
	;;
esac
done

#
# --- libltdl ---
#
AC_ARG_WITH(libltdl,
	[  --with-libltdl@<:@=PREFIX@:>@ load modules with libltdl @<:@default=without@:>@],,
	[with_libltdl=no])
# We don't check here wheter libltdl is installed. (See kiklib/src/configure.in)

#
# --- Xdnd ---
#
AC_ARG_ENABLE(dnd,
	[  --disable-dnd           dnd @<:@default=enabled@:>@],
	dnd=$enable_dnd, dnd="yes")
if test "$dnd" = "no" ; then
	DND_CFLAGS="-DDISABLE_XDND"
else
	DND_LIBS="x_dnd${WIN32TAG}.o"
fi
AC_SUBST(DND_CFLAGS)
AC_SUBST(DND_LIBS)


IM_CFLAGS=""
DL_LIBS_IM=""

#
# --- kbd ---
#
AC_ARG_ENABLE(kbd,
	[  --disable-kbd           input method for Arabic and Indic @<:@default=enabled@:>@],
	kbd=$enable_kbd)
if test "$kbd" != "no" -a "$gui" = "xlib" ; then
	IM_CFLAGS="$IM_CFLAGS -DUSE_KBD"
	MAKE_DIRS="inputmethod/kbd ${MAKE_DIRS}"
	OUTPUT_FILES="inputmethod/kbd/Makefile ${OUTPUT_FILES}"
	if test "x$with_libltdl" != "xno" ; then
		DL_LIBS_IM="-dlopen \$(top_builddir)/inputmethod/kbd/libim-kbd.la ${DL_LIBS_IM}"
	fi
	input_methods_result="$input_methods_result kbd"
fi

#
# --- uim ---
#
AC_ARG_ENABLE(uim,
	[  --enable-uim            uim (Experimental) @<:@default=disabled@:>@],
	uim=$enable_uim)
if test "x$uim" = xyes ; then
	PKG_CHECK_MODULES(UIM, uim >= 1.0.0, have_uim=yes, have_uim=no)
	if test "x$have_uim" = xyes ; then
		IM_CFLAGS="$IM_CFLAGS -DUSE_UIM"
		MAKE_DIRS="inputmethod/uim ${MAKE_DIRS}"
		OUTPUT_FILES="inputmethod/uim/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != xno ; then
			DL_LIBS_IM="-dlopen \$(top_builddir)/inputmethod/uim/libim-uim.la ${DL_LIBS_IM}"
		fi
		input_methods_result="$input_methods_result uim"
	fi
fi
AC_SUBST(UIM_CFLAGS)
AC_SUBST(UIM_LIBS)

#
# --- IIIMF ---
#
AC_ARG_ENABLE(iiimf,
	[  --enable-iiimf          IIIMF (Experimental) @<:@default=disabled@:>@],
	iiimf=$enable_iiimf)
if test "x$iiimf" = xyes ; then
	PKG_CHECK_MODULES(IIIMCF, iiimf-lib-client, have_iiimcf=yes, have_iiimcf=no)
	if test "x$have_iiimcf" = xyes ; then
		found_iiimcf=yes
	elif test -d /usr/lib/im/lib ; then
		LIBS="$LIBS -L/usr/lib/im/lib"
		AC_CHECK_LIB(iiimcf, iiimcf_create_context,
			[
			found_iiimcf=yes
			IIIMCF_CFLAGS="-I/usr/lib/im/include"
			IIIMCF_LIBS="-L/usr/lib/im/lib -liiimcf"
			])
	else
		AC_CHECK_LIB(iiimcf, iiimcf_create_context,
			[
			found_iiimcf=yes
			IIIMCF_CFLAGS=""
			IIIMCF_LIBS="-liiimcf"
			])
	fi

	if test "x$found_iiimcf" = xyes ; then
		IM_CFLAGS="$IM_CFLAGS -DUSE_IIIMF"
		MAKE_DIRS="inputmethod/iiimf ${MAKE_DIRS}"
		OUTPUT_FILES="inputmethod/iiimf/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != xno ; then
			echo ""
			echo "** ERROR **"
			echo " IIIMF plugin couldn't be used with libltdl."
			echo ""
			exit 1
		else
			input_methods_result="$input_methods_result IIIMF"
		fi
		AC_CHECK_LIB(iiimcf, iiimcf_create_hotkey_notify_event,
			[
			IIIMCF_CFLAGS="$IIIMCF_CFLAGS -DHAVE_HOTKEY_NOTFY_EVENT"
			])
		AC_CHECK_LIB(iiimcf, iiimcf_create_aux_getvalues_event,
			[
			IIIMCF_CFLAGS="$IIIMCF_CFLAGS -DHAVE_AUX_GETVALUES_EVENT"
			])
	fi
fi
AC_SUBST(IIIMCF_CFLAGS)
AC_SUBST(IIIMCF_LIBS)

#
# --- m17n library ---
#
AC_ARG_ENABLE(m17nlib,
	[  --enable-m17nlib        m17n library (Experimental) @<:@default=disabled@:>@],
	m17nlib=$enable_m17nlib)
if test "x$m17nlib" = xyes ; then
	AC_CHECK_PROG(m17nlib, m17n-config, yes, no)
fi
if test "x$m17nlib" = xyes ; then
	m17n_saved_libs="$LIBS"
	M17NLIB_LIBS=`m17n-config --libs`
	LIBS="$LIBS $M17NLIB_LIBS"
	AC_CHECK_LIB(m17n, minput_open_im,
		[
		IM_CFLAGS="$IM_CFLAGS -DUSE_M17NLIB"
		M17NLIB_CFLAGS="`m17n-config --cflags`"
		MAKE_DIRS="inputmethod/m17nlib ${MAKE_DIRS}"
		OUTPUT_FILES="inputmethod/m17nlib/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != xno ; then
			DL_LIBS_IM="-dlopen \$(top_builddir)/inputmethod/m17nlib/libim-m17nlib.la ${DL_LIBS_IM}"
		fi
		input_methods_result="$input_methods_result m17nlib"
		])
	LIBS="$m17n_saved_libs"
fi
AC_SUBST(M17NLIB_CFLAGS)
AC_SUBST(M17NLIB_LIBS)

#
# --- SCIM ---
#
AC_ARG_ENABLE(scim,
	[  --enable-scim           SCIM (Experimental) @<:@default=disabled@:>@],
	scim=$enable_scim)
if test "x$scim" = "xyes" ; then
	AC_PROG_CXX
	PKG_CHECK_MODULES(SCIM, scim >= 1.4.0, has_scim=yes , has_scim=no)
	if test "x$has_scim" = xyes ; then
		IM_CFLAGS="$IM_CFLAGS -DUSE_SCIM"
		MAKE_DIRS="inputmethod/scim ${MAKE_DIRS}"
		OUTPUT_FILES="inputmethod/scim/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != xno ; then
			DL_LIBS_IM="-dlopen \$(top_builddir)/inputmethod/scim/libim-scim.la ${DL_LIBS_IM}"
		fi
		input_methods_result="$input_methods_result SCIM"
	fi
fi
AC_SUBST(SCIM_CFLAGS)
AC_SUBST(SCIM_LIBS)


if test "x$IM_CFLAGS" != x ; then
	IM_CFLAGS="$IM_CFLAGS -DUSE_IM_PLUGIN"
fi
AC_SUBST(IM_CFLAGS)
AC_SUBST(DL_LIBS_IM)


#
# --- scrollbar plugins ---
#

DL_LIBS_SB=""
SB_CFLAGS=""

scrollbars="sample,extra"  # default

AC_ARG_WITH(scrollbars,
	[  --with-scrollbars@<:@=ARG@:>@ scrollbar plugins (sample, extra, pixmap_engine) @<:@default=sample,extra@:>@],
	[
	# If given --without-scrollbars or --with-scrollbars with no args.
	if test "${with_scrollbars}" = "no" ; then
		scrollbars=""
	elif test "${with_scrollbars}" != "yes" ; then
		scrollbars=${with_scrollbars}
	fi
	])
scrollbars=`echo ${scrollbars} | sed 's/,/ /g'`
for scrollbar in ${scrollbars} ; do
case ${scrollbar} in
sample)
	MAKE_DIRS="scrollbar/sample ${MAKE_DIRS}"
	OUTPUT_FILES="scrollbar/sample/Makefile ${OUTPUT_FILES}"
	if test "x$with_libltdl" != "xno" ; then
		for name in sample sample2 ; do
			DL_LIBS_SB="-dlopen \$(top_builddir)/scrollbar/sample/lib$name.la ${DL_LIBS_SB}"
		done
	fi
	scrollbars_result="$scrollbars_result $scrollbar"
	;;
extra)
	if test "$gui" = "xlib" ; then
		MAKE_DIRS="contrib/scrollbar/extra ${MAKE_DIRS}"
		OUTPUT_FILES="contrib/scrollbar/extra/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != "xno" ; then
			for name in athena next motif mozmodern ; do
				DL_LIBS_SB="-dlopen \$(top_builddir)/contrib/scrollbar/extra/lib$name.la ${DL_LIBS_SB}"
			done
		fi
		scrollbars_result="$scrollbars_result $scrollbar"
	fi
	;;
pixmap_engine)
	if test "x$allow_undefined_flag" = "xunsupported" ; then
		echo ""
		echo "** ERROR **"
		echo "pixmap_engine has undefined symbols. $host_os does NOT allow it. Please retry"
		echo "to run configure scripts without pixmap_engine"
		echo "(e.g.  --with-scrllbars=sample,extra)"
		echo ""
		exit 1
	fi
	if test "$IMAGELIB_NAME" = "gdk" -o "$IMAGELIB_NAME" = "imlib" -o "$IMAGELIB_NAME" = "imlib2" ; then
		MAKE_DIRS="contrib/scrollbar/pixmap_engine ${MAKE_DIRS}"
		OUTPUT_FILES="contrib/scrollbar/pixmap_engine/Makefile contrib/scrollbar/pixmap_engine/sample3/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != "xno" ; then
			DL_LIBS_SB="-export-dynamic -dlopen \$(top_builddir)/contrib/scrollbar/pixmap_engine/libpixmap_engine.la ${DL_LIBS_SB}"
		else
			DL_LIBS_SB="-export-dynamic"
		fi
		SB_CFLAGS="-DSUPPORT_PIXMAP_ENGINE"
		scrollbars_result="$scrollbars_result $scrollbar"
	else
		echo ""
		echo "** ERROR **"
		echo "To use pixmap_engine for scrollbar, you need any image library such as"
		echo "gdk-pixbuf and imlib. (e.g. --with-imagelib=imlib)"
		echo ""
		exit 1
	fi
	;;
*)
	echo "${scrollbar} is unknown scrollbar."
	;;
esac
done # for scrollbar in ${scrollbars}

AC_SUBST(SB_CFLAGS)
AC_SUBST(DL_LIBS_SB)

AC_ARG_ENABLE(optimize_redrawing,
	[  --enable-optimize-redrawing optimize redrawing a line @<:@default=disabled@:>@],
	optimize_redrawing=$enable_optimize_redrawing,
	optimize_redrawing="no")
if test "$optimize_redrawing" = "yes" ; then
	CFLAGS="$CFLAGS -DOPTIMIZE_REDRAWING"
fi

# gettext
AM_INIT_AUTOMAKE(mlterm,3.0.1)
AM_GNU_GETTEXT([external])

# XXX(maybe not portable)
if test "${USE_NLS}" = "yes" ; then
	AC_CHECK_LIB(intl,gettext,
		[
		INTL_LIBS="-lintl"
		])
fi
AC_SUBST(INTL_LIBS)


#
# --- module related stuffs ---
#

# undefined symbol
AC_MSG_CHECKING([for undefined symbol])
if test "x$allow_undefined_flag" = "xunsupported" ; then
	AC_MSG_RESULT([not supported])
	NO_UNDEFINED_FLAG="-no-undefined"
	KIK_LIBS_FOR_MODULE='-L${top_builddir}/kiklib/src -lkik'
	MKF_LIBS_FOR_MODULE='-L${top_builddir}/mkf/lib -lmkf'
else
	AC_MSG_RESULT(supported)
	NO_UNDEFINED_FLAG=""
	KIK_LIBS_FOR_MODULE=""
	MKF_LIBS_FOR_MODULE=""
fi
AC_SUBST(NO_UNDEFINED_FLAG)
AC_SUBST(KIK_LIBS_FOR_MODULE)
AC_SUBST(MKF_LIBS_FOR_MODULE)

if test "x$with_libltdl" != "xno" ; then
	DL_SELF="-dlopen self"
fi
AC_SUBST(DL_SELF)


CHANGE_DATE=`head -n 1 ${top_srcdir-$srcdir}/ChangeLog | sed -n 's/^\(@<:@0-9@:>@*-@<:@0-9@:>@*-@<:@0-9@:>@*\).*$/\1/p'`
AC_SUBST(CHANGE_DATE)
OUTPUT_FILES="xwindow/version.h ${OUTPUT_FILES}"

if test "${SUBDIRS}" != "" ; then
	ac_configure_args="${ac_configure_args} --without-funcs-mlterm-unuse"
	AC_CONFIG_SUBDIRS(${SUBDIRS})
fi

MAKE_DIRS="mlterm xwindow man etc ${MAKE_DIRS}"
OUTPUT_FILES="Makefile mlterm/Makefile xwindow/Makefile man/Makefile etc/Makefile ${OUTPUT_FILES}"

AC_SUBST(MAKE_DIRS)

if test "${MAKE_DIRS}" != "" ; then
	mkdir -p ${MAKE_DIRS}
fi

AC_OUTPUT(${OUTPUT_FILES})

# configuration result

echo
echo
echo "Mlterm was configured as follows"
echo
echo "Installation path prefix          : $prefix"
dnl echo "Character mapping table           : "
dnl echo "Character property table          : "
echo "Build shared libraries            : $enable_shared"
echo "Build static libraries            : $enable_static"
echo "Optimization for redrawing a line : $optimize_redrawing"
echo "BiDi rendering (Fribidi)          : $fribidi"
echo "ISCII rendering (libind)          : $ind"
echo "External tools                    :$tools_result"
echo "Image library                     : $with_imagelib"
echo "utmp support                      : $utmp"
echo "Type engines                      :$type_engines_result"
echo "DnD                               : $dnd"
echo "Input Methods                     : XIM$input_methods_result"
echo "Scrollbars                        : simple${scrollbars_result}"
echo

