# Copyright 2003, John Darrington

builddir=build

autoconf_generated_files=${patsubst %, $(builddir)/%, ${shell sed -n -e 's/^AC_OUTPUT(\([^)]*\))/\1/gp'  configure.in} config.h }

source_files:=$(shell aegis -l pf -ter -c $(change) ) $(shell aegis -l cf -ter -c $(change))

# Archive name is the project name, followed by the change/branch
# eg wday.1.1

pkg:=$(shell aesub '$$project' )
br:=$(shell aesub '$$change' )
archive:=$(pkg).$(br).tar.gz

all: start $(archive) deb 

.PHONY: start
start: $(autoconf_generated_files)
	cd $(builddir) && make -f Makefile 
	cd $(builddir) && make -f Makefile wcal/wcal
	cd $(builddir) && PATH=$(PATH):/usr/sbin make -f Makefile install

$(autoconf_generated_files): configure $(patsubst $(builddir)/%,%.in,$(autoconf_generated_files)) version
	@$(RM) $(builddir)/config.status $(builddir)/config.log
	@$(RM) $@
	mkdir -p $(builddir)
	cd $(builddir) && $(SHELL) ../configure --prefix=`pwd`


archive_components:=$(filter-out website/%,$(source_files)) configure version $(builddir)/doc/wday.info changelog

$(archive): $(archive_components)
	$(RM) $@
	$(RM) $(pkg).$(br)
	ln -s . $(pkg).$(br)
	tar -czhf $@ $(patsubst %,$(pkg).$(br)/%,$(archive_components) )


configure: configure.in aclocal.m4
	@$(RM) $@ 
	autoconf


	
package:=${shell aesub '$${p trunk_name}' }
version:=${shell aesub '$$project'}.${shell aesub '$$change' }



version: make-version

.PHONY: make-version
make-version:
	@$(RM) $@
	echo PACKAGE=$(package) >> $@
	echo VERSION=$(version) >> $@
	@if ! test -e version  || ! cmp $@ version ; then \
	 $(RM) version ;\
	 cp $@ version ;\
	else \
	 $(RM) $@ ;\
	fi


# The changelog rule 

# This rule actually produces the change log for the current branch.
# This is NOT the way Peter Miller does aegis releases.
# 
.PHONY: changelog
changelog:
	@$(RM) $@
	aereport cl  > $@



.PHONY: deb
deb: deb-build/debian/changelog deb-build/debian/copyright
	@$(RM) wday_*
	$(RM) deb-build/config.log
	( cd deb-build ; dpkg-buildpackage -rfakeroot -us -uc )

.PHONY: deb-build/debian/changelog
deb-build/debian/changelog:
	@$(RM) $@
	aereport -unf -f deb_changelog.rpt  -c $(change) -nph wday.1  \
	| sed -e 's/^\*/\ \ */' -e 's/^--/\ --/' > $@


.PHONY: deb-build/debian/copyright
deb-build/debian/copyright:
	@$(RM) $@
	aereport -f deb_copyright.rpt -unf > $@

