override CFLAGS := -MD -Wall -Werror -D_GNU_SOURCE -g $(CFLAGS)
OBJS=reptyr.o reallocarray.o attach.o
DEPS=$(wildcard *.d platform/*/*.d)
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
	OBJS += platform/linux/linux_ptrace.o platform/linux/linux.o
endif
ifeq ($(UNAME_S),FreeBSD)
	OBJS += platform/freebsd/freebsd_ptrace.o platform/freebsd/freebsd.o
	LDFLAGS += -lprocstat
endif
# Note that because of how Make works, this can be overridden from the
# command-line.
#
# e.g. install to /usr with `make PREFIX=/usr`
PREFIX=/usr/local
BINDIR=$(PREFIX)/bin
MANDIR=$(PREFIX)/share/man

PKG_CONFIG ?= pkg-config
PYTHON_CMD ?= python2

all: reptyr

reptyr: $(OBJS)

ifeq ($(DISABLE_TESTS),)
test: reptyr test/victim PHONY
	$(PYTHON_CMD) test/basic.py
	$(PYTHON_CMD) test/tty-steal.py
else
test: all
endif

VICTIM_CFLAGS ?= $(CFLAGS)
VICTIM_LDFLAGS ?= $(LDFLAGS)
test/victim: test/victim.o
test/victim: override CFLAGS := $(VICTIM_CFLAGS)
test/victim: override LDFLAGS := $(VICTIM_LDFLAGS)

clean:
	rm -f reptyr $(OBJS) test/victim.o test/victim $(DEPS)

BASHCOMPDIR ?= $(shell $(PKG_CONFIG) --variable=completionsdir bash-completion 2>/dev/null)

install: reptyr
	install -d -m 755 $(DESTDIR)$(BINDIR)
	install -m 755 reptyr $(DESTDIR)$(BINDIR)/reptyr
	install -d -m 755 $(DESTDIR)$(MANDIR)/man1
	install -m 644 reptyr.1 $(DESTDIR)$(MANDIR)/man1/reptyr.1
	install -d -m 755 $(DESTDIR)$(MANDIR)/fr/man1
	install -m 644 reptyr.fr.1 $(DESTDIR)$(MANDIR)/fr/man1/reptyr.1
	bashcompdir=$(BASHCOMPDIR) ; \
	test -z "$$bashcompdir" && bashcompdir=/etc/bash_completion.d ; \
	install -d -m 755 $(DESTDIR)$$bashcompdir ; \
	install -m 644 reptyr.bash $(DESTDIR)$$bashcompdir/reptyr

.PHONY: PHONY

# Pull-in dependencies generated by -MD
-include $(OBJS:.o=.d)
