top_builddir = ..
top_srcdir = @top_srcdir@
prefix = @prefix@
sysconfdir = @sysconfdir@
exec_prefix = @exec_prefix@
datadir = @datadir@
bindir = @bindir@
libdir = @libdir@
libexecdir = @libexecdir@

CC = @CC@
LIBTOOL = @LIBTOOL@
INSTALL = @INSTALL@

BINDIR = $(DESTDIR)$(bindir)
LIBEXECDIR = $(DESTDIR)$(libexecdir)

VPATH = $(top_srcdir)/main

OBJ_xlib = main.o
OBJ_win32 = main.o
OBJ_fb = main.o
OBJ_quartz = main-cocoa.o
OBJ_console = main.o
OBJ = daemon.o main_loop.o $(OBJ_@GUI@)

LKIK = @LKIK@
LMKF = @LMKF@
LMLTERM = ../mlterm/libmlterm.a ../mlterm/@LMLTERM_CORE@
LXWINDOW = ../xwindow/libxwindow.a
LKIK_DEB = -lkik_deb
LMKF_DEB = -lmkf_deb

# XDATADIR is to avoid conflicting with DATADIR structure in w32api/objidl.h.
CFLAGS = $(CFLAGS_LOCAL) @KIK_CFLAGS@ @MKF_CFLAGS@ @DEB_CFLAGS@ \
	@X_CFLAGS@ @GUI_CFLAGS@ @SSH2_CFLAGS@ @TYPE_CFLAGS@ @CFLAGS@ @CPPFLAGS@ \
	-DSYSCONFDIR=\"$(sysconfdir)\" -I$(top_srcdir)/mlterm \
	-I${top_srcdir}/xwindow -I${top_builddir}/main -I/usr/local/include

LIBS1 = $(LIBS_LOCAL) $(LXWINDOW) $(LMLTERM) $(LMKF) $(LKIK) \
	-L/usr/local/lib -R/usr/local/lib @SSH2_LIBS_FOR_PROG@ \
	@OT_LAYOUT_LIBS_FOR_PROG@ @PTHREAD_LIB@

LIBS2_xlib = @IMAGELIB_LIBS@ @DL_LIBS_IM@ @DL_LIBS_SB@ @MATH_LIBS@ @TYPE_LIBS_FOR_PROG@ \
	@X_LIBS@ -lX11 @X_EXTRA_LIBS@
#	@X_LIBS@ @X_PRE_LIBS@ -lX11 @X_EXTRA_LIBS@

LIBS2_fb = @MATH_LIBS@ @DL_LIBS_SB@ @FT_LIBS@

LIBS2_win32 = -mwindows -limm32 @SUBSYSTEM@ ../xwindow/winrs.o

LIBS2_quartz = -framework Foundation -framework Cocoa

LIBS2_console = @SIXEL_LIBS@

LIBS = $(LIBS1) $(LIBS2_@GUI@)

PROG_xlib = mlterm
PROG_win32 = mlterm
PROG_fb = mlterm-fb
PROG_console = mlterm-con-server
PROG_quartz = mlterm

PROG2_console = mlterm-con

INSTALL_OPT = @INSTALL_OPT@

LIBTOOL_CC = $(LIBTOOL) --mode=compile $(CC) $(CFLAGS)
LIBTOOL_LINK = $(LIBTOOL) --mode=link $(CC) @LDFLAGS@
LIBTOOL_INSTALL = $(LIBTOOL) --mode=install $(INSTALL)

all: $(PROG_@GUI@) $(PROG2_@GUI@)

debug:
	$(MAKE) LKIK="$(LKIK_DEB)" LMKF="$(LMKF_DEB)" all

$(PROG_@GUI@): $(OBJ) $(LMLTERM) $(LXWINDOW)
	$(LIBTOOL_LINK) -o $(PROG_@GUI@) $(OBJ@MAIN_OBJ_SUFFIX@) \
		$(LIBS) @CTL_LIBS_FOR_PROG@

mlterm-con: $(top_srcdir)/main/main-con.c
	$(CC) -o mlterm-con $(top_srcdir)/main/main-con.c $(CFLAGS_LOCAL) @CFLAGS@ @CPPFLAGS@ \
		@LDFLAGS@ -DLIBEXECDIR=\"$(libexecdir)\"

.SUFFIXES: .c .o
.SUFFIXES: .m .o

.c.o:
	$(LIBTOOL_CC) -c $<

.m.o:
	$(LIBTOOL_CC) -c $<

$(BINDIR):
	mkdir -p $(BINDIR)

$(LIBEXECDIR)/mlterm:
	mkdir -p $(LIBEXECDIR)/mlterm

install : $(BINDIR) $(LIBEXECDIR)/mlterm
	if test "@GUI@" = "console" ; then \
		$(LIBTOOL_INSTALL) $(PROG2_@GUI@) $(BINDIR) ; \
		$(LIBTOOL_INSTALL) $(INSTALL_OPT) $(PROG_@GUI@) $(LIBEXECDIR)/mlterm ; \
	else \
		$(LIBTOOL_INSTALL) $(INSTALL_OPT) $(PROG_@GUI@) $(BINDIR) ; \
	fi

uninstall :
	rm -f $(BINDIR)/$(PROG_@GUI@)

wc :
	find . -name "*.[ch]" -a \! -name "test_*" | xargs wc -l

clean:
	rm -rf $(PROG_@GUI@)* $(PROG2_@GUI@) *core $(OBJ) $(OBJ:.o=.lo) .libs

distclean: clean
	rm -f Makefile version.h
