include ../../config.mak

ifeq ($(BUILD_STATIC), yes)
  LDFLAGS += -static
endif

LDFLAGS += 
CXXFLAGS += -O2

LIB_PATH=../lib
CMDLINE_FILE='"../tok.c/cmdline.h"'
COMMON_PATH=../common

tok_c: tok.o cmdline.c common_tok.o common.o
	$(CXX) $(CXXFLAGS) -D _CMDLINE_FILE=$(CMDLINE_FILE) \
	tok.c cmdline.c common.o common_tok.o -o tok_c $(LDFLAGS)

tok.o: tok.c cmdline.h 
	$(CXX) $(CXXFLAGS) -c -D _CMDLINE_FILE=$(CMDLINE_FILE) tok.c

common_tok.o: cmdline.h  common_tok.cc common_tok.h
	$(CXX) $(CXXFLAGS) -c -D _CMDLINE_FILE=$(CMDLINE_FILE) common_tok.cc

common.o: $(COMMON_PATH)/cmdline_common.ggo $(COMMON_PATH)/common.cc \
	$(COMMON_PATH)/common.h
	$(CXX) $(CXXFLAGS) -c -D _CMDLINE_FILE=$(CMDLINE_FILE) $(COMMON_PATH)/common.cc

cmdline.ggo: cmdline_tok.ggo ../common/cmdline_common.ggo
	cat cmdline_tok.ggo ../common/cmdline_common.ggo > cmdline.ggo

cmdline.c cmdline.h: cmdline.ggo
	$(GENGETOPT) -i cmdline.ggo --conf-parser

.PHONY: install
install:
ifdef BIN_DIR
	install -m 0755 tok_c $(BIN_DIR)
endif

.PHONY: uninstall
uninstall:
ifdef BIN_DIR
	rm $(BIN_DIR)/tok_c
endif

clean: clean.cmdline
	rm *.o || true
	rm tok_c || true

clean.cmdline:
	rm cmdline.* || true
