#
# @(#)$Id: Makefile,v 1.1 2008/02/05 16:58:12 davidg Exp $
#

.PHONY: release minorversion majorversion tagversion
.PHONY: pack rpm pkg prep clean dist userdoc apidoc check

_os        := $(shell uname -s)
_date      := $(shell date '+%d/%m/%y %H:%M')

#######################################################
#
# General settings
#
#######################################################

####################################################################
# Linux specific settings
####################################################################
#
#

_rpmbuild  := $(shell [ -x /usr/bin/rpmbuild ] && echo rpmbuild || echo rpm)
_builddir  := $(shell rpm --eval %_builddir 2>/dev/null)
_specdir   := $(shell rpm --eval %_specdir 2>/dev/null)
_sourcedir := $(shell rpm --eval %_sourcedir 2>/dev/null)
_srcrpmdir := $(shell rpm --eval %_srcrpmdir 2>/dev/null)
_rpmdir    := $(shell rpm --eval %_rpmdir 2>/dev/null)


include    config.mk
_module    = $(NAME)
_vpfx      = $(NAME)_
_vtag      = $(subst -,_,$(subst .,_,$(_vpfx)$(VERSION)$(BRANCH)))
_prodname  = $(NAME)-$(VERSION)$(BRANCH)
_prodtar   = $(_prodname).src.tgz


###################################################################
# Create linux specific build directories
###################################################################

envdir:: $(_srcrpmdir) $(_rpmdir)
$(_srcrpmdir):
	mkdir -p $(_srcrpmdir)
$(_rpmdir):
	mkdir -p $(_rpmdir)

####################################################################
# Create new release/version/majorversion
####################################################################
#
# These targets update the X.Y.Z components of the version in the
# config file and then commit the current version, tagging all
# files with the release number from the config file. Also
# wires the current date and time into the config file. Also
# adds an entry to the ChangeLog.


release: config.tmp
	@echo 'increasing release number x.x.<X> and timestamp..'
	@perl <config.tmp >config.mk -e 'while (<>) \
	  { s/^(VERSION=\d+\.\d+\.)(\d+)(.*)$$/$$1.($$2+1).$$3/e; \
	    s/^(RELEASE=)(\d+)(.*)$$/$$1."1".$$3/e; print; }'
	@$(MAKE) tagversion

minorversion: config.tmp
	@echo 'increasing minorversion number x.<X>.x and timestamp..'
	@perl <config.tmp >config.mk -e 'while (<>) \
	  { s/^(VERSION=\d+\.)(\d+)(\..*)$$/$$1.($$2+1).".0"/e; \
	    s/^(RELEASE=)(\d+)(.*)$$/$$1."1".$$3/e; print; }'
	@$(MAKE) tagversion

majorversion: config.tmp
	@echo 'increasing majorversion number <X>.x.x and timestamp..'
	@perl <config.tmp >config.mk -e 'while (<>) \
	  { s/^(VERSION=)(\d+)(\..*)$$/$$1.($$2+1).".0.0"/e; \
	    s/^(RELEASE=)(\d+)(.*)$$/$$1."1".$$3/e; print; }'
	@$(MAKE) tagversion

config.tmp: config.mk
	@cvs update .
	@sed <config.mk >config.tmp '/^DATE=/d'
	@echo 'DATE=$(_date)' >>config.tmp
	@cp config.mk config.mk~

tagversion:
	@echo cvs tagging new release: $(VERSION)
	@rm -f config.tmp
	@echo Enter ChangeLog comment or hit return key:
	@perl -e '$$cl=<STDIN>; chomp($$cl); $$date=`date +%Y-%m-%d`; \
	chomp($$date); \
	$$login=getlogin(); \
	$$logstr="<unknown>"; \
	$$logstr=(getpwnam($$login))[6] if defined $$login; \
	print $$date."  $$logstr\n\n"; \
	print "	* Release: $(VERSION)\n"; \
	print "	- ".$$cl."\n" unless ($$cl eq ""); \
	print "\n"; ' >ChangeLog.tmp
	@if [ ! -r ChangeLog ] ; then touch ChangeLog ; cvs add ChangeLog ; fi
	@cat ChangeLog >>ChangeLog.tmp
	@mv ChangeLog.tmp ChangeLog
	@cvs commit -m "Release: $(VERSION)"
	@cvs tag -F -c `echo $(_vtag) |sed -e s/\\\./_/g -e s/-/_/g`
	@cvs tag -F -c latest

####################################################################
# Build distributions in tar & RPM/PKG format
####################################################################
#
# These targets make distribution tar files and PKGs/RPMs from the
# CVS repository, using the version declared in the config file.
# The checked-out files in the current directory are neither
# modified, nor read (apart from the config file).
#
# "pack" and "rpm" built from the sources in the current directory.
# 
# For "deb", remember to run it under fakeroot!
#
#
# The prep:: target allows the including makefile to specify actions
# required to process the files at distribution pack time.
#
# These targets make distribution tar files and PKGs/RPMs based on the
# checked-out sources in the current directory. No connection to
# the CVS repository is required, and the files do not need to be
# committed. The RPM version numbers are generated from the
# version number specified in the config file.
#

debtgz:
	@rm -rf /tmp/pkging/$(_prodname)
	@mkdir -p /tmp/pkging/$(_prodname)
	@find . -path '*CVS*' -prune -o -path '*.svn*' -prune -o -type f -print >files.tmp
	@tar cfT - files.tmp |(cd /tmp/pkging/$(_prodname) ; tar xf -)
	@rm -f files.tmp
	@cd /tmp/pkging/$(_prodname) ; \
	    sed <config.mk >config.tmp \
	        -e 's%^RELEASE=\(.*\)%RELEASE=\1%' \
	        -e 's%^VERSION=.*%&$(BRANCH)%' ; \
	    mv config.tmp config.mk ; \
	    echo 'TARFILE=$(_prodtar)' >>config.mk ; \
	    echo 'PROD=\#' >>config.mk ; \
	    $(MAKE) config.sh ;\
	    $(MAKE) prep ;\
	    test -n "$(_test_dep)" && rm -f $(_test_dep) ;\
	    rm -f config.sh
	@cd /tmp/pkging ; fakeroot tar czf /tmp/pkging/$(_prodtar) $(_prodname)
	@cp -p /tmp/pkging/$(_prodtar) .

deb:	control debtgz
	@rm -rf /tmp/pkging/$(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE) 2>/dev/null
	@mkdir -p /tmp/pkging/$(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE)
	@make PREFIX=/tmp/pkging/$(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE) install
	@mkdir -p /tmp/pkging/$(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE)/DEBIAN
	@cp control /tmp/pkging/$(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE)/DEBIAN/
	@cd /tmp/pkging/; \
	  fakeroot tar zcf $(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE).tgz \
	  $(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE)
	@mv /tmp/pkging/$(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE).tgz .
	@fakeroot tar zxf $(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE).tgz 
	@fakeroot dpkg-deb --build $(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE)
	@rm -rf $(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE)

pack:   envdir
	@echo packing distribution...
	@rm -rf $(_builddir)/$(_prodname)
	@rm -f $(_sourcedir)/$(_prodtar)
	@mkdir -p $(_builddir)/$(_prodname)
	@find . -path '*CVS*' -prune -o -path '*.svn*' -prune -o -type f -print >files.tmp
	@tar cfT - files.tmp |(cd $(_builddir)/$(_prodname) ; tar xf -)
	@rm -f files.tmp
	@[ ! -f specfile.spec -a -f specfile.cin ] \
	     && cp specfile.cin \
	        $(_builddir)/$(_prodname)/specfile.spec \
	     || : ;
	@cp cl2rpm \
	        $(_builddir)/$(_prodname)/ > /dev/null 2>&1 \
	     || : ;
	@cd $(_builddir)/$(_prodname) ; \
	    sed <config.mk >config.tmp \
	        -e 's%^RELEASE=\(.*\)%RELEASE=\1%' \
	        -e 's%^VERSION=.*%&$(BRANCH)%' ; \
	    mv config.tmp config.mk ; \
	    echo 'TARFILE=$(_prodtar)' >>config.mk ; \
	    echo 'PROD=\#' >>config.mk ; \
	    $(MAKE) config.sh ;\
	    $(MAKE) prep ;\
	    test -n "$(_test_dep)" && rm -f $(_test_dep) ;\
	    rm -f config.sh
	@cd $(_builddir) ; tar czf $(_sourcedir)/$(_prodtar) $(_prodname)
	@cp -p $(_sourcedir)/$(_prodtar) .

spec: pack
	@echo generating specfile.spec ...
	@cd $(_builddir)/$(_prodname) ; \
	    $(MAKE) config.sh ;\
	    ./config.sh <specfile.spec >$(_specdir)/$(_prodname).spec
	@test -f ChangeLog && \
	    cl2rpm ChangeLog \
	                        >>$(_specdir)/$(_prodname).spec 2>/dev/null || true

rpm:    spec
	@echo building rpm ...
	@cd $(_specdir) ; $(_rpmbuild) -ba $(_specdir)/$(_prodname).spec
	@if [ -f $(_srcrpmdir)/$(_prodname)-$(RELEASE).src.rpm ] ; then \
	  mv $(_srcrpmdir)/$(_prodname)-$(RELEASE).src.rpm . ;  \
	fi
	@if [ -f $(_rpmdir)/i386/$(_prodname)-$(RELEASE).i386.rpm ] ; then \
	  mv $(_rpmdir)/i386/$(_prodname)-$(RELEASE).i386.rpm . ;  \
	fi
	@if [ -f $(_rpmdir)/i686/$(_prodname)-$(RELEASE).i686.rpm ] ; then \
	  mv $(_rpmdir)/i686/$(_prodname)-$(RELEASE).i686.rpm . ;  \
	fi
	@if [ -f $(_rpmdir)/x86_64/$(_prodname)-$(RELEASE).x86_64.rpm ] ; then \
	  mv $(_rpmdir)/x86_64/$(_prodname)-$(RELEASE).x86_64.rpm . ;  \
	fi
	@if [ -f $(_rpmdir)/athlon/$(_prodname)-$(RELEASE).athlon.rpm ] ; then \
	  mv $(_rpmdir)/athlon/$(_prodname)-$(RELEASE).athlon.rpm . ;  \
	fi
	@if [ -f $(_rpmdir)/ia64/$(_prodname)-$(RELEASE).ia64.rpm ] ; then \
	  mv $(_rpmdir)/ia64/$(_prodname)-$(RELEASE).ia64.rpm . ;  \
	fi
	@if [ -f $(_rpmdir)/noarch/$(_prodname)-$(RELEASE).noarch.rpm ] ; then \
	  mv $(_rpmdir)/noarch/$(_prodname)-$(RELEASE).noarch.rpm . ;  \
	fi

pkg:    spec
	@echo building pkg ...
	@cd $(_specdir) ; $(_pkgbuild) -f $(_specdir)/$(_prodname).spec

prep::

#####################################################################
# Create substitution script
####################################################################
#
# This target reads the config file and creates a shell script which
# can substitute variables of the form @VAR@ for all config
# variables VAR. The messing around with the temporary makefile is
# to ensure that any recursive or external references in the
# variable values are evaluated by "make" in the same context as
# when the config file is included in the makefile.

config.sh: Makefile $(_test_dep)
	@cp /dev/null makefile.tmp
	@echo include config.mk >>makefile.tmp
	@echo dumpvars: >>makefile.tmp
	@cat config.mk | \
	 perl >>makefile.tmp -e 'my $$fmt = "\t\@echo \"-e \\\"s\@%s\@\$$(%s)g\\\" \\\\\"" ; while (<>) { $$v{$$1}=1 if /^([A-Za-z0-9_]+)\s*:?=.*$$/; } map { printf "$$fmt >>config.sh\n", $$_, $$_; } sort(keys(%v)); print "\n"; '
	@echo '#!/bin/sh' >config.sh
	@echo 'sed \' >>config.sh
	@$(MAKE) -f makefile.tmp dumpvars >/dev/null
	@echo ' -e "s/\@MSG\@/ ** Generated file : do not edit **/"'>>config.sh
	@chmod oug+x config.sh
	@rm makefile.tmp

####################################################################
# Configure
####################################################################

%:: %.cin config.sh
	@echo configuring $@ ...
	@rm -f $@ ; cp $< $@
	@./config.sh <$< >$@ ; chmod oug-w $@

%.$(MANSECT):: %.$(MANSECT).man.cin
	@echo creating $@ ...
	@./config.sh <$< >$@ ; chmod oug-w $@

configure: $(shell find . -name \*\.cin 2>/dev/null | sed -e 's/.cin//' || echo)

default-install:
	@[ -f MAINTAINER ] && install -D -m 0644 MAINTAINER $(PREFIX)$(DOCDIR)/MAINTAINER || :
	@[ -f CHANGES ] && install -D -m 0644 CHANGES $(PREFIX)$(DOCDIR)/CHANGES || :
	@[ -f ChangeLog ] && install -D -m 0644 ChangeLog $(PREFIX)$(DOCDIR)/ChangeLog || :
	@[ -f README ] && install -D -m 0644 README $(PREFIX)$(DOCDIR)/README || :
	@[ -f INSTALL ] && install -D -m 0644 INSTALL $(PREFIX)$(DOCDIR)/INSTALL || :
	@[ -f LICENSE ] && install -D -m 0644 LICENSE $(PREFIX)$(DOCDIR)/LICENSE || :
	@[ -d doc ] && cd doc && for i in * ; do \
	        test `expr match $$i .\*\.cin\$$` -eq 0 && install -m 0444 $$i $(PREFIX)/$(DOCDIR)/$$i ; \
	done || :


####################################################################
# Install
####################################################################

install: configure default-install
	@echo installing in $(PREFIX) ...
	#
	# MODIFY location of installed scripts/binaries here
	#
	@mkdir -p $(PREFIX)/$(DOCDIR)
	@mkdir -p $(PREFIX)/$(BINDIR)
	install -m 0755 mkgroup-sshlpk $(PREFIX)/$(BINDIR)/mkgroup-sshlpk
	install -m 0755 mkgroup-dnlist $(PREFIX)/$(BINDIR)/mkgroup-dnlist
	install -m 0755 sshlpk-list $(PREFIX)/$(BINDIR)/sshlpk-list
	install -m 0755 sshlpk-akfgen $(PREFIX)/$(BINDIR)/sshlpk-akfgen
	install -m 0755 mkhomedir-sshlpk $(PREFIX)/$(BINDIR)/mkhomedir-sshlpk
	install -m 0644 openssh-lpk.schema $(PREFIX)/$(DOCDIR)/openssh-lpk.schema
	install -m 0644 README.txt $(PREFIX)/$(DOCDIR)/README.txt


####################################################################

filelist: install
	@find $(PREFIX) -type d | sed -e 's^$(PREFIX)g' -e 's/^/%dir /' >> $(FILELIST)
	@find $(PREFIX) ! -type d -a ! -name ".filelist" | sed -e 's^$(PREFIX)g' >> $(FILELIST)
	@echo Files found in distribution listing $(FILELIST)
	@cat $(FILELIST)


clean::
	@echo cleaning common files ...
	@rm -f config.sh config.tex control
	@rm -f `find . -name '*~'`
	@rm -f `find . -name '*#'`
	@rm -f `find . -name '*.tmp'`
	@rm -f $(_prodtar)
	@rm -f `find . -name '$(_prodname)-$(RELEASE).*.rpm'`
	@rm -f $(NAME)_$(VERSION)$(BRANCH)-$(RELEASE).tgz
	@rm -f $(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE).tgz
	@rm -f $(NAME)_$(VERSION)$(BRANCH)-$(DEBRELEASE).deb
	@rm -rf $(NAME)_$(VERSION)$(BRANCH)-$(RELEASE)

dist: pack

ndpf: rpm
	scp -p $(_prodname)-$(RELEASE).noarch.rpm login.nikhef.nl:/public/www/grid/ndpf/files/local/packages/
	ssh login.nikhef.nl "cd /public/www/grid/ndpf/files/local/packages/ && createrepo -s sha256 ."

ndpfdeb: debian
	scp -p $(NAME)_$(VERSION)-$(RELEASE)_all.deb login.nikhef.nl:/public/www/grid/ndpf/files/local/deb/pool/main/
	ssh login.nikhef.nl "cd /public/www/grid/ndpf/files/local/deb && [ -f regen.sh ] && ./regen.sh"

