#
# Console APT Compilation Interface
#

CXXFLAGS  = -I. -Iinit $(PICFLAGS)

include globalmakeflags

OBJS      = $(subst .cc,.o,$(wildcard */*.cc))
BIN       = capt
LDLIBS    = $(LFLAGS) $(GPMLIB) $(APTPKGLIB) $(CURSESLIB)
DESTDIR   = /usr/local

.PHONY: all bin clean distclean install
all: $(BIN)

$(BIN): $(OBJS)
	$(CXX) -o $(BIN)  $(OBJS) $(LDFLAGS) $(LDLIBS)

clean:
	rm -f $(OBJS) $(BIN)
	find . -regex '^.+\.?[od~]$$' -exec rm -f {} \;

distclean: clean
	rm -rf debian/tmp
	rm -f *-stamp build config.status config.log config.cache makefile configure init/config.h globalmakeflags

install:
	install -d -m755 $(DESTDIR)/bin
	install -s -m755 $(BIN) $(DESTDIR)/bin
	install -d -m755 $(DESTDIR)/man/man1 
	ln -s $(BIN) $(DESTDIR)/bin/apt-find
	ln -s $(BIN) $(DESTDIR)/bin/console-apt

# -- Dark magic below this point.

# Include the dependencies that are available
The_DFiles = $(wildcard */*.d)
ifneq ($(words $(The_DFiles)),0)
include $(The_DFiles)
endif 

# Dependency generation. We want to generate a .d file using gnu cpp.
# For GNU systems the compiler can spit out a .d file while it is compiling,
# this is specified with the INLINEDEPFLAG. Other systems might have a 
# makedep program that can be called after compiling, that's illistrated
# by the DEPFLAG case.
# Compile rules are expected to call this macro after calling the compiler
define DoDep
	@sed -e "1s/.*:/$(subst /,\\/,$@):/" $(basename $(@F)).d > $(@F).d
	@-rm -f $(basename $(@F)).d
	@mv $(@F).d $(dir $@)
endef

%.o: %.cc
	$(CXX) -c $(INLINEDEPFLAG) $(CPPFLAGS) $(CXXFLAGS) -o $@ $<
	$(DoDep)
