# EtherPix config file generator
#
# Copyright 2010-2017 Stefan Schuermans <stefan schuermans info>

TARGET := etherpix_config_gen
VER := 1
VERMIN := 2
VERREV := 0

SRCS := $(wildcard src/*.cpp)

CPP := g++
INSTALL := install

CFLAGS := -W -Wall -O3
DEFINES := -DETPCG_VER_MAJ=$(VER) -DETPCG_VER_MIN=$(VERMIN) \
           -DETPCG_VER_REV=$(VERREV)
INCLUDES :=
LDFLAGS :=
LIBS := -ldime -lm

SRCS_BASE := $(patsubst %.cpp,%,$(SRCS))
DEPS := $(addsuffix .d,$(SRCS_BASE))
OBJS := $(addsuffix .o,$(SRCS_BASE))
TGT := src/$(TARGET)

VERSION := $(VER).$(VERMIN).$(VERREV)

ENV_PREFIX := $(shell echo $$PREFIX)
STOW_DIR := /usr/local/stow
STOW_PREFIX := $(STOW_DIR)/etpConfigGen-$(VERSION)
DEF_PREFIX := /usr/local
ifneq ($(ENV_PREFIX),)
  PREFIX := $(ENV_PREFIX)
else
  ifeq ($(shell if [ -d $(STOW_DIR) ]; then echo stow; fi),stow)
    PREFIX := $(STOW_PREFIX)
  else
    PREFIX := $(DEF_PREFIX)
  endif
endif

.PHONY: all clean install uninstall
.SECONDARY:
.SUFFIXES:

all: $(DEPS) $(TGT)

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

%.d: %.cpp
	$(CPP) $(CFLAGS) $(DEFINES) $(INCLUDES) -M -o $@ $<
	sed -i 's!^$(basename $(notdir $<))!$(dir $<)&!' $@

%.o: %.cpp Makefile
	$(CPP) $(CFLAGS) $(DEFINES) $(INCLUDES) -c -o $@ $<

$(TGT): $(OBJS)
	$(CPP) $(LDFLAGS) -o $@ $+ $(LIBS)

clean:
	rm -f $(DEPS) $(OBJS) $(TGT)

install: all
	$(INSTALL) -d $(PREFIX)/bin
	$(INSTALL) -m 755 -t $(PREFIX)/bin $(TGT)

uninstall:
	rm -f $(PREFIX)/bin/$(notdir $(TGT))