###########################################################################
 # Copyright (C) 2005-2009 Philipp Marek.                                #
 #                                                                       #
 # This program is free software;  you can redistribute it and/or modify #
 # it under the terms of the GNU General Public License version 2 as #
 # published by the Free Software Foundation.                            #
###########################################################################



################################ Definitions ################################
DIR	:= /usr/share/doc

HEADURL	:= "$URL: http://fsvs.tigris.org/svn/fsvs/tags/fsvs-1.2.9/fsvs/src/Makefile.in $"
HEADREV	:= "$Revision: 2506 $"
VERSION	= $(shell  perl -e '($$r) = (q( $(HEADREV) ) =~ m:(\d+):); $$t= q( $(HEADURL) ) =~ m:/tags/([^/]+): ? $$1 : "trunk"; print "$$t:$$r\n";' )

CFLAGS	:= @CFLAGS@
CFLAGS	+= -fgnu89-inline -Wall -funsigned-char -Os -DFSVS_VERSION='"$(VERSION)"'  -Wno-deprecated-declarations
LDFLAGS	:= @LDFLAGS@
FSVS_LDFLAGS = $(LDFLAGS)
FSVS_LIBS = -lsvn_subr-1 -lsvn_delta-1 -lsvn_ra-1 -lpcre -lgdbm -ldl
EXTRALIBS	:= @EXTRALIBS@
WAA_CHARS?= @WAA_WC_MD5_CHARS@

ifdef RPATH
LDFLAGS	+= -Wl,-rpath,$(RPATH)
endif

ifeq (@ENABLE_DEBUG@, 1)
	# The component tests need all local variables, ie. no optimization.
	CFLAGS += -O0
	CFLAGS += -DDEBUG -g
	LDFLAGS += -g
ifeq (@ENABLE_GCOV@, 1)
	CFLAGS += -fprofile-arcs -ftest-coverage
	LDFLAGS += -fprofile-arcs
endif
endif

#	CFLAGS += -m64 -Wpadded
#	LDFLAGS += -m64



C_FILES	:= $(sort $(wildcard *.c))
H_FILES	:= $(wildcard *.h)
D_FILES := $(C_FILES:%.c=.%.d)
DEST	:= fsvs


################################ Targets ###################################
ifeq (@CHROOTER_JAIL@, )
all:	deps tags check-version check-dox $(DEST) lsDEST
else
all:	tools/fsvs-chrooter
endif


check-version: config.h fsvs.c
	@dev/check-version-output.pl $^

check-dox: options.c dox/options.dox
	@dev/check-option-docs.pl $^

tags:	$(C_FILES) $(wildcard *.h)
	@echo "     $@"
	@-ctags $^
	@echo ":au BufNewFile,BufRead *.c syntax keyword Constant" $(shell grep -v "^!" < $@ | cut -f1 | grep _) > .vimrc
.IGNORE: tags
clean:
	rm -f *.o *.s $(D_FILES) $(DEST) 2> /dev/null || true

lsDEST: $(DEST)
	@ls -la $<

version:
	@echo $(VERSION)

version-nnl:
	@perl -e '$$_=shift; s/\s+$$//; print;' $(VERSION)

.SILENT: version.nnl version
.PHONY: version-nnl version 


################################ Distribution ###############################

bindir	= @bindir@
exec_prefix= @exec_prefix@
prefix	= @prefix@
mandir	= @mandir@
install:
	mkdir -p $(DESTDIR)/etc/fsvs $(DESTDIR)/var/spool/fsvs $(DESTDIR)$(bindir) $(DESTDIR)/etc/fsvs/svn/auth/svn.simple $(DESTDIR)/etc/fsvs/svn/auth/svn.ssl.server $(DESTDIR)/etc/fsvs/svn/auth/svn.ssl.client-passphrase
	install -m 0755 $(DEST) $(DESTDIR)/$(bindir)
#	install -m 0644 ../doc/fsvs.1 $(DESTDIR)/(mandir)

# No automatic rebuild (?)
#../doc/USAGE: $(C_FILES) $(H_FILES)
#.PHONY: ../doc/USAGE
DOXDIR=../../doxygen/html/
MANDIR=../../doxygen/man/man1/
MANDEST=../doc/
DOXFLAG=../../doxygen/html/index.html
$(DOXFLAG):
	( cat doxygen-data/Doxyfile-man ; echo PROJECT_NUMBER=$(VERSION)) | doxygen -
	( cat doxygen-data/Doxyfile ; echo PROJECT_NUMBER=$(VERSION)) | doxygen -
	# Change the /§* to the correct /*
	cd $(DOXDIR) && perl -i.bak -pe '1 while s#([/*])\xc2?\xa7([/*])#\1\2#;' *.html
	cd $(MANDIR) && perl -i.bak -pe '1 while s#([/*])\xc2?\xa7([/*])#\1\2#;' *.?
	rm $(DOXDIR)/*.bak $(DOXDIR)/html-doc.zip || true
	cd $(DOXDIR)/.. && zip -rq9 html-doc.zip html -x 'html/.svn/*' && tar -cf html-doc.tar --exclude .svn html && bzip2 -vkf9 html-doc.tar && gzip -vf9 html-doc.tar
$(DOXDIR)/group__cmds.html: $(DOXFLAG)
	touch $@
$(DOXDIR)/group__ignpat.html: $(DOXFLAG)
	touch $@

# Fix for badly generated man page (Doxygen)
# Some other idea? Is there some other workaround?
$(MANDEST)/fsvs.1: $(MANDIR)/cmds.1
	tools/man-repair.pl $@ "FSVS - fast versioning tool" < $<
$(MANDEST)/fsvs-howto-backup.5: $(MANDIR)/howto_backup.1
	tools/man-repair.pl $@ "FSVS - Backup HOWTO" < $<
$(MANDEST)/fsvs-howto-master_local.5: $(MANDIR)/howto_master_local.1
	tools/man-repair.pl $@ "FSVS - Master/Local HOWTO" < $<
$(MANDEST)/fsvs-options.5: $(MANDIR)/options.1
	tools/man-repair.pl $@ "FSVS - Options and configfile" < $<
$(MANDEST)/fsvs-url-format.5: $(MANDIR)/url_format.1
	tools/man-repair.pl $@ "FSVS - URL format" < $<
$(MANDEST)/fsvs-groups.5: $(MANDIR)/groups_spec.1
	tools/man-repair.pl $@ "FSVS - Group definitions" < $<
$(MANDEST)/fsvs-ignore-patterns.5: $(MANDIR)/ignpat.1
	tools/man-repair.pl $@ "FSVS - Ignore definitions" < $<

../doc/USAGE: $(DOXDIR)/group__cmds.html
	dev/dox2txt.pl $< > $@
../doc/IGNORING: $(DOXDIR)/group__ignpat.html
	dev/dox2txt.pl $< > $@
doc.g-c: ../doc/USAGE
	# Generate static text strings
	( cat $< ; echo "end" ) | dev/make_doc.pl > $@
docs: $(DOXFLAG) ../doc/USAGE ../doc/IGNORING doc.g-c 
docs: $(MANDEST)/fsvs.1 $(MANDEST)/fsvs-options.5 
docs: $(MANDEST)/fsvs-url-format.5 $(MANDEST)/fsvs-groups.5
docs: $(MANDEST)/fsvs-howto-backup.5 $(MANDEST)/fsvs-howto-master_local.5
.PHONY: docs $(DOXFLAG)


################################ Rules ######################################
%.o:	%.c
#	@echo "     CC $<"
	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<

# if the Makefile has changed, the output will (at least sometimes)
# change, too.
$(DEST): $(C_FILES:%.c=%.o)
#	@echo "     Link $@"
	$(CC) $(FSVS_LDFLAGS) -o $@ $^ $(FSVS_LIBS) $(LDLIBS) $(LIBS) $(EXTRALIBS)
ifeq (@ENABLE_RELEASE@, 1)
	-strip $@
endif


# For debugging: generate preprocessed, generate assembler
%.s:	%.c
	$(CC) $(CFLAGS) -S -fverbose-asm -o $@ $< || true
%.P	: %.c
	$(CC) $(CFLAGS) -E -o $@ $<


############################### Dependencies ################################
deps:	$(D_FILES)
.%.d:	%.c
#	@echo "     deps for $<"
	$(CC) $(CPPFLAGS) $(CFLAGS) -MM $< | perl -pe 's#\bdoc.g-c\b##' > $@
include $(D_FILES)

tools/fsvs-chrooter: tools/fsvs-chrooter.c
tools/fsvs-chrooter: interface.h config.h

############################### GCov Usage ################################
ifeq (@ENABLE_GCOV@, 1)
GCOV_FILES := $(C_FILES:%.c=%.c.gcov)
GCOV_SMRY_FILES := $(GCOV_FILES:%.gcov=%.gcov.smry)
GCOV_DATA := $(C_FILES:%.c=%.gcda) $(C_FILES:%.c=%.gcno)

gcov:  $(GCOV_FILES)
	@dev/gcov-summary.pl $(GCOV_SMRY_FILES)

%.c.gcov:	%.c
	@gcov -f $< > $<.gcov.smry
# -b -c

gcov-clean:
	rm -f *.gcov *.gcov.smry *.gcda 2> /dev/null || true

gcov-unused-funcs:
	grep -B1 ":0.00%" *.gcov.smry

.PHONY: gcov gcov-clean
endif


################################ Statistics #################################
diffstat:
	svk diff | diffstat

count:
	@echo "sum of lines:                        "`cat $(C_FILES) $(H_FILES) | wc -l -`
	@echo "sum w/o comments, {, }, empty lines: "`perl -e 'undef $$/; while (<>) { 1 while s#//.*##; 1 while s#/\\*[\\x00-\\xff]*?\\*/##; 1 while s#\s*[{}]\s*##; $$c++ while s#[\r\n]+# #; }; sub END { print $$c,"\n" } ' $(C_FILES) $(H_FILES)` 

revcount: count
	@last_rev=$(shell svk info | grep Revision | cut -d" " -f2) ; echo "number of edits up to revision $$last_rev:" ; for r in `seq 2 $$last_rev` ; do svk diff -r`expr $$r - 1`:$$r /svn2/trunk ; done | perl -pe 's#\ssrc/# #g;' | diffstat

structs: $(DEST)
	@for a in `perl -ne 'print $$1,"\n" if m#^\s*struct\s+(\w+)\s+{\s*$$#' $(C_FILES) $(H_FILES)` ; do printf "%-30s " "struct $$a" ; gdb --batch -ex "printf \"\t%6d\", sizeof(struct $$a)" $(DEST) | cut -f2 -d= ; done 2>&1 | sort -k3 -n

.PHONY:	revcount count diffstat


################################ Testing ####################################
run-tests: $(DEST)
	WAA_CHARS=$(WAA_CHARS) $(MAKE) -C ../tests BINARY=$(shell pwd)/$(DEST) VERBOSE=$(VERBOSE) $(TESTS)
ifeq (@ENABLE_GCOV@, 1)
# I don't know why, but gcov wants to open the .gcda and .gcno
# files Read-Write. I filed a bug report for this.
# If the tests are run as root (which is currently necessary because
# of the devices and device-tests), the normal user who compiled
# the sources will not be allowed to open this files ...
#
# Not all files have code .. and so not all files (of the generated list) 
# will exist; therefore "true".
	-@chmod 777 $(GCOV_DATA) > /dev/null 2>&1
endif

ext-tests: $(DEST)
	dev/permutate-all-tests

.PHONY:	run-tests ext-tests


################################ -- THE END -- ##############################
## vi: ts=8 sw=8
