ifndef PREFIX
  PREFIX=/usr/local
endif
ifeq ($(CC),cc)
  CC=gcc
endif
ifeq ($(shell $(CC) -dumpmachine),mingw32)
  EXE=.exe
  SYMLINK=mklink $(2) $(1)
  HARDLINK=mklink /H $(2) $(1)
else
  EXE=
  SYMLINK=ln -s $(1) $(2)
  HARDLINK=ln $(1) $(2)
endif
TARGETDIRS = $(PREFIX)/bin/

dupedit$(EXE): dupedit.o libwhich.o
	gcc $(LDFLAGS) $^ -o $@
dupedit.o: dupedit.c lang.h
	gcc -O3 $(CFLAGS) -I. -c $< -o $@
%.o: %.c
	gcc -Os $(CFLAGS) -c $< -o $@
lang.h:
	test -f lang/$$LANG || LANG="en.ascii"; ln -s lang/$$LANG lang.h
clean:
	rm -fr dupedit$(EXE) *.o lang.h testdir/
install: dupedit $(TARGETDIRS)
	install -s dupedit$(EXE) $(PREFIX)/bin/
uninstall:
	rm -f $(PREFIX)/bin/dupedit$(EXE)
	@if rmdir --ignore-fail-on-non-empty $(TARGETDIRS) > /dev/null 2>&1; then echo rmdir $(TARGETDIRS); fi
PHONY: clean install uninstall

testdir: testdir/
	echo hello > testdir/dup
	echo world > testdir/unik
	echo hello > testdir/hard1 && $(call HARDLINK, testdir/hard1, testdir/hard2)
	$(call SYMLINK, hard1, testdir/sym)
	touch testdir/empty1 testdir/empty2
%/:
	mkdir -p $@