# Makefile for drbd
#
# This file is part of drbd by Philipp Reisner
#
# drbd is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# drbd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with drbd; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#

include Makefile.vars
REL_VERSION = $(word 3, $(shell grep REL_VERSION drbd_config.h))

all: INSTALL.html
	@ for i in $(SUBDIRS); do $(MAKE) -C $$i all || test $$i = documentation || exit 1 ; done
	@ echo -e "\n\tBuild successful."

install:
	@ for i in $(SUBDIRS); do $(MAKE) -C $$i install || test $$i = documentation || exit 1 ; done

clean:
	@ set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i clean; done
	rm -f *~ .filelist INSTALL.html INSTALL.pod pod2htm*
	rm -rf dist

uninstall:
	@ set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i uninstall; done

check_changelogs_up2date:
	@ up2date=true; rver_re=$(REL_VERSION); rver_re=$${rver_re//./\\.}; \
	in_changelog=$$(sed -n -e '0,/^%changelog/d' \
	                     -e '/^- *drbd ('"$$rver_re"'-/p' \
	                     -e '/^\*.* \['"$$rver_re"'-/p' < drbd.spec.in) ; \
	if test -z "$$in_changelog" ; \
	then \
	   echo "You need to update the %changelog in drbd.spec.in"; \
	   up2date=false; fi; \
	if ! grep "^drbd ($$rver_re-" >/dev/null 2>&1 debian/changelog; \
	then \
	   echo "You need to update debian/changelog"; \
	   up2date=false; fi ; \
	if ! grep "^drbd_$$rver_re-" >/dev/null 2>&1 debian/files; \
	then \
	   echo "You need to update debian/files"; \
	   up2date=false; fi ; \
	$$up2date

update.filelist:
	cvs status | grep -o "/drbd/drbd/[^,]*" | \
	sed "s/\/drbd\/drbd/drbd-$(REL_VERSION)/" > .filelist
	find documentation -name "*.[58]" -o -name "*.html" | \
	sed "s/^/drbd-$(REL_VERSION)\//" >> .filelist
	echo drbd-$(REL_VERSION)/.filelist >> .filelist

.filelist:
	@ echo -e "\nto create the filelist:   make update.filelist\nyou need cvs access, though.\n"
	@ false

tgz: .filelist
ifeq ($(FORCE),)
	$(MAKE) -C documentation
endif
	cd scripts; ln -sf drbd datadisk
	rm -f drbd-$(REL_VERSION)
	ln -s . drbd-$(REL_VERSION)
	tar --owner=0 --group=0 -czf drbd-$(REL_VERSION).tar.gz -T .filelist
	rm drbd-$(REL_VERSION)

ifeq ($(FORCE),)
tgz: check_changelogs_up2date
endif

KDIR := $(shell echo /lib/modules/`uname -r`/build)
KVER := $(shell \
	echo -e "\#include <linux/version.h>\ndrbd_kernel_release UTS_RELEASE" | \
        gcc -nostdinc -E -P -I$(KDIR)/include - 2>&1 | \
        sed -ne 's/^drbd_kernel_release "\(.*\)".*/\1/p')

# maybe dist/RPMS/$(ARCH) instead of i386 ?
rpm: tgz
	mkdir -p dist/BUILD \
	         dist/RPMS/i386 \
	         dist/SPECS \
	         dist/SOURCES \
	         dist/TMP \
	         dist/install \
	         dist/SRPMS
	[ -h dist/SOURCES/drbd-$(REL_VERSION).tar.gz ] || \
	  $(LN_S) $(PWD)/drbd-$(REL_VERSION).tar.gz \
	          $(PWD)/dist/SOURCES/drbd-$(REL_VERSION).tar.gz
	if test drbd.spec.in -nt dist/SPECS/drbd.spec ; then \
	   sed -e "s/^\(Version:\).*/\1 $(REL_VERSION)/;" \
	       -e "s/^\(Packager:\).*/\1 $(USER)@$(HOSTNAME)/;" < drbd.spec.in \
	   > dist/SPECS/drbd.spec ; \
	fi
	$(RPMBUILD) -ba ${RPMOPTIONS}\
	    --define "_topdir $(PWD)/dist" \
	    --define "buildroot $(PWD)/dist/install" \
	    --define "kernelversion $(KVER)" \
	    --define "kdir $(KDIR)" \
	    $(PWD)/dist/SPECS/drbd.spec

# the INSTALL file is writen in lge-markup, which is basically
# wiki style plus some "conventions" :)
# why don't I write in or convert to HTML directly?
# editing INSTALL feels more natural this way ...

INSTALL.html: INSTALL.pod
	-pod2html --title "Howto Build and Install DRBD" \
		< INSTALL.pod > INSTALL.html ; rm -f pod2htm*
#	-w3m -T text/html -dump < INSTALL.html > INSTALL.txt

INSTALL.pod: INSTALL
	-@perl -pe 'BEGIN { print "=pod\n\n"; };                   \
	 	s/^= +(.*?) +=$$/=head1 $$1/;                     \
	 	s/^== +(.*?) +==$$/=head2 $$1/;                   \
	 	if(s/^ +([^#]*)$$/$$1/ or /^\S/) {                \
			s/(Note:)/B<$$1>/g;                       \
			s/"([^"]+)"/C<$$1>/g;                     \
	 		s,((^|[. ])/(\S|`[^`]*`)+),C<$$1>,g;\
	 	}' \
	 	< INSTALL > INSTALL.pod
