topsrc = @top_srcdir@

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
libdir = @libdir@
CC = @CC@
CFLAGS = @CFLAGS@
MAKE = @MAKE@
INSTALL = @INSTALL@
LN = @LN_S@
have_readline = @have_readline@
have_zlib = @have_zlib@
have_gnome = @have_gnome@
gnome_prefix = @gnome_prefix@
have_gtksourceview = @have_gtksourceview@
use_gtksourceview = @use_gtksourceview@

GNOME_CFLAGS = @GNOME_CFLAGS@
GNOME_LIBS = @GNOME_LIBS@
GTK_CFLAGS = @GTK_CFLAGS@
GTK_LIBS = @GTK_LIBS@
GTKSOURCEVIEW_CFLAGS = @GTKSOURCEVIEW_CFLAGS@
GTKSOURCEVIEW_LIBS = @GTKSOURCEVIEW_LIBS@
PNG_CFLAGS = @PNG_CFLAGS@
PNG_LIBS = @PNG_LIBS@
GDK_PIXBUF_CFLAGS = @GDK_PIXBUF_CFLAGS@
GDK_PIXBUF_LIBS = @GDK_PIXBUF_LIBS@
USE_NLS = @USE_NLS@
INETLIB = @INETLIB@

GTK_EXTRA_CFLAGS = -I$(topsrc)/gui2/gtkextra-lite
GTK_EXTRA_LIB = -L./gtkextra-lite -lgtkextra-lite

ifeq ($(CC),)
  CC = gcc
endif
ifeq ($(INSTALL_PROGRAM),)
  INSTALL_PROGRAM = $(INSTALL) -m 755
endif
ifeq ($(INSTALL_DATA),)
  INSTALL_DATA = $(INSTALL) -m 644
endif
ifeq ($(LN),)
  LN = ln -sf
endif

ifeq ($(have_gtksourceview),yes)
  langbase := $(shell pkg-config --variable=prefix gtksourceview-1.0)
  langdir = $(langbase)/share/gtksourceview-1.0/language-specs
  query_install_lang = install-lang
endif

ifeq ($(have_gtksourceview),no)
  ifeq ($(use_gtksourceview),yes)
    GTKSOURCEVIEW_CFLAGS = -I$(topsrc)/gui2
    GTKSOURCEVIEW_LIBS = -L./gtksourceview -lgtksourceview-lite
    MY_SOURCEVIEW_LIB = gtksourceview/libgtksourceview-lite.a
    langdir = $(prefix)/share/gretl/gtksourceview
    query_install_lang = install-lang
  endif
endif

ifeq ($(have_gnome),2.0)
  LIBS = $(GNOME_LIBS) $(GTKSOURCEVIEW_LIBS) $(GTK_EXTRA_LIB) \
	../lib/libgretl-1.0.la
  GUI_CFLAGS = $(GNOME_CFLAGS) $(GTKSOURCEVIEW_CFLAGS) $(GTK_EXTRA_CFLAGS)
  query_install_gnome = install-gnome
  GNOMEDEF = -DDATADIR=\"$(gnome_prefix)/share\"
else
  LIBS = $(GTK_LIBS) $(GTKSOURCEVIEW_LIBS) $(GTK_EXTRA_LIB) ../lib/libgretl-1.0.la
  GUI_CFLAGS = $(GTK_CFLAGS) $(GTKSOURCEVIEW_CFLAGS) $(GTK_EXTRA_CFLAGS)
endif
ifeq ($(USE_NLS),yes)
  datadir = @datadir@
  localedir = $(datadir)/locale
  NLSDEF = -DLOCALEDIR=\"$(localedir)\"
endif

DEFS = $(NLSDEF) $(GNOMEDEF)

override CFLAGS += -I.. -I$(libsrc) $(GUI_CFLAGS) $(PNG_CFLAGS) \
	$(GDK_PIXBUF_CFLAGS) $(DEFS)
override LIBS += $(PNG_LIBS) $(GDK_PIXBUF_LIBS)

# Directories
bindir	= $(prefix)/bin
gretldir = $(prefix)/share/gretl
tooldir = $(topsrc)/tools
libsrc = $(topsrc)/lib/src
clisrc = $(topsrc)/cli
docdir = $(topsrc)/doc

#### End of system configuration section. ####

SHELL = /bin/sh
LIBTOOL = ../libtool
PROG  = gretl_x11

vpath %.c $(topsrc)/gui2
vpath %.h $(topsrc)/gui2

SRCS = about.c \
	boxplots.c \
	calculator.c \
	callbacks.c \
	clipboard.c \
	cmdstack.c \
	console.c \
	database.c \
	datafiles.c \
	dialogs.c \
	dlgutils.c \
	filelists.c \
	fileselect.c \
	graph_page.c \
	gpt_control.c \
	gpt_dialog.c \
	gretl.c \
	guiprint.c \
	gui_utils.c \
	helpfiles.c \
	library.c \
	menustate.c \
	model_table.c \
	objectsave.c \
	obsbutton.c \
	selector.c \
	series_view.c \
	session.c \
	settings.c \
	ssheet.c \
	textbuf.c \
	textutil.c \
	toolbar.c \
	treeutils.c \
	webget.c 

OBJS = $(SRCS:.c=.o)

HACK_SRC = gtkfontselhack.c
HACK_HDR = gtkfontselhack.h
HACK_OBJ = gtkfontselhack.o

GTKEXTRA_LITE = gtkextra-lite/libgtkextra-lite.a

%.o: %.c
	$(CC) -c $(CFLAGS) $<
	$(CC) $(CFLAGS) -MM $< > .deps/$*.d 

$(PROG): .deps $(OBJS) $(HACK_OBJ) $(GTKEXTRA_LITE) $(MY_SOURCEVIEW_LIB)
	$(LIBTOOL) --mode=link $(CC) -o $@ $(OBJS) $(HACK_OBJ) $(LIBS) $(INETLIB)

$(HACK_OBJ): $(HACK_SRC) $(HACK_HDR)
	$(CC) $(CFLAGS) $(GTK_CFLAGS) -c $<

$(GTKEXTRA_LITE): 
	make -C gtkextra-lite

gtksourceview/libgtksourceview-lite.a:
	make -C gtksourceview

.deps:
	mkdir $@

-include .deps/*.d

.PHONY:

install: install-exec install-data $(query_install_gnome) $(query_install_lang)

install-strip: $(PROG) install-data $(query_install_gnome)
	$(LIBTOOL) --mode=install $(INSTALL_PROGRAM) -s $(PROG) $(bindir)/$(PROG)

install-data: installdirs
	$(INSTALL_PROGRAM) ../gretl_sh $(bindir)/gretl
	$(INSTALL_DATA) $(topsrc)/pixmaps/gretl-logo.xpm $(gretldir)
	$(INSTALL_DATA) $(topsrc)/COPYING $(gretldir)

install-exec: $(PROG) installdirs 
	$(LIBTOOL) --mode=install $(INSTALL_PROGRAM) $(PROG) $(bindir)/$(PROG)

install-gnome: $(PROG) installdirs 
	$(MAKE) -C ../gnome install 

install-lang: 
	$(tooldir)/mkinstalldirs $(langdir)
	$(INSTALL_DATA) $(topsrc)/gui2/gretl.lang $(langdir)
	$(INSTALL_DATA) $(topsrc)/gui2/gnuplot.lang $(langdir)

installdirs:
	$(tooldir)/mkinstalldirs $(bindir) 
	$(tooldir)/mkinstalldirs $(gretldir)

clean:
	rm -f *.o $(PROG)
	rm -rf .libs .deps
	make -C gtkextra-lite clean
	make -C gtksourceview clean

distclean: clean
	rm -f Makefile debug

