BASEVERSION := $(shell cat VERSION)

UNIXDATE := $(shell date +%s)
DATEREL := .$(shell date +"%Y%m%d%H%M%S")

VERSION := $(BASEVERSION)$(DATEREL)

all:
	echo Please specify something to make!

tarball: 
	if [ ! -d dists ]; then mkdir dists; fi
	for d in `cat dist/manifest.dirs` ; \
	   do install -d -m 755 photopub-$(VERSION)/$$d ; done
	for d in `cat dist/manifest.files` ; \
	   do install -m 644 $$d photopub-$(VERSION)/$$d ; done
	for d in `cat dist/manifest.scripts` ; \
	   do install -m 755 $$d photopub-$(VERSION)/$$d ; done
	for d in `find dist/templates -path '*CVS' -prune -o -type d -print` ; \
	   do install -d -m 755 photopub-$(VERSION)/$$d ; done
	for d in `find dist/templates -path '*CVS' -prune -o -type f -print` ; \
	   do install -m 644 $$d photopub-$(VERSION)/$$d ; done

	rm -f CHANGELOG.tmp
	([ "$(DATEREL)" != "" ] && echo "%$(VERSION) $(UNIXDATE)" >> CHANGELOG.tmp) || true
	cat CHANGELOG >> CHANGELOG.tmp

	echo "%define version $(VERSION)" > photopub-$(VERSION)/photopub.spec
	cat dist/redhat/photopub.spec >> photopub-$(VERSION)/photopub.spec
	echo "%changelog" >> photopub-$(VERSION)/photopub.spec
	dist/makechangelogs.pl redhat < CHANGELOG.tmp >> photopub-$(VERSION)/photopub.spec
	chmod 644 photopub-$(VERSION)/photopub.spec

	install -d -m 755 photopub-$(VERSION)/debian
	for d in `cat dist/debian/manifest.files`; \
	   do install -m 644 dist/debian/$$d photopub-$(VERSION)/debian/$$d ; done
	for d in `cat dist/debian/manifest.scripts`; \
	   do install -m 755 dist/debian/$$d photopub-$(VERSION)/debian/$$d ; done
	dist/makechangelogs.pl debian < CHANGELOG.tmp > photopub-$(VERSION)/debian/changelog
	darcs changes > photopub-$(VERSION)/doc/darcs-changelog
	tar -cvf - photopub-$(VERSION) | gzip > \
	   dists/photopub-$(VERSION).tar.gz
	chmod 644 dists/photopub-$(VERSION).tar.gz
	rm -rf photopub-$(VERSION)/

	dist/makechangelogs.pl html < CHANGELOG.tmp > dists/photopub-$(VERSION)-changelog.html
	chmod 644 dists/photopub-$(VERSION)-changelog.html

	rm -f CHANGELOG.tmp


redhat: tarball
	for d in redhat/BUILD redhat/SOURCES redhat/SPECS redhat/RPMS/noarch \
	         redhat/SRPMS ; \
	   do mkdir -p $$d ; done
	echo "%_topdir `pwd`/redhat" > dist/redhat/rpmmacros
	echo "%_build_name_fmt %%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm" >> dist/redhat/rpmmacros
	umask 022 && \
	   rpm --rcfile dist/redhat/rpmrc -ta dists/photopub-$(VERSION).tar.gz
	cp redhat/RPMS/noarch/photopub-$(VERSION)-1.noarch.rpm \
	   redhat/SRPMS/photopub-$(VERSION)-1.src.rpm dists/
	rm -rf redhat/
	(which rpmlint > /dev/null && \
	   rpmlint dists/photopub-$(VERSION)-1.noarch.rpm \
	           dists/photopub-$(VERSION)-1.src.rpm) \
          || true

debian: tarball
	cd dists && \
	   tar -xzvf photopub-$(VERSION).tar.gz && \
	   cd photopub-$(VERSION) && dpkg-buildpackage -b -us -uc -rfakeroot
	rm -rf dists/photopub-$(VERSION)
	(which lintian > /dev/null && \
	   lintian -I dists/photopub_$(VERSION)_all.deb) \
	  || true

packages: redhat debian

UPLOADROOT="urchin.earth.li:/srv/www/urchin.earth.li/html/projects/photopub/releases/main/"
DOWNLOADROOT="http://urchin.earth.li/projects/photopub/releases/main/"

upload: packages
	cd dists && scp photopub-$(VERSION).tar.gz photopub_$(VERSION)_all.deb photopub-$(VERSION)-1.noarch.rpm photopub-$(VERSION)-changelog.html $(UPLOADROOT)

release:
	[ "$(VER)" != "" ]
	rm -f CHANGELOG.tmp
	echo "%$(VER) $(UNIXDATE)" > CHANGELOG.tmp
	cat CHANGELOG >> CHANGELOG.tmp
	mv CHANGELOG.tmp CHANGELOG
	echo $(VER) > VERSION
	make upload DATEREL=
	make email VER=$(VER)

email:
	[ "$(VER)" != "" ]
	echo "Changes in this version:"  > EMAIL.tmp
	dist/makechangelogs.pl latest  < CHANGELOG >> EMAIL.tmp
	echo >> EMAIL.tmp
	echo "Full changelog:" >> EMAIL.tmp
	echo "$(DOWNLOADROOT)photopub-$(VER)-changelog.html" >> EMAIL.tmp
	echo >> EMAIL.tmp
	echo "Download URLs:" >> EMAIL.tmp
	echo "$(DOWNLOADROOT)photopub-$(VER).tar.gz" >> EMAIL.tmp
	echo "$(DOWNLOADROOT)photopub_$(VER)_all.deb" >> EMAIL.tmp
	echo "$(DOWNLOADROOT)photopub-$(VER)-1.noarch.rpm" >> EMAIL.tmp
	$(EDITOR) EMAIL.tmp
	mail -s"$(VER) released" photopub-announce@lists.urchin.earth.li < EMAIL.tmp
	rm EMAIL.tmp
