topsrc = @top_srcdir@

prefix = @prefix@
exec_prefix = @exec_prefix@
CC = @CC@
CFLAGS = @CFLAGS@
MAKE = @MAKE@
INSTALL = @INSTALL@
LN = @LN_S@

build_gui = @build_gui@
have_gtk = @have_gtk@
have_gmp = @have_gmp@

GTK_CFLAGS = @GTK_CFLAGS@
GTK_LIBS = @GTK_LIBS@
XML_CFLAGS = @XML_CFLAGS@
XML_LIBS = @XML_LIBS@
LIBOLE2_CFLAGS = @LIBOLE2_CFLAGS@
LIBOLE2_LIBS = @LIBOLE2_LIBS@
GMP_CFLAGS = @GMP_CFLAGS@
GMP_LIBS = @GMP_LIBS@
LAPACK_LIBS = @LAPACK_LIBS@
FLIB = @FLIB@
HAVE_X12A = @HAVE_X12A@
HAVE_TRAMO = @HAVE_TRAMO@
HAVE_AUDIO = @HAVE_AUDIO@
FLITE_CFLAGS = @FLITE_CFLAGS@
FLITE_LIBS = @FLITE_LIBS@

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


VPATH = $(topsrc)/plugin:$(topsrc)/tests

override CFLAGS += -I.. -I$(topsrc)/gui2 -DHAVE_CONFIG_H

LIBTOOL = ../libtool
COMPILE = $(LIBTOOL) --mode=compile $(CC) -c $(CFLAGS) 
LINK = $(LIBTOOL) --mode=link $(CC) -module -avoid-version -rpath $(plugindir)

GRETLINC = -I$(topsrc)/lib/src
GRETLLIB = ../lib/libgretl-1.0.la

ifeq ($(have_gtk),2.0)
  plugindir = $(prefix)/lib/gretl-gtk2
  GRETL_LIBOLE2_CFLAGS := -I$(topsrc)/plugin $(shell pkg-config --cflags glib-2.0)
else
  plugindir = $(prefix)/lib/gretl-gtk1
  GRETL_LIBOLE2_CFLAGS := -I$(topsrc)/plugin $(shell glib-config --cflags)
endif  

SRCS = excel_import.c \
	fiml.c \
	liml.c \
	gnumeric_import.c \
	import_common.c \
	johansen.c \
	kernel.c \
	lad.c \
	longname.c \
	mp_ols.c \
	panel_data.c \
	pca.c \
	progress_bar.c \
	range-mean.c \
	fractals.c \
	stats_tables.c \
	sysest.c \
	tramo_options.c \
	tramo-x12a.c \
	workbook.c \
	nistcheck.c \
	arma.c \
	arma_x12.c \
	logistic.c \
	tobit.c \
	garch.c \
	urcdist.c \
	vif.c \
	poisson.c

PLUGINS = arma.la \
	arma_x12.la \
	fractals.la \
	garch.la \
	johansen.la \
	kernel.la \
	lad.la \
	nistcheck.la \
	panel_data.la \
	range-mean.la \
	stats_tables.la \
	sysest.la \
	tobit.la \
	urcdist.la \
	vif.la \
	poisson.la \
	$(TXPLUGIN)

GUI_DEPENDENT_PLUGINS = excel_import.la \
	gnumeric_import.la \
	progress_bar.la \
	leverage.la \
	logistic.la \
	pca.la 

ifeq ($(build_gui),yes)
  PLUGINS += $(GUI_DEPENDENT_PLUGINS)
endif

ifeq ($(have_gmp),yes)
  PLUGINS += mp_ols.la
  GMPDEF = -DUSE_GMP
endif

ifeq ($(HAVE_TRAMO),1)
  TXPLUGIN = tramo-x12a.la
endif
ifeq ($(HAVE_X12A),1)
  TXPLUGIN = tramo-x12a.la
endif

ifeq ($(HAVE_AUDIO),1)
  PLUGINS += audio.la
  AUDIO_CFLAGS = $(FLITE_CFLAGS)
  AUDIO_LIBS = $(FLITE_LIBS)
endif  

ifeq ($(LIBOLE2_LIBS),)
  query-ms-ole.lo = ms-ole.lo
  vpath %.c $(topsrc)/plugin/libole2
  vpath %.h $(topsrc)/plugin/libole2
  LIBOLE2_CFLAGS = $(GRETL_LIBOLE2_CFLAGS)
endif

URCDATA = $(topsrc)/plugin/data/urcdata.gz

override CFLAGS += $(GRETLINC) $(GTK_CFLAGS) $(GMP_CFLAGS) $(XML_CFLAGS) \
	$(LIBOLE2_CFLAGS) $(AUDIO_CFLAGS) -I. $(GMPDEF)

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

all: .deps $(PLUGINS)

.deps:
	mkdir $@

-include .deps/*.d

stats_tables.la: stats_tables.lo
	$(LINK) -o $@ $< $(GRETLLIB)

panel_data.la: panel_data.lo
	$(LINK) -o $@ $< $(GRETLLIB)

range-mean.la: range-mean.lo
	$(LINK) -o $@ $< $(GRETLLIB)

fractals.la: fractals.lo
	$(LINK) -o $@ $< $(GRETLLIB)

lad.la: lad.lo
	$(LINK) -o $@ $< $(GRETLLIB)

kernel.la: kernel.lo
	$(LINK) -o $@ $< $(GRETLLIB)

vif.la: vif.lo
	$(LINK) -o $@ $< $(GRETLLIB)

urcdist.la: urcdist.lo
	$(LINK) -o $@ $< -lz

pca.la: pca.lo
	$(LINK) -o $@ $< $(GRETLLIB) $(LAPACK_LIBS)

tramo-x12a.la: tramo-x12a.lo tramo_options.lo
	$(LINK) -o $@ $^ $(GTK_LIBS) $(GRETLLIB)

progress_bar.la: progress_bar.lo
	$(LINK) -o $@ $< $(GTK_LIBS)

gnumeric_import.la: gnumeric_import.lo
	$(LINK) -o $@ $< $(GTK_LIBS) $(GRETLLIB) $(XML_LIBS)

excel_import.la: excel_import.lo workbook.lo $(query-ms-ole.lo)
	$(LINK) -o $@ $^ $(GTK_LIBS) $(GRETLLIB) $(LIBOLE2_LIBS)

mp_ols.la: mp_ols.lo
	$(LINK) -o $@ $< $(GRETLLIB) $(GMP_LIBS)

johansen.la: johansen.lo 
	$(LINK) -o $@ $< $(GRETLLIB) $(LAPACK_LIBS)

sysest.la: sysest.lo fiml.lo liml.lo
	$(LINK) -o $@ $^ $(GRETLLIB) $(LAPACK_LIBS)

leverage.la: leverage.lo 
	$(LINK) -o $@ $< $(GTK_LIBS) $(GRETLLIB) $(LAPACK_LIBS)

nistcheck.la: nistcheck.lo 
	$(LINK) -o $@ $< $(GRETLLIB) $(LAPACK_LIBS)

arma.la: arma.lo 
	$(LINK) -o $@ $< $(GRETLLIB) $(LAPACK_LIBS)

arma_x12.la: arma_x12.lo 
	$(LINK) -o $@ $< $(GTK_LIBS) $(GRETLLIB) $(LAPACK_LIBS)

logistic.la: logistic.lo
	$(LINK) -o $@ $< $(GTK_LIBS) $(GRETLLIB) 

tobit.la: tobit.lo
	$(LINK) -o $@ $< $(GTK_LIBS) $(GRETLLIB) 

poisson.la: poisson.lo
	$(LINK) -o $@ $< $(GTK_LIBS) $(GRETLLIB) 

garch.la: garch.lo fcp.lo
	$(LINK) -o $@ $^ $(GRETLLIB) $(LAPACK_LIBS)

audio.la: audio.lo midi_utils.lo
	$(LINK) -o $@ $^ $(GRETLLIB) $(AUDIO_LIBS)

$(GRETLLIB):
	$(MAKE) -C ../lib

.PHONY: 

install: $(GRETLLIB) $(PLUGINS) $(URCDATA) installdirs 
	$(LIBTOOL) --mode=install $(INSTALL_PROGRAM) \
	$(PLUGINS) $(plugindir)
	$(INSTALL_DATA) $(URCDATA) $(plugindir)/data

install-strip: $(GRETLLIB) $(PLUGINS) installdirs
	$(LIBTOOL) --mode=install $(INSTALL_PROGRAM) -s \
	$(PLUGINS) $(plugindir)

installdirs:
	$(topsrc)/tools/mkinstalldirs $(plugindir)
	$(topsrc)/tools/mkinstalldirs $(plugindir)/data

clean:
	rm -f *.lo *.o *.la
	rm -rf .libs .deps

distclean: clean
	rm -f Makefile

