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

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

BINDIR = $(DESTDIR)$(bindir)
LIBEXECDIR = $(DESTDIR)$(libexecdir)/mlterm
LIBEXECDIR_win32 = $(DESTDIR)$(bindir)

VPATH = $(top_srcdir)/main

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

LPOBL = @LPOBL@
LMEF = @LMEF@
LMLTERM = ../vtemu/libmlterm@TRUECOLOR_TAG@.a ../vtemu/@LMLTERM_CORE@
LUITK = ../uitoolkit/libuitoolkit.a
LPOBL_DEB = -lpobl_deb
LMEF_DEB = -lmef_deb

# XDATADIR is to avoid conflicting with DATADIR structure in w32api/objidl.h.
CFLAGS = $(CFLAGS_LOCAL) @POBL_CFLAGS@ @MEF_CFLAGS@ @DEB_CFLAGS@ @BRLAPI_CFLAGS@ \
	@X_CFLAGS@ @GUI_CFLAGS@ @SSH2_CFLAGS@ @TYPE_CFLAGS@ @FT_CFLAGS@ @WAYLAND_CFLAGS@ @SDL2_CFLAGS@ \
	@DAEMON_CFLAGS@ @SPLIT_CFLAGS@ @DND_CFLAGS@ @TRANSFER_CFLAGS@ @IMAGE_CFLAGS@ \
	@TRUECOLOR_CFLAGS@ @CFLAGS@ @CPPFLAGS@ -DSYSCONFDIR=\"$(sysconfdir)\" \
	-I$(top_srcdir)/vtemu \
	-I${top_srcdir}/uitoolkit -I${top_builddir}/main -I/usr/local/include

CFLAGS_FOR_FEATURES = @OT_LAYOUT_CFLAGS@ @CTL_CFLAGS@ @IM_CFLAGS@ @IMAGELIB_CFLAGS@ @UTMP_CFLAGS@

LIBS1 = $(LIBS_LOCAL) $(LUITK) $(LMLTERM) $(LMEF) $(LPOBL) -L/usr/local/lib \
	-R/usr/local/lib @SSH2_LIBS_FOR_PROG@ @BRLAPI_LIBS@ @OT_LAYOUT_LIBS_FOR_PROG@ @PTHREAD_LIB@ @REGEX_LIBS@

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

LIBS2_fb = @MATH_LIBS@ @DL_LIBS_IM@ @DL_LIBS_SB@ @FT_LIBS@ @SOCK_LIBS@

LIBS2_win32 = -mwindows -limm32 ../uitoolkit/winrs.o # @SUBSYSTEM@

LIBS2_quartz = @COCOA_LIBS@

LIBS2_beos = -lbe -ltranslation -lsocket

LIBS2_console = @SIXEL_LIBS@ @MATH_LIBS@ @DL_LIBS_IM@ @DL_LIBS_SB@ @SOCK_LIBS@

LIBS2_wayland = @WAYLAND_LIBS@ @FT_LIBS@ @MATH_LIBS@ @DL_LIBS_IM@ @DL_LIBS_SB@ @SOCK_LIBS@

LIBS2_sdl2_win32 = ../uitoolkit/winrs.o

LIBS2_sdl2 = @SDL2_LIBS@ @MATH_LIBS@ @DL_LIBS_IM@ @DL_LIBS_SB@ @FT_LIBS@ @SOCK_LIBS@ $(LIBS2_@GUI@_@SDL2GUI@)

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

PROG_xlib = mlterm
PROG_win32 = mlterm
PROG_fb = @MLTERM_FB@
PROG_console = mlterm-con-server
PROG_quartz = mlterm
PROG_beos = mlterm
PROG_wayland = mlterm-wl
PROG_sdl2 = mlterm-sdl2
PROG2_console = mlterm-con

INSTALL_OPT = @INSTALL_OPT@

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

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

debug:
	$(MAKE) LPOBL="$(LPOBL_DEB)" LMEF="$(LMEF_DEB)" all

$(PROG_@GUI@): $(OBJ) $(LMLTERM) $(LUITK)
	if test "@GUI@" = "beos"; then \
		$(LIBTOOL_LINK_CXX) -o $(PROG_@GUI@) $(OBJ@MAIN_OBJ_SUFFIX@) \
			$(LIBS) @CTL_LIBS_FOR_PROG@ ; \
	else \
		$(LIBTOOL_LINK) -o $(PROG_@GUI@) $(OBJ@MAIN_OBJ_SUFFIX@) \
			$(LIBS) @CTL_LIBS_FOR_PROG@ ; \
	fi

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

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

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

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

.cpp.o:
	$(LIBTOOL_CXX) -c $<

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

$(LIBEXECDIR@WIN32TAG@):
	mkdir -p $(LIBEXECDIR@WIN32TAG@)

install: $(BINDIR) $(LIBEXECDIR@WIN32TAG@)
	if test "@GUI@" = "console" ; then \
		$(LIBTOOL_INSTALL) $(PROG2_@GUI@) $(BINDIR) ; \
		$(LIBTOOL_INSTALL) $(INSTALL_OPT) $(PROG_@GUI@) $(LIBEXECDIR@WIN32TAG@) ; \
	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
