CC=@CC@
CFLAGS=@CFLAGS@
C_FILES:=ccontrol.c ccontrol-parse.c ccontrol-lock.c ccontrol-identify.c stdrusty.c
GPERF_C_FILES:=extensions.c keywords.c
# Everything depends on makefile, headers, gperf files
ALLFILES=$(C_FILES) $(GPERF_C_FILES) Makefile stdrusty.h ccontrol.h
BINDIR=@BINDIR@
LIBDIR=@LIBDIR@
MANDIR=@MANDIR@
XMLTO=@XMLTO@
ASCIIDOC=@ASCIIDOC@
VALGRIND=@VALGRIND@
VERSION=@VERSION@

LINKS=$(foreach l,gcc cc g++ c++ ld make,$(prefix)$(LIBDIR)/$l)

ccontrol: $(ALLFILES) ccontrol.1
	$(CC) $(CFLAGS) -DVERSION='"$(VERSION) (@VERSION_NAME@)"' -o $@ $(C_FILES)

Makefile: Makefile.in configure config.status
	./configure --reconfigure

config.status:
	@echo Please rerun ./configure.; exit 1

ccontrol_test: $(ALLFILES)
	$(CC) $(CFLAGS) -DVERSION='"$(VERSION) (@VERSION_NAME@)"' -DTESTING -o $@ $(C_FILES)

$(GPERF_C_FILES): %.c: %.gperf
	gperf $< > $@

install: installbin installlib installman installgui

$(prefix)$(BINDIR) $(prefix)$(MANDIR)/man1 $(prefix)$(LIBDIR):
	mkdir -p $@

installgui: $(prefix)$(LIBDIR)/ccontrol-key.png $(prefix)$(BINDIR)/gccontrol.py

$(prefix)$(LIBDIR)/ccontrol-key.png: gui/ccontrol-key.png $(prefix)$(LIBDIR) 
	cp $< $@

$(prefix)$(BINDIR)/gccontrol.py: gui/gccontrol.py $(prefix)$(BINDIR) 
	cp $< $@

installman: $(prefix)$(MANDIR)/man1 $(prefix)$(MANDIR)/man1/ccontrol.1 $(prefix)$(MANDIR)/man1/ccontrol-init.1

installbin: $(prefix)$(BINDIR)/ccontrol $(prefix)$(BINDIR)/ccontrol-init

$(prefix)$(BINDIR)/ccontrol: ccontrol $(prefix)$(BINDIR)
	cp $< $@

$(prefix)$(BINDIR)/ccontrol-init: ccontrol-init
	cp $< $@

installlib: $(LINKS)

$(LINKS): %: ccontrol $(prefix)$(LIBDIR)
	ln -sf $(BINDIR)/ccontrol $@

$(prefix)$(MANDIR)/man1/ccontrol.1: ccontrol.1
	cp $< $@

$(prefix)$(MANDIR)/man1/ccontrol-init.1: ccontrol-init.1
	cp $< $@

uninstall:
	rm -f $(prefix)$(BINDIR)/ccontrol $(prefix)$(BINDIR)/ccontrol-init $(LINKS) $(prefix)$(MANDIR)/man1/ccontrol.1 $(prefix)$(MANDIR)/man1/ccontrol-init.1

check: ccontrol_test
	@testsuite/test.sh --valgrind=$(VALGRIND)

dist: clean
	rm -f Makefile config.status
	DIR=`basename \`pwd\``; cd .. && ln -sf $$DIR ccontrol-$(VERSION) && tar -c -v -j --exclude='*~' --exclude='*.orig' --exclude='*.rej' -f ccontrol-$(VERSION).tar.bz2 ccontrol-$(VERSION)/* ccontrol-$(VERSION)/.??* && rm -f ccontrol-$(VERSION)

clean:
	rm -f *.o ccontrol ccontrol_test

%: %.xml
	$(XMLTO) man $*.xml

%.xml: %.txt
	$(ASCIIDOC) -d manpage -b docbook $*.txt

distclean: clean
	rm -f $(GPERF_C_FILES) Makefile config.status ccontrol.1 ccontrol-init.1
