

SUBDIRS	= am
BIN	= 

HTMGIFS	= logo_am.gif logo_lipronet.gif logo_doxygen.gif

PWD	= $(shell pwd)

ifndef CC
export CC	= gcc
endif

ifndef CFLAGS
export CFLAGS	= -O2 -Wall
endif

ifndef CXXFLAGS
#export CXXFLAGS	= -O2 -Wall
endif

ifndef DOXYGEN
export DOXYGEN	= $(shell which doxygen)
endif

ifndef TEE
export TEE	= $(shell which tee)
endif

ifndef CP
export CP	= $(shell which cp)
endif

ifndef MKDIR
export MKDIR	= $(shell which mkdir)
endif

ifndef CAT
export CAT	= $(shell which cat)
endif

ifndef SED
export SED	= $(shell which sed)
endif

ifndef GREP
export GREP	= $(shell which grep)
endif

ifndef FIND
export FIND	= $(shell which find)
endif



#------------------ END OF YOUR EDIT -------------------------

OBJ	=	$(patsubst %.c,%.o,$(wildcard *.c))		\
		$(patsubst %.C,%.o,$(wildcard *.C))		\
		$(patsubst %.cc,%.o,$(wildcard *.cc))		\
		$(patsubst %.cxx,%.o,$(wildcard *.cxx))		\
		$(patsubst %.cpp,%.o,$(wildcard *.cpp))		\
		$(patsubst %.m,%.o,$(wildcard *.m))		\
		$(patsubst %.s,%.o,$(wildcard *.s))		\
		$(patsubst %.S,%.o,$(wildcard *.S))

ARC	=	$(addsuffix .a,$(word $(words 			\
			$(subst /, ,$(PWD))),		\
			$(subst /, ,$(PWD))))

VPATH = $(shell $(FIND) -type d | $(GREP) -v doc | $(GREP) -v dxg)
override CFLAGS		+= $(patsubst %,-I%,$(subst :, ,$(VPATH)))
override CXXFLAGS	+= $(patsubst %,-I%,$(subst :, ,$(VPATH)))
override LDLAGS		+= $(patsubst %,-L%,$(subst :, ,$(VPATH)))

TOOLBINS = $(PWD)/cpmtools/bin

.PHONY:	all		doc		clean		\
	suball		subdoc		subclean	\
	distclean	docclean			\
	subdistclean	subdocclean

all: suball $(OBJ) $(ARC) $(BIN)

doc: dxg/Doxyfile subdoc $(addprefix $(PWD)/doc/autogen/html/, $(HTMGIFS))
	@$(DOXYGEN) $< 2>&1 | $(TEE) $@.log

clean: subclean
	@echo "--- remove" $(OBJ) $(ARC) $(BIN)
	@$(RM) $(OBJ) $(ARC) $(BIN)

distclean: subdistclean clean
	@echo "--- remove backup stuff"
	@$(RM) *~ *.bak *.old
	@echo "--- remove log files"
	@$(RM) *.log
	@echo "--- remove critical error files"
	@$(RM) core

docclean: subdocclean
	@echo "--- remove hole documentation"
	@$(RM) -rf doc/autogen/*

suball: $(addsuffix _all, $(SUBDIRS))

%_all:
	@echo ">>> change directory" $(subst _all,,$@)
	@$(MAKE) -s -C $(subst _all,,$@) all $(MAKEFLAGS)
	@echo "<<< leave directory" $(subst _all,,$@)

subdoc: $(addsuffix _doc, $(SUBDIRS))

%_doc:
	@echo ">>> change directory" $(subst _all,,$@)
	@$(MAKE) -s -C $(subst _doc,,$@) doc $(MAKEFLAGS)
	@echo "<<< leave directory" $(subst _all,,$@)

subclean: $(addsuffix _clean, $(SUBDIRS))

%_clean:
	@echo ">>> change directory" $(subst _all,,$@)
	@$(MAKE) -s -C $(subst _clean,,$@) clean $(MAKEFLAGS)
	@echo "<<< leave directory" $(subst _all,,$@)

subdistclean: $(addsuffix _distclean, $(SUBDIRS))

%_distclean:
	@echo ">>> change directory" $(subst _all,,$@)
	@$(MAKE) -s -C $(subst _distclean,,$@) distclean $(MAKEFLAGS)
	@echo "<<< leave directory" $(subst _all,,$@)

subdocclean: $(addsuffix _docclean, $(SUBDIRS))

%_docclean:
	@echo ">>> change directory" $(subst _all,,$@)
	@$(MAKE) -s -C $(subst _docclean,,$@) docclean $(MAKEFLAGS)
	@echo "<<< leave directory" $(subst _all,,$@)

$(PWD)/doc/autogen/html/%: $(PWD)/dxg/images/%
	@echo "--- copy needed HTML image:" $(notdir $@)
	@$(MKDIR) -p doc/autogen/html
	@$(CP) -f $< $@

# Delete the default suffixes and
# define our own suffix list.
.SUFFIXES:
.SUFFIXES: .c .C .cc .cxx .cpp .m .s .S .h .o

.c.o:
	@echo "--- build" $@ "from" $<
	@$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $< >$(subst .o,.log,$@) 2>&1

.C.o:

.cc.o:

.cxx.o:

.cpp.o:

.m.o:

.s.o:

.S.o:

$(ARC): $(OBJ)
	@echo "--- build" $@ "from" $?
	@$(AR) rcs $@ $?

$(BIN): $(ARC)
	@echo "--- build" $@ "from" $?
	@$(CC) -o $@ $? $(shell $(FIND) -name "*.a")

.PHONY:	bootdisk amdisk cpmLzdisk $(PWD)/$(SUBDIRS)/PCMSYS.BIN

$(PWD)/$(SUBDIRS)/PCMSYS.BIN: am_all

bootdisk: amdisk

amdisk: $(PWD)/buildsk.sh $(TOOLBINS) $(PWD)/$(SUBDIRS)/PCMSYS.BIN
	@$< $@
	
