ifeq ($(MAKELEVEL),0)
$(error Sorry, this makefile is not intended to be \
used directly. Please use the makefile in the toplevel \
directory of the ApectC++ source tree instead)
endif

CXX ?= g++ -O6
ACFLAGS += $(ACOPTFLAGS) -k

ifeq ($(filter -r, $(ACFLAGS)),)
  ACFLAGS += -r repo.acp
endif

ifneq ($(MAKECMDGOALS),run)
  ACFLAGS += -v9
endif

PROG:=feature$(EXT)

SOURCES := $(wildcard *.cc)

ACTEST :=$(shell pwd|sed -e s+/.*/++)

JUNK    := Junk
OBJECTS := $(addprefix $(JUNK)/,$(patsubst %.cc,%.o,$(SOURCES)))
DEPS    := $(addprefix $(JUNK)/,$(patsubst %.cc,%.d,$(SOURCES)))
ACC     := $(patsubst %.cc,%.acc,$(SOURCES))
SOURCES := $(wildcard *.acc)
HEADERS := $(wildcard *.h)
ASPECTS := $(wildcard *.ah)
INCLUDE_LIST := $(patsubst %.ah,-include %.ah,$(ASPECTS))

all: $(PROG)

run: all
	./$(PROG)

check: all


diff:  $(PROG)
	@./$(PROG) > $(basename $(PROG)).out
	@diff $(basename $(PROG)).out $(basename $(PROG)).ref
	@rm $(basename $(PROG)).out


ref:   $(PROG)	
	@./$(PROG) > $(basename $(PROG)).ref


feature$(EXT): $(OBJECTS)
	@echo Linking
	@$(CXX) $(CXXFLAGS) -o $@ $(OBJECTS)


clean:
	@rm -rf *.o $(JUNK) *.acc feature$(EXT) *~ *.acp

%.acc : %.cc
	@echo Weaving $<
	$(AC) $(ACFLAGS) -p . -c $< -o $@ 


$(JUNK)/%.o : %.acc
	@echo Compiling $<
	@$(CXX) $(CXXFLAGS) -x c++ -c $< -o $@

$(JUNK)/%.d : %.cc
	@mkdir -p $(JUNK)
	@$(CXX) $(CXXFLAGS) -x c++ -MM $(INCLUDE_LIST) $< | \
        sed -e "s/$*\.o/$(JUNK)\/& $(JUNK)\/$*.d $*.acc/g" > $@


ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif

.PHONY: clean all run diff ref

# don't remove any intermediate files
.SECONDARY:
