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'
elif test -d ${top_srcdir-$srcdir}/../mkf -a -d ${top_srcdir-$srcdir}/../kiklib ; then
	# --with-gui=xlib,fb => cd fb ; --with-gui=fb
	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_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)

AC_ARG_WITH(gui,
	[  --with-gui@<:@=ARG@:>@        gui library(xlib|win32)],
	gui=$with_gui)
if test "$gui" = "fb,xlib" -o "$gui" = "xlib,fb" ; then
	gui=xlib
	SUBDIRS="${SUBDIRS} fb"
	MAKE_DIRS="${MAKE_DIRS} fb"

	# clone source tree to build both mlterm-xlib and mlterm-fb.
	if test -d ${top_srcdir-$srcdir}/fb ; then
		rm -rf ${top_srcdir-$srcdir}/fb
	fi
	mkdir -p ${top_srcdir-$srcdir}/fb
	for file in ChangeLog Makefile.in common configure etc gtk inputmethod java \
		main man mlterm script scrollbar xwindow doc ; do
		cp -R ${top_srcdir-$srcdir}/$file ${top_srcdir-$srcdir}/fb/
	done
fi

if test "$gui" = "fb" ; then
	MAKE_DIRS="doc/kbd ${MAKE_DIRS}"
	OUTPUT_FILES="doc/kbd/Makefile ${OUTPUT_FILES}"
elif test "$gui" = "quartz" ; then
	(cd xwindow/quartz/ && \
	 for file in `ls *.c` ; do if test -f ../$file ; then mv ../$file ../xlib/ ; fi ; done)
fi

ml_cv_is_posix="yes"

case "${host_os}" in
mingw32*)
	CYGPATHW=echo
	WIN32TAG=_win32
	if test ! -f "/lib/libmsys-1.0.dll.a" ; then
		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*)
	AC_CHECK_PROG(CYGPATHW,cygpath,cygpath -w,echo)
	WIN32TAG=_win32
	case "${CC} ${CFLAGS}" in
	*mno-cygwin* | *-mingw*)
		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" -a "$gui" != "console" ; then
			echo "$gui is not supported in ${host_os}. Use win32."
			gui="win32"
		fi
	else
		if test "$gui" != "" -a "$gui" != "win32" -a "$gui" != "console" ; then
			echo "$gui is not supported in ${host_os}. Use win32."
			gui="win32"
		fi
	fi
	;;
*)
	CYGPATHW=echo
	if test "$gui" != "xlib" -a "$gui" != "fb" -a "$gui" != "console" -a "$gui" != "quartz" ; then
		if test "$gui" != "" ; then
			echo "$gui is not supported in ${host_os}. xlib is used instead."
		fi
		gui="xlib"
	fi
	# $have_x is defined in AC_PATH_XTRA.
	if test "$gui" = "xlib" -a "$have_x" != "yes" ; then
		echo ""
		echo "** ERROR **"
		echo "Could not find xlib."
		echo ""
		exit 1
	fi
	;;
esac

# for java/Makefile.in
AC_SUBST(WIN32TAG)
AC_SUBST(CYGPATHW)

if test "$gui" = "quartz"; then
	ML_NORM_OBJ="ml_normalize.o"
	ML_NORM_CFLAGS="-DUSE_NORMALIZE"
fi
AC_SUBST(ML_NORM_OBJ)
AC_SUBST(ML_NORM_CFLAGS)

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

if test "$with_gnu_ld" = "yes" ; then
	DEXPORT="-Wl,--version-script=\$(VPATH)/dexport.map"
elif test "`$LD --version 2>&1 | $GREP Solaris`" != "" ; then
	# This option is for SunOS 5.x (Solaris) or later.
	DEXPORT="-Wl,-M -Wl,\$(VPATH)/dexport-sun.map -Wl,-z -Wl,nodefs"
fi
AC_SUBST(DEXPORT)

AC_ARG_WITH(gtk,
	[  --with-gtk@<:@=ARG@:>@        gtk+ version (2.0|3.0)],
	gtk_version=$with_gtk)
if test "$gtk_version" = "" ; then
	gtk_version=3.0
fi
PKG_CHECK_MODULES(GTK, gtk+-$gtk_version, have_gtk=yes, have_gtk=no)
if test "$have_gtk" != "yes"; then
	if test "$gtk_version" = "3.0"; then
		gtk_version=2.0
	else
		gtk_version=3.0
	fi
	PKG_CHECK_MODULES(GTK, gtk+-$gtk_version, have_gtk=yes, have_gtk=no)
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)

PKG_CHECK_MODULES(GDK_PIXBUF, gdk-pixbuf-2.0, have_gdk_pixbuf2=yes, have_gdk_pixbuf2=no)
# try to link libgio-2.0 explicitly. (SF topic #6234829)
PKG_CHECK_EXISTS(gdk-pixbuf-2.0 >= 2.14, [ PKG_CHECK_MODULES(GIO, gio-2.0) ])
GDK_PIXBUF_CFLAGS="`echo $GDK_PIXBUF_CFLAGS $GIO_CFLAGS | tr -d '\r'`"
GDK_PIXBUF_LIBS="`echo $GDK_PIXBUF_LIBS $GIO_LIBS | tr -d '\r'`"

AC_CHECK_PROG(have_gdk_pixbuf1,gdk-pixbuf-config,yes)

if test "$gui" = "xlib" ; then
	AC_ARG_WITH(imagelib,
		[  --with-imagelib@<:@=ARG@:>@   image library (gdk-pixbuf) @<:@default=no@:>@],
		imagelib=$with_imagelib)
else
	# Don't link an external image library on win32gdi or framebuffer.
	with_imagelib=""
fi

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
	echo ""
	echo "** WARNING **"
	echo " imlib is no longer supported."
	echo ""
	IMAGELIB_CFLAGS="`imlib-config --cflags` -DBUILTIN_IMAGELIB"
	IMAGELIB_LIBS="`imlib-config --libs`"
	;;
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
	echo ""
	echo "** WARNING **"
	echo " imlib2 is no longer supported."
	echo ""
	IMAGELIB_CFLAGS="`imlib2-config --cflags` -DBUILTIN_IMAGELIB"
	IMAGELIB_LIBS="`imlib2-config --libs`"
	;;
gdk-pixbuf)
	if test "$have_gdk_pixbuf2" = "yes" ; then
		gdk_pixbuf_version=2
	elif test "$have_gdk_pixbuf1" = "yes" ; then
		gdk_pixbuf_version=1
	else
		echo ""
		echo "** ERROR **"
		echo "Could not find gdk-pixbuf"
		echo ""
		exit 1
	fi
	;;
gdk-pixbuf2)
	if test "$have_gdk_pixbuf2" = "yes" ; then
		gdk_pixbuf_version=2
	else
		echo ""
		echo "** ERROR **"
		echo "Could not find gdk-pixbuf2"
		echo ""
		exit 1
	fi
	;;
gdk-pixbuf1)
	if test "$have_gdk_pixbuf1" = "yes" ; then
		gdk_pixbuf_version=1
	else
		echo ""
		echo "** ERROR **"
		echo "Could not find gdk-pixbuf1"
		echo ""
		exit 1
	fi
	;;
*)
	if test "${imagelib}" != "" ; then
		echo ""
		echo "** ERROR **"
		echo "${imagelib} library is NOT supported."
		echo ""
		exit 1
	fi
	;;
esac

if test "$gdk_pixbuf_version" != "" ; then
	if test "$gdk_pixbuf_version" = "1" ; then
		# Override GDK_PIXBUF_XXX variables.
		GDK_PIXBUF_CFLAGS="`gdk-pixbuf-config --cflags|tr -d '\r'`"
		GDK_PIXBUF_LIBS="`gdk-pixbuf-config --libs|tr -d '\r'`"
		echo ""
		echo "** WARNING **"
		echo " gdk-pixbuf1 is no longer supported."
		echo ""
	fi

	IMAGELIB_CFLAGS="${GDK_PIXBUF_CFLAGS} -DBUILTIN_IMAGELIB"
	IMAGELIB_LIBS="${GDK_PIXBUF_LIBS}"

elif test "$have_gdk_pixbuf2" != "yes" -a "$have_gdk_pixbuf1" = "yes" ; then
	GDK_PIXBUF_CFLAGS="`gdk-pixbuf-config --cflags|tr -d '\r'`"
	GDK_PIXBUF_LIBS="`gdk-pixbuf-config --libs|tr -d '\r'`"
fi

# Used by mlimgloader.
AC_SUBST(GDK_PIXBUF_CFLAGS)
AC_SUBST(GDK_PIXBUF_LIBS)

AC_SUBST(IMAGELIB_CFLAGS)
AC_SUBST(IMAGELIB_LIBS)

# XXX For pow in libm.so which is used in x_imagelib.c
# /lib/* ... e.g.) /lib/i386-linux-gnu in ubuntu.
for ml_cv_lib_path in /lib /usr/lib /lib/* `echo $LIBRARY_PATH | tr ":" " "` ; do
	libm_files=`ls $ml_cv_lib_path/libm\\.so* $ml_cv_lib_path/libm\\.sl* 2> /dev/null`
	for libm_file in $libm_files ; do
		if test -z "$ml_cv_libm_dir" -a -n "`$NM -D $libm_file | $GREP pow`" ; then
			ml_cv_libm_dir=`dirname $libm_file`
		fi
	done
done
if test -z "$IMAGELIB_LIBS" ; then
	if test -n "$ml_cv_libm_dir" ; then
		MATH_CFLAGS="-DDLOPEN_LIBM -DLIBMDIR=\\\"$ml_cv_libm_dir\\\""
	else
		AC_CHECK_LIB(m, pow, MATH_LIBS=-lm)
	fi
else
	AC_CHECK_LIB(m, pow, MATH_LIBS=-lm)
fi
AC_SUBST(MATH_LIBS)
AC_SUBST(MATH_CFLAGS)


AC_ARG_ENABLE(image,
	[  --disable-image         show image @<:@default=enabled@:>@],
	image=$enable_image, image="yes")
if test "$image" != "yes" ; then
	# If sixel.h is not found on gui=console, set IMAGE_CFLAGS="-DNO_IMAGE" forcibly below.
	IMAGE_CFLAGS="-DNO_IMAGE"
fi
AC_SUBST(IMAGE_CFLAGS)


if test "$gui" = "win32" ; then
	GUI="win32"
	GUI_CFLAGS="-DUSE_WIN32GUI"
	X_CFLAGS=""
elif test "$gui" = "xlib" ; then
	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)
elif test "$gui" = "quartz" ; then
	GUI="quartz"
	GUI_CFLAGS="-DUSE_QUARTZ"
	X_CFLAGS=""
elif test "$gui" = "console" ; then
	GUI="console"
	GUI_CFLAGS="-DUSE_CONSOLE"
	AC_CHECK_HEADER(sixel.h, SIXEL_CFLAGS="-DUSE_LIBSIXEL")
	if test "$SIXEL_CFLAGS" = "-DUSE_LIBSIXEL" ; then
		SIXEL_LIBS="-lsixel"
	else
		IMAGE_CFLAGS="-DNO_IMAGE"
		IMAGELIB_CFLAGS=""
		IMAGELIB_LIBS=""
	fi
	AC_SUBST(SIXEL_CFLAGS)
	AC_SUBST(SIXEL_LIBS)
else
	GUI="fb"
	# for NetBSD/x68k
	# <machine/vuid_event.h> is also checked not to enable USE_GRF on NetBSD/macppc.
	AC_CHECK_HEADER(machine/grfioctl.h,
		AC_CHECK_HEADER(machine/vuid_event.h, GUI_CFLAGS="-DUSE_GRF"))
	GUI_CFLAGS="${GUI_CFLAGS} -DUSE_FRAMEBUFFER"
fi
AC_SUBST(GUI)
AC_SUBST(GUI_CFLAGS)

AC_CHECK_HEADER(regex.h,REGEX_CFLAGS="-DHAVE_REGEX")
AC_SUBST(REGEX_CFLAGS)


AC_ARG_ENABLE(dl-ctl,
	[  --disable-dl-ctl        dynamic loading ctl library @<:@default=enable@:>@],
	dl_ctl=$enable_dl_ctl, dl_ctl="yes")
if test "$enable_shared" = "no" ; then
	dl_ctl="no"
fi

if test "$dl_ctl" = "yes" ; then
	LMLTERM_CORE=libmlterm_core.la
	CTL_LOADER_OBJ=ml_ctl_loader.o
else
	LMLTERM_CORE=libmlterm_core.a
	CTL_CFLAGS="-DNO_DYNAMIC_LOAD_CTL"
	CTL_LIBS_FOR_VTE="../mlterm/libctl/*.lo"
fi
AC_SUBST(LMLTERM_CORE)
AC_SUBST(CTL_LOADER_OBJ)

AC_ARG_ENABLE(fribidi,
	[  --disable-fribidi       bidi @<:@default=enabled@:>@],
	fribidi=$enable_fribidi, fribidi="yes")
if test "$fribidi" = "yes" ; then
	PKG_CHECK_MODULES(FRIBIDI, fribidi, have_fribidi=yes, have_fribidi=no)
	if test "$have_fribidi" != "yes" ; then
		AC_CHECK_PROG(fribidi_config,fribidi-config,yes,fribidi="no")
		if test "$fribidi_config" = "yes" ; then
			FRIBIDI_CFLAGS="`fribidi-config --cflags`"
			FRIBIDI_LIBS="`fribidi-config --libs`"
			have_fribidi="yes"
		fi
	fi
else
	have_fribidi="no"
fi
if test "$have_fribidi" = "yes" ; then
	# "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'`"
	if test "$dl_ctl" = "yes" ; then
		CTL_LIBS="libctl_bidi.la ${CTL_LIBS}"
	else
		CTL_CFLAGS="-DUSE_FRIBIDI ${CTL_CFLAGS}"
		CTL_LIBS="libctl_bidi.a ${CTL_LIBS}"
		CTL_LIBS_FOR_PROG="../mlterm/libctl/libctl_bidi.a ${FRIBIDI_LIBS} ${CTL_LIBS_FOR_PROG}"
		CTL_LIBS_FOR_VTE="${FRIBIDI_LIBS} ${CTL_LIBS_FOR_VTE}"
	fi
fi

AC_SUBST(FRIBIDI_CFLAGS)
AC_SUBST(FRIBIDI_LIBS)
AC_SUBST(CTL_CFLAGS)
AC_SUBST(CTL_LIBS_FOR_VTE)

AC_ARG_WITH(pthread,
	[  --without-pthread       don't use pthread @<:@default=with@:>@])


AC_ARG_ENABLE(dl-type,
	[  --disable-dl-type       dynamic loading type engine library @<:@default=enable@:>@],
	dl_type=$enable_dl_type, dl_type="yes")
if test "$enable_shared" = "no" ; then
	dl_type="no"
fi

if test "$dl_type" = "yes" ; then
	TYPE_LOADER_OBJ=x_type_loader.o
	if test "$gui" != "xlib" ; then
		TYPE_CFLAGS="-DNO_DYNAMIC_LOAD_TYPE"
	fi
else
	TYPE_CFLAGS="-DNO_DYNAMIC_LOAD_TYPE"
	TYPE_LIBS_FOR_VTE="../xwindow/libtype/*.lo"
fi
AC_SUBST(TYPE_LOADER_OBJ)

AC_ARG_ENABLE(anti_alias,
	[  --enable-anti-alias     same as --with-type-engines=xcore,xft,cairo on xlib @<:@default=enabled@:>@
                          enable truetype fonts on framebuffer @<:@default=disabled@:>@ ],
	anti_alias=$enable_anti_alias, anti_alias="auto")

if test "$anti_alias" = "yes" -a "$gui" = "fb" ; then
	PKG_CHECK_MODULES(FT, freetype2, have_ft=yes, have_ft=no)
	if test "$have_ft" = "no" ; then
		PKG_CHECK_MODULES(FT, freetype, have_ft=yes, have_ft=no)
	fi
	if test "$have_ft" = "yes" ; then
		# "tr ..." is hack for cygwin.
		# (removing \r which is output by win32 native pkg-config.)
		FT_CFLAGS="-DUSE_FREETYPE `echo $FT_CFLAGS | tr -d '\r'`"
		FT_LIBS="`echo $FT_LIBS | tr -d '\r'`"
		AC_SUBST(FT_CFLAGS)
		AC_SUBST(FT_LIBS)
	fi
fi

AC_ARG_WITH(type_engines,
	[  --with-type-engines@<:@=ARG@:>@ type engines (xcore, xft, cairo) @<:@default=xcore,xft,cairo@:>@],
	type_engines=$with_type_engines)

if test "$gui" != "xlib" ; then
	type_engines="xcore"
elif test "$type_engines" = "" ; then
	if test "$anti_alias" = "no" ; then
		type_engines="xcore"
	else
		type_engines="xcore,xft,cairo"
	fi
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
		AC_CHECK_PROG(xft_config,xft-config,yes)
		if test "$xft_config" = "yes" ; then
			XFT_CFLAGS="`xft-config --cflags`"
			XFT_LIBS="`xft-config --libs`"
			have_xft=yes
		else
			AC_CHECK_LIB(Xft,XftDrawCreate,
				[
				have_xft=yes
				# -lfontconfig is necessary because x_font.c refers FcXXX
				# functions directly.
				XFT_LIBS="-lXft -lfontconfig"
				])
		fi
	elif test "$gui" = "xlib" -a "$enable_shared" = "no" ; then
		# In case --disable-shared is specified on some platforms
		# (e.g. ubuntu 14.04) where dependency_libs="" in libXft.la.
		XFT_LIBS="$XFT_LIBS -lfontconfig -lfreetype"
	fi

	if test "$have_xft" = "yes" ; then
		# "tr ..." is hack for cygwin.
		# (removing \r which is output by win32 native pkg-config.)
	        XFT_CFLAGS="`echo $XFT_CFLAGS | tr -d '\r'`"
	        XFT_LIBS="`echo $XFT_LIBS | tr -d '\r'`"
		if test "$dl_type" = "yes" ; then
			TYPE_LIBS="libtype_xft.la ${TYPE_LIBS}"
		else
			NODL_OBJ="x_window_xft.o ${NODL_OBJ}"
			TYPE_LIBS_FOR_VTE="${XFT_LIBS} ${TYPE_LIBS_FOR_VTE}"
		fi
		TYPE_CFLAGS="-DUSE_TYPE_XFT ${TYPE_CFLAGS}"
		type_engines_result="$type_engines_result xft"
	fi
	;;
cairo)
	PKG_CHECK_MODULES(CAIRO, cairo, have_cairo=yes, have_cairo=no)
	if test "$have_cairo" != "yes" ; then
		AC_CHECK_LIB(cairo,cairo_create,
			[
			have_cairo=yes
			# -lfontconfig is necessary because x_font.c refers FcXXX
			# functions directly.
			CAIRO_LIBS="-lcairo -lfontconfig"
			])
	elif test "$gui" = "xlib" -a "$enable_shared" = "no" ; then
		# In case --disable-shared is specified on some platforms
		# (e.g. ubuntu 14.04) where dependency_libs="" in libcairo.la.
		CAIRO_LIBS="$CAIRO_LIBS -lfontconfig -lfreetype"
	fi

	if test "$have_cairo" = "yes" ; then
		# "tr ..." is hack for cygwin.
		# (removing \r which is output by win32 native pkg-config.)
	        CAIRO_CFLAGS="`echo $CAIRO_CFLAGS | tr -d '\r'`"
	        CAIRO_LIBS="`echo $CAIRO_LIBS | tr -d '\r'`"
		if test "$dl_type" = "yes" ; then
			TYPE_LIBS="libtype_cairo.la ${TYPE_LIBS}"
			if test "$with_pthread" != "no" ; then
				# NetBSD doesn't allow shared libraries to link
				# libpthread without linking it to executables.
				case "${host_os}" in
				netbsd*)
					PTHREAD_LIB="-lpthread"
					;;
				esac
			fi
		else
			NODL_OBJ="x_window_cairo.o ${NODL_OBJ}"
			TYPE_LIBS_FOR_VTE="${CAIRO_LIBS} ${TYPE_LIBS_FOR_VTE}"
		fi
		TYPE_CFLAGS="-DUSE_TYPE_CAIRO ${TYPE_CFLAGS}"
		type_engines_result="$type_engines_result cairo"
	fi
	;;
xcore)
	TYPE_CFLAGS="-DUSE_TYPE_XCORE ${TYPE_CFLAGS}"
	type_engines_result="$type_engines_result xcore"
	;;
*)
	echo "${type_engine} is unknown type engine."
	;;
esac
done

AC_ARG_ENABLE(otl,
	[  --disable-otl           Open Type layout with the use of harfbuzz or libotf @<:@default=enabled@:>@],
	otl=$enable_otl,
	otl="yes")
if test "$otl" != "yes" ; then
	echo "Open Type layout is disabled"
elif test "$gui" = "xlib" -a "$dl_type" = "no" -a "$type_engines_result" = " xcore"; then
	echo "libotf/libharfbuzz is not available."
elif test "$gui" = "fb" -a "$have_ft" != "yes" ; then
	echo "libotf/libharfbuzz is not available."
elif test "$gui" = "console" ; then
	echo "libotf/libharfbuzz is not available."
else
	PKG_CHECK_MODULES(HB, harfbuzz, have_hb=yes, have_hb=no)
	if test "$have_hb" = "yes" ; then
		OT_LAYOUT_CFLAGS="$HB_CFLAGS -DUSE_HARFBUZZ -DUSE_OT_LAYOUT"
		OT_LAYOUT_LIBS="$HB_LIBS"
		OT_LAYOUT_OBJ=hb.o
	else
		# XXX
		if test "$gui" = "quartz" ; then
			AC_CHECK_HEADER(harfbuzz/hb.h, have_hb=yes, have_hb=no)
			if test "$have_hb" = "yes"; then
				OT_LAYOUT_CFLAGS="-DHB_HACK_FOR_QUARTZ -DUSE_HARFBUZZ -DUSE_OT_LAYOUT"
				OT_LAYOUT_LIBS="-lharfbuzz"
				OT_LAYOUT_OBJ=hb.o
			fi
		fi

		if test "$have_hb" != "yes"; then
			AC_CHECK_PROG(libotf_config,libotf-config,yes)
			if test "$libotf_config" = "yes" ; then
				OT_LAYOUT_CFLAGS="`libotf-config --cflags` -DUSE_OT_LAYOUT"
				OT_LAYOUT_LIBS="`libotf-config --libs`"
				OT_LAYOUT_OBJ=otf.o
			fi
		fi
	fi

	if test "$have_hb" = "yes" ; then
		if test "$gui" = "quartz" ; then
			AC_CHECK_HEADER(harfbuzz/hb.h, have_hb_h=yes, have_hb_h=no)
			AC_CHECK_HEADER(harfbuzz/hb-coretext.h, have_hb_coretext_h=yes,
					have_hb_coretext_h=no)
			if test "$have_hb_h" = "yes" -a "$have_hb_coretext_h" != "yes" ; then
				echo ""
				echo "** ERROR **"
				echo "harfbuzz/hb-coretext.h is not found."
				echo "Install harfbuzz with --with-coretext=yes in advance."
				echo ""
			fi
		elif test "$PKG_CONFIG" != "" ; then
			OT_LAYOUT_CFLAGS="$OT_LAYOUT_CFLAGS `$PKG_CONFIG freetype2 --cflags`"
		fi
	fi

	if test "$OT_LAYOUT_OBJ" != "" -a "$enable_shared" = "no" ; then
		OT_LAYOUT_CFLAGS="$OT_LAYOUT_CFLAGS -DNO_DYNAMIC_LOAD_OTL"
		OT_LAYOUT_LIBS_FOR_PROG="$OT_LAYOUT_LIBS"
	fi
fi
AC_SUBST(OT_LAYOUT_LIBS)
AC_SUBST(OT_LAYOUT_LIBS_FOR_PROG)
AC_SUBST(OT_LAYOUT_CFLAGS)
AC_SUBST(OT_LAYOUT_OBJ)

if test "$OT_LAYOUT_OBJ" != "" ; then
	MAKE_DIRS="xwindow/libotl ${MAKE_DIRS}"
	OUTPUT_FILES="xwindow/libotl/Makefile ${OUTPUT_FILES}"
fi

if test "${NODL_OBJ}" != "" ; then
	TYPE_LIBS=libtype.a
	TYPE_LIBS_FOR_PROG="../xwindow/libtype/libtype.a ${CAIRO_LIBS} ${XFT_LIBS} ${OT_LAYOUT_LIBS}"
fi

if test "${TYPE_CFLAGS}" = "" ; then
	echo "No type engine is specified."
	exit  1
fi

AC_SUBST(XFT_CFLAGS)
AC_SUBST(XFT_LIBS)
AC_SUBST(CAIRO_LIBS)
AC_SUBST(CAIRO_LIBS)
AC_SUBST(TYPE_CFLAGS)
AC_SUBST(TYPE_LIBS)
AC_SUBST(TYPE_LIBS_FOR_PROG)
AC_SUBST(TYPE_LIBS_FOR_VTE)
AC_SUBST(NODL_OBJ)
AC_SUBST(PROG)

AC_ARG_ENABLE(ssh2,
	[  --disable-ssh2          libssh2 @<:@default=enabled@:>@],
	ssh2=$enable_ssh2, ssh2="yes")
if test "$ssh2" = "yes" ; then
	AC_CHECK_LIB(ssh2,libssh2_init,
		[
		SSH2_CFLAGS="-DUSE_LIBSSH2"
		if test "x$allow_undefined_flag" = "xunsupported" ; then
			# ml_pty_ssh.c uses CRYPTO_xxx functions.
			SSH2_LIBS="-lcrypto -lssh2"
		else
			SSH2_LIBS="-lssh2"
		fi
		], ssh2="no")
fi

if test "$ml_cv_is_posix" = "no" ; then
	if test "$ssh2" = "yes" ; then
		ML_PTY_OBJ="ml_pty_ssh.o"
		# Not dlopen
		SSH2_CFLAGS="$SSH2_CFLAGS -DNO_DYNAMIC_LOAD_SSH"
		SSH2_LIBS_FOR_PROG="$SSH2_LIBS -lwsock32 -lws2_32"
		SSH2_LIBS=
	else
		ML_PTY_OBJ="ml_pty_pipewin32.o"
	fi
else
	ML_PTY_OBJ="ml_pty_unix.o"

	if test "$ssh2" = "yes" ; then
		if test "$with_pthread" != "no" ; then
			AC_CHECK_HEADER(pthread.h,
				[
				SSH2_CFLAGS="$SSH2_CFLAGS -DHAVE_PTHREAD"
				SSH2_LIBS="$SSH2_LIBS -lpthread"
				])
			# NetBSD doesn't allow shared libraries to link libpthread
			# without linking it to executables.
			case "${host_os}" in
			netbsd*)
				PTHREAD_LIB="-lpthread"
				;;
			esac
		fi

		ML_PTY_OBJ="ml_pty_ssh.o ${ML_PTY_OBJ}"

		if test "$enable_shared" = "no" ; then
			SSH2_CFLAGS="$SSH2_CFLAGS -DNO_DYNAMIC_LOAD_SSH"
			SSH2_LIBS_FOR_PROG="$SSH2_LIBS"
			SSH2_LIBS=
		else
			# dlopen libptyssh
			MAKE_DIRS="mlterm/libptyssh ${MAKE_DIRS}"
			OUTPUT_FILES="mlterm/libptyssh/Makefile ${OUTPUT_FILES}"
		fi
	fi
fi
AC_SUBST(SSH2_LIBS)
AC_SUBST(SSH2_LIBS_FOR_PROG)
AC_SUBST(SSH2_CFLAGS)
AC_SUBST(ML_PTY_OBJ)

AC_ARG_ENABLE(utmp,
	[  --disable-utmp          utmp @<:@default=enabled@:>@],
	utmp=$enable_utmp,
	[
	case "${host_os}" in
	*darwin*)
		utmp="no"
		;;
	*)
		utmp="yes"
		;;
	esac
	])
AC_ARG_ENABLE(pty_helper,
	[  --enable-pty-helper     use pty helper @<:@default=disabled@:>@],
	pty_helper=$enable_pty_helper)
if test "$utmp" = "yes" -a "$pty_helper" != "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

if test "$gui" = "fb" ; then
	INSTALL_OPT="-m 4755 -o root"
fi

AC_SUBST(UTMP_CFLAGS)
AC_SUBST(INSTALL_OPT)

if test "${have_gtk}" = "yes" ; then
	if test "${gtk_version}" = "3.0" ; then
		PKG_CHECK_MODULES(VTE, vte-2.91, have_vte=yes, have_vte=no)
		if test "$have_vte" = "yes" ; then
			VTE_ABI=2.91
		else
			PKG_CHECK_MODULES(VTE, vte-2.90, have_vte=yes, have_vte=no)
			if test "$have_vte" = "yes"; then
				VTE_ABI=2.90
			fi
		fi
	else
		PKG_CHECK_MODULES(VTE, vte, have_vte=yes, have_vte=no)
	fi
	if test "$have_vte" = "yes" ; then
		OUTPUT_FILES="gtk/Makefile ${OUTPUT_FILES}"
		AC_SUBST(VTE_ABI)
	fi
else
	have_vte=no
fi

tools="mlclient,mlconfig,mlcc,mlterm-menu,mlimgloader,registobmp,mlfc"
AC_ARG_WITH(tools,
	[  --with-tools@<:@=ARG@:>@      external tools @<:@default=mlclient,mlconfig,mlcc,mlterm-menu,mlimgloader,registobmp,mlfc@:>@],
	[
	# if given --without-tools
	if test "${with_tools}" = "no" ; then
		tools=""
	else
		if test "${with_tools}" != "yes" ; then
			tools=${with_tools}
		fi
		if test -z "$IMAGELIB_CFLAGS" -a "`echo ${tools}|$GREP mlimgloader`" = "" ; then
			# If --with-imagelib option is not specified, mlimgloader is always built.
			tools=${tools},mlimgloader
		fi
	fi
	])
tools=`echo ${tools} | sed 's/,/ /g'`

AC_ARG_ENABLE(use-tools,
	[  --disable-use-tools     don't build and use external tools @<:@default=enabled@:>@])
if test "$enable_use_tools" = "no" ; then
	TOOLS_CFLAGS="-DNO_TOOLS"
	tools=
fi
AC_SUBST(TOOLS_CFLAGS)

for tool in ${tools} ; do
case ${tool} in
mlconfig)
	if test "${have_gtk}" = "yes" -a "$gui" != "fb" -a "$gui" != "console" ; then
		MAKE_DIRS="tool/mlconfig ${MAKE_DIRS}"
		OUTPUT_FILES="tool/mlconfig/Makefile tool/mlconfig/po/Makefile.in ${OUTPUT_FILES}"
		tools_result="$tools_result $tool"
		if test "$gui" = "xlib" -a "$gtk_version" = "2.0" ; then
			XLFDSEL_OBJ="gtkxlfdsel.o"
			AC_SUBST(XLFDSEL_OBJ)
		fi
	else
		echo ""
		echo "** WARNING  **"
		echo " couldn't compile mlconfig since headers/libraries of GTK+-2 are missing."
		echo ""
	fi
	;;
mlterm-menu)
	if test "${have_gtk}" = "yes" -a "$gui" != "fb" -a "$gui" != "console" ; 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+-2 are missing."
		echo ""
	fi
	;;
mlclient)
	if test "$ml_cv_is_posix" = "yes" -a "$gui" != "console" ; then
		MAKE_DIRS="tool/mlclient ${MAKE_DIRS}"
		OUTPUT_FILES="tool/mlclient/Makefile ${OUTPUT_FILES}"
		tools_result="$tools_result $tool"
	fi
	;;
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}"
		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
	;;
mlimgloader)
	if test "$gui" != "quartz" ; then
		OUTPUT_FILES="tool/mlimgloader/Makefile ${OUTPUT_FILES}"
		MAKE_DIRS="tool/mlimgloader ${MAKE_DIRS}"
		tools_result="$tools_result $tool"

		AC_TRY_COMPILE([
			#include  <windows.h>
			#include  <gdiplus.h>],,
			have_gdiplus=yes,have_gdiplus=no)
		if test "$gui" = "win32" -a "$have_gdiplus" = "yes" ; then
			MLIMGLOADER_LIB=gdiplus
		elif test "$have_gdk_pixbuf2" = "yes" -o "$have_gdk_pixbuf1" = "yes" ; then
			MLIMGLOADER_LIB=gdk-pixbuf
		else
			MLIMGLOADER_LIB=none
		fi
		AC_SUBST(MLIMGLOADER_LIB)
	fi
	;;
registobmp)
	PKG_CHECK_MODULES(SDLTTF, SDL_ttf, have_sdlttf=yes, have_sdlttf=no)
	if test "$have_sdlttf" = "yes" ; then
		AC_CHECK_PROG(sdl_config,sdl-config,yes)
		if test "$sdl_config" = "yes" ; then
			SDL_CFLAGS=`sdl-config --cflags`
			SDL_LIBS=`sdl-config --libs`
		else
			continue
		fi
	else
		PKG_CHECK_MODULES(SDLTTF, SDL2_ttf, have_sdlttf=yes, have_sdlttf=no)
		if test "$have_sdlttf" = "yes" ; then
			AC_CHECK_PROG(sdl2_config,sdl2-config,yes)
			if test "$sdl2_config" = "yes" ; then
				SDL_CFLAGS=`sdl2-config --cflags`
				SDL_LIBS=`sdl2-config --libs`
			else
				continue
			fi
		else
			continue
		fi
	fi

	SDL_CFLAGS="`echo $SDL_CFLAGS $SDLTTF_CFLAGS | tr -d '\r'`"
	SDL_LIBS="`echo $SDL_LIBS $SDLTTF_LIBS | tr -d '\r'`"
	AC_SUBST(SDL_CFLAGS)
	AC_SUBST(SDL_LIBS)

	if test "$gui" != "win32" ; then
		if test "$have_fc" = "" ; then
			PKG_CHECK_MODULES(FONTCONFIG, fontconfig, have_fc=yes, have_fc=no)
		fi
		if test "$have_fc" = "yes" ; then
			FONTCONFIG_CFLAGS="`echo $FONTCONFIG_CFLAGS | tr -d '\r'` -DUSE_FONTCONFIG"
			FONTCONFIG_LIBS="`echo $FONTCONFIG_LIBS | tr -d '\r'`"
			AC_SUBST(FONTCONFIG_CFLAGS)
			AC_SUBST(FONTCONFIG_LIBS)
		fi
	fi

	OUTPUT_FILES="tool/registobmp/Makefile ${OUTPUT_FILES}"
	MAKE_DIRS="tool/registobmp ${MAKE_DIRS}"
	tools_result="$tools_result $tool"
	;;
mlfc)
	if test "$gui" != "win32" -a "$gui" != "quartz" ; then
		if test "$have_fc" = "" ; then
			PKG_CHECK_MODULES(FONTCONFIG, fontconfig, have_fc=yes, have_fc=no)
		fi
		if test "$have_fc" = "yes" ; then
			FONTCONFIG_CFLAGS="`echo $FONTCONFIG_CFLAGS | tr -d '\r'` -DUSE_FONTCONFIG"
			FONTCONFIG_LIBS="`echo $FONTCONFIG_LIBS | tr -d '\r'`"
			AC_SUBST(FONTCONFIG_CFLAGS)
			AC_SUBST(FONTCONFIG_LIBS)
			OUTPUT_FILES="tool/mlfc/Makefile ${OUTPUT_FILES}"
			MAKE_DIRS="tool/mlfc ${MAKE_DIRS}"
			tools_result="$tools_result $tool"
		fi
	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"
fi
AC_SUBST(DND_CFLAGS)


DL_LIBS_IM=""

#
# --- kbd ---
#
AC_ARG_ENABLE(kbd,
	[  --disable-kbd           input method for Arabic and Indic @<:@default=enabled@:>@],
	kbd=$enable_kbd)
if test "$enable_shared" = "no" ; then
	kbd="no"
fi

if test "$kbd" != "no" ; then
	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,
	[  --disable-uim           uim @<:@default=enabled@:>@],
	uim=$enable_uim)
if test "$enable_shared" = "no" -o "$gui" = "quartz" ; then
	uim="no"
fi

if test "x$uim" != xno ; then
	PKG_CHECK_MODULES(UIM, uim >= 1.0.0, have_uim=yes, have_uim=no)
	if test "x$have_uim" = xyes ; then
		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)

#
# --- m17n library ---
#
AC_ARG_ENABLE(m17nlib,
	[  --disable-m17nlib       m17n library @<:@default=enabled@:>@],
	m17nlib=$enable_m17nlib)
if test "$enable_shared" = "no" -o "$gui" = "quartz" ; then
	m17nlib="no"
fi

if test "x$m17nlib" != xno ; then
	AC_CHECK_PROG(m17nlib, m17n-config, yes, no)
	m17n_saved_libs="$LIBS"
	M17NLIB_LIBS=`m17n-config --libs`
	LIBS="$LIBS $M17NLIB_LIBS"
	AC_CHECK_LIB(m17n, minput_open_im,
		[
		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)

#
# --- IBUS ---
#
AC_ARG_ENABLE(ibus,
	[  --disable-ibus          IBUS @<:@default=enabled@:>@],
	ibus=$enable_ibus)
if test "$enable_shared" = "no" -o "$gui" = "quartz" ; then
	ibus="no"
fi

if test "x$ibus" != "xno" ; then
	PKG_CHECK_MODULES(IBUS, ibus-1.0 >= 1.3.0, has_ibus=yes, has_ibus=no)
	if test "x$has_ibus" = xyes ; then
		MAKE_DIRS="inputmethod/ibus ${MAKE_DIRS}"
		OUTPUT_FILES="inputmethod/ibus/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != xno ; then
			DL_LIBS_IM="-dlopen \$(top_builddir)/inputmethod/ibus/libim-ibus.la ${DL_LIBS_IM}"
		fi
		if test "$with_pthread" != "no" ; then
			# NetBSD doesn't allow shared libraries to link libpthread
			# without linking it to executables.
			case "${host_os}" in
			netbsd*)
				PTHREAD_LIB="-lpthread"
				;;
			esac
		fi
		input_methods_result="$input_methods_result iBus"
	fi
fi
AC_SUBST(IBUS_CFLAGS)
AC_SUBST(IBUS_LIBS)

#
# --- FCITX ---
#
AC_ARG_ENABLE(fcitx,
	[  --disable-fcitx         FCITX @<:@default=enabled@:>@],
	fcitx=$enable_fcitx)
if test "$enable_shared" = "no" -o "$gui" = "quartz" ; then
	fcitx="no"
fi

if test "x$fcitx" != "xno" ; then
	PKG_CHECK_MODULES(FCITX, fcitx-gclient, has_fcitx=yes, has_fcitx=no)
	if test "x$has_fcitx" = xyes ; then
		MAKE_DIRS="inputmethod/fcitx ${MAKE_DIRS}"
		OUTPUT_FILES="inputmethod/fcitx/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != xno ; then
			DL_LIBS_IM="-dlopen \$(top_builddir)/inputmethod/fcitx/libim-fcitx.la ${DL_LIBS_IM}"
		fi
		if test "$with_pthread" != "no" ; then
			# NetBSD doesn't allow shared libraries to link libpthread
			# without linking it to executables.
			case "${host_os}" in
			netbsd*)
				PTHREAD_LIB="-lpthread"
				;;
			esac
		fi
		input_methods_result="$input_methods_result fcitx"
	fi
fi
AC_SUBST(FCITX_CFLAGS)
AC_SUBST(FCITX_LIBS)

#
# --- SCIM ---
#
AC_ARG_ENABLE(scim,
	[  --disable-scim          SCIM @<:@default=enabled@:>@],
	scim=$enable_scim)
if test "$enable_shared" = "no" -o "$gui" = "quartz" ; then
	scim="no"
fi

if test "x$scim" != "xno" -a "$gui" != "fb" -a "$gui" != "console" ; 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
		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)


#
# --- CANNA ---
#
AC_ARG_ENABLE(canna,
	[  --disable-canna         CANNA @<:@default=enabled@:>@],
	canna=$enable_canna)
if test "$enable_shared" = "no" -o "$ml_cv_is_posix" = "no" ; then
	canna="no"
fi

if test "x$canna" != "xno" ; then
	AC_CHECK_HEADER(canna/jrkanji.h, has_canna=yes, has_canna=no)
	if test "x$has_canna" = xyes ; then
		CANNA_LIBS="-lcanna"
		MAKE_DIRS="inputmethod/canna ${MAKE_DIRS}"
		OUTPUT_FILES="inputmethod/canna/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != xno ; then
			DL_LIBS_IM="-dlopen \$(top_builddir)/inputmethod/canna/libim-canna.la ${DL_LIBS_IM}"
		fi
		input_methods_result="$input_methods_result canna"
	fi
fi
AC_SUBST(CANNA_CFLAGS)
AC_SUBST(CANNA_LIBS)


#
# --- WNN ---
#
AC_ARG_ENABLE(wnn,
	[  --disable-wnn           WNN @<:@default=enabled@:>@],
	wnn=$enable_wnn)
if test "$enable_shared" = "no" -o "$ml_cv_is_posix" = "no" ; then
	wnn="no"
fi

if test "x$wnn" != "xno" ; then
	AC_CHECK_HEADER(wnn/jllib.h, has_wnn=yes, has_wnn=no)
	if test "x$has_wnn" = xyes ; then
		WNN_LIBS="-ljd"
		if test -d /usr/pkg/share/wnn ; then
			WNN_CFLAGS="-DWNNLIBDIR=\\\"/usr/pkg/share\\\""
		elif test -d /usr/local/share/wnn ; then
			WNN_CFLAGS="-DWNNLIBDIR=\\\"/usr/local/share\\\""
		elif test -d /usr/local/lib/wnn ; then
			WNN_CFLAGS="-DWNNLIBDIR=\\\"/usr/local/lib\\\""
		elif test -d /usr/share/wnn ; then
			WNN_CFLAGS="-DWNNLIBDIR=\\\"/usr/share\\\""
		else
			WNN_CFLAGS="-DWNNLIBDIR=\\\"/usr/lib\\\""
		fi
		MAKE_DIRS="inputmethod/wnn ${MAKE_DIRS}"
		OUTPUT_FILES="inputmethod/wnn/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != xno ; then
			DL_LIBS_IM="-dlopen \$(top_builddir)/inputmethod/wnn/libim-wnn.la ${DL_LIBS_IM}"
		fi
		input_methods_result="$input_methods_result wnn"
	fi
fi
AC_SUBST(WNN_CFLAGS)
AC_SUBST(WNN_LIBS)


#
# --- SKK ---
#
AC_ARG_ENABLE(skk,
	[  --disable-skk           SKK @<:@default=enabled@:>@],
	skk=$enable_skk)
if test "$enable_shared" != "no" -a "$skk" != "no"; then
	MAKE_DIRS="inputmethod/skk ${MAKE_DIRS}"
	OUTPUT_FILES="inputmethod/skk/Makefile ${OUTPUT_FILES}"
	if test "x$with_libltdl" != xno ; then
		DL_LIBS_IM="-dlopen \$(top_builddir)/inputmethod/skk/libim-skk.la ${DL_LIBS_IM}"
	fi
	if test "$ml_cv_is_posix" = "no" ; then
		SKK_LIBS="-lwsock32 -lws2_32"
	fi
	input_methods_result="$input_methods_result skk"
fi
AC_SUBST(SKK_LIBS)


if test "x$input_methods_result" != x ; then
	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,pixmap_engine"

AC_ARG_WITH(scrollbars,
	[  --with-scrollbars@<:@=ARG@:>@ scrollbar plugins (sample, extra, pixmap_engine) @<:@default=sample,extra,pixmap_engine@:>@],
	[
	# 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
	])

if test "$enable_shared" != "no" ; then
	scrollbars=`echo ${scrollbars} | sed 's/,/ /g'`
else
	scrollbars=
fi

for scrollbar in ${scrollbars} ; do
case ${scrollbar} in
sample)
	if test "$gui" != "fb" -a "$gui" != "console" -a "$gui" != "quartz" ; then
		MAKE_DIRS="scrollbar/sample ${MAKE_DIRS}"
		OUTPUT_FILES="scrollbar/sample/Makefile ${OUTPUT_FILES}"
		if test "x$with_libltdl" != "xno" ; then
			DL_LIBS_SB="-dlopen \$(top_builddir)/scrollbar/sample/libsample.la ${DL_LIBS_SB}"
		fi
		scrollbars_result="$scrollbars_result $scrollbar"
	fi
	;;
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."
		echo ""
	elif test "$gui" = "xlib" ; then
		if test "$have_gdk_pixbuf1" = "yes" -o "$have_gdk_pixbuf2" = "yes" ; 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="-dlopen \$(top_builddir)/contrib/scrollbar/pixmap_engine/libpixmap_engine.la ${DL_LIBS_SB}"
			fi
			SB_CFLAGS="-DSUPPORT_PIXMAP_ENGINE"
			scrollbars_result="$scrollbars_result $scrollbar"
		else
			echo ""
			echo "** ERROR **"
			echo "To use pixmap_engine for scrollbar, you need gdk-pixbuf library."
			echo ""
		fi
	fi
	;;
*)
	echo "${scrollbar} is unknown scrollbar."
	;;
esac
done # for scrollbar in ${scrollbars}

AC_SUBST(SB_CFLAGS)
AC_SUBST(DL_LIBS_SB)


#
# --- 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)


AC_ARG_ENABLE(vt52,
	[  --enable-vt52           enable VT52 emulation @<:@default=disabled@:>@],
	vt52=$enable_vt52,
	vt52="no")
if test "$vt52" = "yes" ; then
	VT52_CFLAGS="-DUSE_VT52"
fi
AC_SUBST(VT52_CFLAGS)

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.7.2)
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)

# Because pic object files are fat, non-pic ones are linked unless --disable-static
# option is specified.
if test "$enable_static" = "no" ; then
	MAIN_OBJ_SUFFIX=":.o=.lo"
fi
AC_SUBST(MAIN_OBJ_SUFFIX)

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="main/version.h ${OUTPUT_FILES}"

if test "${SUBDIRS}" != "" ; then
	ac_configure_args="${ac_configure_args} --without-funcs-mlterm-unuse"
	# for mlterm-x.x.x/fb
	ac_configure_args="${ac_configure_args} --with-gui=fb --with-type-engines=xcore --without-tools"
	if test "$enable_shared" = "no" ; then
		ac_configure_args="${ac_configure_args} --disable-dl-table"
	fi

	ac_configure_args="${ac_configure_args} --disable-ind --disable-fribidi"
	if test "$dl_ctl" = "no" -a "`echo $SUBDIRS |grep fb`" != "" ; then
		echo "libind and fribidi are disabled."
	fi

	AC_CONFIG_SUBDIRS(${SUBDIRS})
fi

MAKE_DIRS="mlterm mlterm/libctl xwindow/libtype xwindow main man etc ${MAKE_DIRS}"
OUTPUT_FILES="Makefile mlterm/Makefile mlterm/libctl/Makefile xwindow/libtype/Makefile xwindow/Makefile main/Makefile man/Makefile etc/Makefile java/Makefile ${OUTPUT_FILES}"

AC_ARG_ENABLE(ind,
	[  --disable-ind           libind @<:@default=enabled@:>@],
	ind=$enable_ind,
	[
	if test "$gui" = "quartz" ; then
		ind="no"
	else
		ind="yes"
	fi
	])
if test "$ind" = "yes" ; then
	IND_CFLAGS='-I${top_builddir}/libind'
	IND_LIBS='${top_builddir}/libind/libind.a'
	MAKE_DIRS="libind ${MAKE_DIRS}"
	OUTPUT_FILES="libind/Makefile ${OUTPUT_FILES}"
	if test "$dl_ctl" = "yes" ; then
		CTL_LIBS="libctl_iscii.la ${CTL_LIBS}"
	else
		CTL_CFLAGS="-DUSE_IND ${CTL_CFLAGS}"
		CTL_LIBS="libctl_iscii.a ${CTL_LIBS}"
		CTL_LIBS_FOR_PROG="../mlterm/libctl/libctl_iscii.a ${CTL_LIBS_FOR_PROG}"
	fi
fi
AC_SUBST(IND_LIBS)
AC_SUBST(IND_CFLAGS)
AC_SUBST(CTL_LIBS)
AC_SUBST(CTL_LIBS_FOR_PROG)

AC_SUBST(PTHREAD_LIB)

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"
dnl echo "Optimization for redrawing a line : $optimize_redrawing"
echo "BiDi rendering (Fribidi)          : $have_fribidi"
echo "Indic rendering                   : $ind"
if test "$OT_LAYOUT_OBJ" = "hb.o"; then
	echo "OpenType Layout                   : yes (harfbuzz)"
	if test "$have_hb_h" = "yes" -a "$have_hb_coretext_h" != "yes" ; then
		echo "                                    (Error: hb-coretext.h is missing)"
	fi
elif test "$OT_LAYOUT_OBJ" = "otf.o"; then
	echo "OpenType Layout                   : yes (libotf)"
else
	echo "OpenType Layout                   : no"
fi
echo "External tools                    :$tools_result"
echo "Image processing                  : $image"
echo "Built-in image library            : $with_imagelib"
echo "utmp support                      : $utmp"
if test "$gui" = "fb"; then
	if test "$have_ft" = "yes"; then
		echo "Supported font formats            : pcf ttf"
	else
		echo "Supported font formats            : pcf"
	fi
elif test "$gui" != "win32"; then
	echo "Type engines                      :$type_engines_result"
fi
echo "DnD                               : $dnd"
echo "Input Methods                     : XIM$input_methods_result"
echo "Scrollbars                        : simple${scrollbars_result}"
echo "libssh2                           : $ssh2"
echo "GTK+                              : $have_gtk ($gtk_version)"
if test "$VTE_ABI" = ""; then
	vte_version=""
else
	vte_version=" (${VTE_ABI})"
fi
echo "libvte                            : $have_vte$vte_version"
echo "VT52                              : $vt52"
echo
