# Copyright (c) 2009-2011 Centro Svizzero di Calcolo Scientifico (CSCS)
# Licensed under the GPLv2.
# NAMES
TARGET = reassemble-inventory
MAIN   = main.c
OUT    = inventory.out
# PROGRAMS
CC    = gcc
RM    = rm -vf
SHELL = /bin/bash

# PATHS
INCDIR = ../../include
LIBDIR = ../../libalps
ENGINE = ../query_alps_engine/query_alps_engine

# FLAGS
CFLAGS = -I$(INCDIR) -Wall -Werror #-pedantic
ALPSLIB= $(LIBDIR)/libalps.a
LDLIBS = -lexpat
CFLAGS += -g

# RULES
.PHONY: clean check

all: check $(TARGET)

# when apbasil is available, compare the outputs directly
ifneq ($(shell which apbasil),)
QUERY = <?xml version="1.0"?>\
	<BasilRequest protocol="$(shell $(ENGINE) --basil)"\
		      method="QUERY" type="INVENTORY"/>
	#<BasilRequest protocol="1.0"\

check: $(TARGET) $(ENGINE)
	diff -sup <(apbasil <<< '$(QUERY)' | tee $(OUT)) <(./$<)
else
check: $(TARGET)
	time ./$<
endif

$(TARGET): $(MAIN) $(ALPSLIB)
	$(CC) $(CFLAGS) $^ $(LDLIBS) -o $@

$(ENGINE):
	$(MAKE) -C $(@D) $(@F)

$(ALPSLIB): $(wildcard $(LIBDIR)/*.c)
	$(MAKE) -C $(dir $(ALPSLIB))

tags: $(wildcard *.c $(LIBDIR)/*.c $(INCDIR)/*.h)
	ctags $^ > $@

clean:
	@$(RM) *.o *~ ? core *.out tags *.orig *.rej
	@$(RM) $(TARGET)

distclean: clean
