include ../globalmakeflags

SUBDIR=src

MAJOR=0
MINOR=0
MICRO=0
LDFLAGS=-L. -ldebconf @LIBS@ @RPATH@
LIBNAME=libdebconf.so
CLILIB=libdebconfclient.so
CLILIBNAME_A=libdebconfclient.a
CLILIBNAME=$(CLILIB).$(MAJOR).$(MINOR).$(MICRO)
CLISONAME=$(CLILIB).$(MAJOR)
DEBCONF=debconf
TOOLS=debconf-loadtemplate debconf-copydb debconf-communicate \
	debconf-dumpdb \
	dpkg-reconfigure #dpkg-preconfigure
BIN=$(DEBCONF) $(TOOLS)

LIBOBJS=commands.opic configuration.opic confmodule.opic debug.opic \
	database.opic debconfclient.opic frontend.opic priority.opic \
	strutl.opic question.opic template.opic rfc822.opic

CLILIBOBJS=debconfclient.opic

CLILIBOBJS_A=$(CLILIBOBJS:.opic=.o)

all: $(BIN) $(LIBNAME) $(CLILIBNAME) $(CLILIBNAME_A) modules/Makefile client/confmodule
	@$(MAKE) -C modules $@

bin: $(BIN)

$(BIN): $(LIBNAME) $(addsuffix .o,$(BIN))
	@echo Compiling tool $@
	$(CC) $(CFLAGS) -o $@ $@.o -Wl,-rpath,${moddir} $(LDFLAGS)

$(LIBNAME): $(LIBOBJS)
	@echo Creating library $@
	$(CC) $(CFLAGS) -shared -Wl -o $@ $^ -ldl -ldebian-installer

$(CLILIBNAME): $(CLILIBOBJS)
	@echo Creating shared library $@
	$(CC) $(CFLAGS) -shared -Wl,-soname,$(CLISONAME) -o $@ $^ -ldl

$(CLILIBNAME_A): $(CLILIBOBJS_A)
	$(AR) cr $@ $^
	ranlib $@

$(CLISONAME) $(CLILIB): $(CLILIBNAME)
	-@ln -sf $^ $@

commands-list.h: commands-list.in
	$(srcdir)/$(SUBDIR)/commands-list.h.build $< > $@

client/confmodule: commands-list.in client/confmodule.in
	$(srcdir)/$(SUBDIR)/client/confmodule.build $^ > $@

confmodule.o confmodule.opic: commands-list.h

install:
	install -d -m 755 $(DESTDIR)${bindir}
	install -d -m 755 $(DESTDIR)${sbindir}
	install -d -m 755 $(DESTDIR)${etcdir}
	install -d -m 755 $(DESTDIR)${libdir}
	install -d -m 755 $(DESTDIR)${moddir}
	install -d -m 755 $(DESTDIR)${sharedir}
	install -m 755 debconf debconf-loadtemplate $(DESTDIR)${bindir}
	install -m 755 dpkg-reconfigure $(DESTDIR)${sbindir}
	install -m 644 $(LIBNAME) $(DESTDIR)${moddir}
	install -m 644 $(CLILIBNAME) $(DESTDIR)${libdir}
	ln -sf $(CLILIBNAME) $(DESTDIR)${libdir}/$(CLISONAME)
	ln -sf $(CLILIBNAME) $(DESTDIR)${libdir}/$(CLILIB)
	install -m 644 $(CLILIBNAME_A) $(DESTDIR)${libdir}
	install -m 644 cdebconf.conf-dist $(DESTDIR)${etcdir}/cdebconf.conf
	install -m 644 client/confmodule $(DESTDIR)${sharedir}
	ln -sf ../../bin/$(DEBCONF) $(DESTDIR)${sharedir}/frontend
ifneq ($(TARGET),udeb)
	install -m 755 debconf debconf-copydb debconf-dumpdb $(DESTDIR)${bindir}
	install -d -m 755 $(DESTDIR)${incdir}
	install -m 644 ${srcdir}/src/debconfclient.h $(DESTDIR)${incdir}
endif
	@$(MAKE) -C modules $@

clean:
	-@rm -f $(BIN) $(LIBNAME) $(CLILIB) $(CLILIBNAME) $(CLILIBNAME_A) $(CLISONAME) *~ *.o *.opic commands-list.h client/confmodule
	@$(MAKE) -C modules clean

.PHONY: all bin install clean
