SHELL=@SHELL@

prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
mandir=@mandir@

CC=@CC@
CFLAGS= @CFLAGS@
LDFLAGS=@LDFLAGS@
LIBS=@LIBS@
BLUETOOTH_LIBS=@BLUETOOTH_LIBS@
INSTALL=@INSTALL@
OBJEXT=@OBJEXT@
EXEEXT=@EXEEXT@

PROGRAMS=scmxx apoconv adr2vcf smi
SUBDIRS=$(PROGRAMS) atcommand charsets helper smspdu options versit

OUTPREFIX=../
BINARIES=$(patsubst %,$(OUTPREFIX)%$(EXEEXT),$(PROGRAMS))

smspdu_dep=smspdu charsets helper

scmxx_dep=scmxx atcommand $(smspdu_dep) options
scmxx_libs=$(BLUETOOTH_LIBS)
apoconv_dep=apoconv charsets helper
adr2vcf_dep=adr2vcf charsets helper versit options
smi_dep=smi $(smspdu_dep) options

.PHONY: all
all: $(BINARIES)

$(OUTPREFIX)scmxx$(EXEEXT): $(patsubst %, %.$(OBJEXT), $(scmxx_dep))
$(OUTPREFIX)apoconv$(EXEEXT): $(patsubst %, %.$(OBJEXT), $(apoconv_dep))
$(OUTPREFIX)adr2vcf$(EXEEXT): $(patsubst %, %.$(OBJEXT), $(adr2vcf_dep))
$(OUTPREFIX)smi$(EXEEXT): $(patsubst %, %.$(OBJEXT), $(smi_dep))

#GNU make will automatically call the rule for the include
#It will print a non-fatal error message, though but do not
#worry about it.
#If your make cannot do this and you cannot use GNU make,
#change this from 'include' to 'sinclude' and run
# make depend
#before compiling.
#ifneq ($(MAKECMDGOALS),clean)
#ifneq ($(MAKECMDGOALS),distclean)
#include .dependencies
#endif
#endif

#.PHONY: depend
#depend: .dependencies
#.dependencies: $(DEP_SRC) $(DEP_HEADER)
#	$(CC) $(CFLAGS) -MM $(DEP_SRC) >.dependencies

.PHONY: %-clean
%-clean:
	@$(MAKE) -C $* clean

.PHONY: clean
clean: $(patsubst %,%-clean,$(SUBDIRS))
	-rm -f $(BINARIES)

.PHONY: %-distclean
%-distclean:
	@$(MAKE) -C $* distclean

.PHONY: distclean
distclean: clean $(patsubst %,%-distclean,$(SUBDIRS))
	-rm -f .dependencies

.PHONY: install %-install
install: $(patsubst %,%-install,$(BINARIES))
%-install: %
	$(INSTALL) -d $(DESTDIR)$(bindir)
	$(INSTALL) -m 755 $* $(DESTDIR)$(bindir)

.PHONY: uninstall %-uninstall
uninstall: $(patsubst %,%-uninstall,$(BINARIES))
%-uninstall: %
	-rm -f $(DESTDIR)$(bindir)/$(notdir $*)

%.$(OBJEXT):
	@$(MAKE) -C $* all

$(BINARIES):
	$(CC) -o $@ $(patsubst %, %.$(OBJEXT), $($(notdir $(basename $@))_dep)) $(LDFLAGS) $($(notdir $(basename $@))_libs) $(LIBS)
	@if [ "$(EXEEXT)" != ".exe" ]; then chmod 755 $@; fi
