# Makefile written for the Debian package.
# Based on the Remi Perrot's one, done for 
# the 2.18 release.

# Destination paths
# For a better maintenance, we'll create by hand each
# bugzilla's sub directories.
DESTDIR		= 
BUGZILLA_PERLDIR= $(DESTDIR)/usr/share/perl5
BUGZILLA_CONFIG = $(DESTDIR)/etc/bugzilla3
BUGZILLA_CGIDIR = $(DESTDIR)/usr/lib/cgi-bin/bugzilla3
BUGZILLA_ROOT	= $(DESTDIR)/usr/share/bugzilla3
BUGZILLA_WWW	= $(BUGZILLA_ROOT)/web
BUGZILLA_LIBDIR = $(BUGZILLA_ROOT)
BUGZILLA_DATADIR = $(DESTDIR)/var/lib/bugzilla3
BUGZILLA_CONTRIB= $(BUGZILLA_ROOT)/contrib

PKGVER := $(shell dpkg-parsechangelog |grep Version |sed -e 's,Version: \([^-]\+\).*,\1,g')
ORIGTARGZ := bugzilla_$(PKGVER).orig.tar.gz

# Source paths
cgi_files	= *.cgi
lib_files	= *.pm 
pl_files	= runtests.pl 
lib_perlscripts	= checksetup.pl collectstats.pl email_in.pl importxml.pl mod_perl.pl testserver.pl whineatnews.pl whine.pl
lib_dir		= Bugzilla
static_files	= *.dtd *.txt 

# Where to find the sources.
BUGZILLA_TAR    := $(shell ls -1 bugzilla-?.?.tar.gz bugzilla-?.?.?.tar.gz| head -n 1)
BUGZILLA_TARDIR := $(subst .tar.gz,,$(BUGZILLA_TAR))
BUGZILLA_SRCDIR = bugzilla-srcdir

BUGZILLA_DE = $(shell ls -1 germzilla-*.tar.gz | head -n 1)
BUGZILLA_BE = $(shell ls -1 bugzilla-be-*.tar.gz | head -n 1)
#BUGZILLA_BG = $(shell ls -1 bugzilla-bg-*.tar.gz | head -n 1)
BUGZILLA_FR = $(shell ls -1 bugzilla-*.fr.tar.gz | head -n 1)
BUGZILLA_RU = $(shell ls -1 bugzilla-*-ru-*.tar.gz | head -n 1)
BUGZILLA_CS = $(shell ls -1 bugzilla.cs.*.tar.gz | head -n 1)
BUGZILLA_JA = $(shell ls -1 Bugzilla-ja-*.tar.bz2 | head -n 1)
BUGZILLA_ZH = $(shell ls -1 bugzilla.zh-TW.*.tar.gz | head -n 1)

INSTALL = /usr/bin/install

extractsrc:	$(BUGZILLA_SRCDIR)
$(BUGZILLA_SRCDIR):	$(BUGZILLA_TAR)
	tar -xzf $(BUGZILLA_TAR)
	test -d $(BUGZILLA_TARDIR)
	tar -C $(BUGZILLA_TARDIR)/template -xzf $(BUGZILLA_DE)
	test -d $(BUGZILLA_TARDIR)/template/de
	find $(BUGZILLA_TARDIR)/template/de -name gzversion.html.tmpl -exec cp /dev/null {} \;
	tar -C $(BUGZILLA_TARDIR) -xzf $(BUGZILLA_FR)
	test -d $(BUGZILLA_TARDIR)/template/fr
	: # tar -C $(BUGZILLA_TARDIR) -xzf $(BUGZILLA_BG)
	: # test -d $(BUGZILLA_TARDIR)/template/bg
	tar -C $(BUGZILLA_TARDIR) --transform "s/bugzilla[^\/]\+\///g" -xzf $(BUGZILLA_BE)
	test -d $(BUGZILLA_TARDIR)/template/be
	tar -C $(BUGZILLA_TARDIR) -xzf $(BUGZILLA_RU)
	test -d $(BUGZILLA_TARDIR)/template/ru
	tar -C $(BUGZILLA_TARDIR)/template --transform "s/template/cs/g" -xzf $(BUGZILLA_CS)
	test -d $(BUGZILLA_TARDIR)/template/cs
	$(INSTALL) -d $(BUGZILLA_TARDIR)/template/ja
	tar -C $(BUGZILLA_TARDIR)/template/ja -xjf $(BUGZILLA_JA)
	test -d $(BUGZILLA_TARDIR)/template/ja/default
	tar -C $(BUGZILLA_TARDIR) -xzf $(BUGZILLA_ZH)
	test -d $(BUGZILLA_TARDIR)/template/zh-TW
	find $(BUGZILLA_TARDIR)/template -name "*.tmpl" -exec chmod a-x {} \;
	find $(BUGZILLA_TARDIR)/template -name ".cvsignore" -or -name "CVS" -or -name ".svn" -exec rm {} \;
	mv $(BUGZILLA_TARDIR) $(BUGZILLA_SRCDIR)

install: install_static_dirs install_static_files install_lib_files \
	 install_cgi install_template install_contrib


install_fr_FIXME:	extractsrc
	@echo "Extracting french templates"
	$(INSTALL) -d $(BUGZILLA_ROOT)/template
	tar zxf l10n/fr.tar.gz -C $(BUGZILLA_ROOT)/template 


install_contrib:	extractsrc
	$(INSTALL) -d $(BUGZILLA_CONTRIB)
	$(INSTALL) -m 0755 $(BUGZILLA_SRCDIR)/contrib/recode.pl $(BUGZILLA_CONTRIB)
#	$(INSTALL) -m 0755 $(BUGZILLA_SRCDIR)/contrib/*.pl $(BUGZILLA_CONTRIB)
#	$(INSTALL) -m 0644 $(BUGZILLA_SRCDIR)/contrib/*.pm $(BUGZILLA_CONTRIB)
#	$(INSTALL) -m 0755 $(BUGZILLA_SRCDIR)/contrib/*.py $(BUGZILLA_CONTRIB)
#	$(INSTALL) -m 0644 $(BUGZILLA_SRCDIR)/contrib/README* $(BUGZILLA_CONTRIB)
#	$(INSTALL) -m 0644 $(BUGZILLA_SRCDIR)/contrib/*.html $(BUGZILLA_CONTRIB)
#	$(INSTALL) -m 0755 $(BUGZILLA_SRCDIR)/contrib/*.sh $(BUGZILLA_CONTRIB)
#	$(INSTALL) -m 0755 $(BUGZILLA_SRCDIR)/contrib/*.rb $(BUGZILLA_CONTRIB)
#	$(INSTALL) -d $(BUGZILLA_CONTRIB)/bugzilla-submit
#	cp $(BUGZILLA_SRCDIR)/contrib/bugzilla-submit/* $(BUGZILLA_CONTRIB)/bugzilla-submit
#	$(INSTALL) -d $(BUGZILLA_CONTRIB)/cmdline
#	cp $(BUGZILLA_SRCDIR)/contrib/cmdline/* $(BUGZILLA_CONTRIB)/cmdline
#	$(INSTALL) -d $(BUGZILLA_CONTRIB)/gnatsparse
#	$(INSTALL) -m 0644 $(BUGZILLA_SRCDIR)/contrib/gnatsparse/README $(BUGZILLA_CONTRIB)/gnatsparse
#	$(INSTALL) -m 0755 $(BUGZILLA_SRCDIR)/contrib/gnatsparse/*.py $(BUGZILLA_CONTRIB)/gnatsparse


install_static_dirs:	extractsrc
	@cd $(BUGZILLA_SRCDIR) && for this_dir in `find skins -type d` ; do \
		install -d -m 0755 -o root -g root $(BUGZILLA_WWW)/$$this_dir ;\
	done
	@cd $(BUGZILLA_SRCDIR) && for this_dir in `find js -type d` ; do \
		install -d -m 0755 -o root -g root $(BUGZILLA_WWW)/$$this_dir ;\
	done
	@cd $(BUGZILLA_SRCDIR) && for this_dir in `find data -type d` duplicates webdot; do \
		install -d -m 0755 -o root -g root $(BUGZILLA_DATADIR)/$$this_dir ;\
	done


install_template:	extractsrc
	@cd $(BUGZILLA_SRCDIR) && for this_dir in `find template -type d` ; do \
		install -d -m 0755 -o root -g root $(BUGZILLA_DATADIR)/$$this_dir ;\
	done
	@cd $(BUGZILLA_SRCDIR) && for this_file in `find template -type f` ; do \
		install -m 0644 -o root -g root $$this_file $(BUGZILLA_DATADIR)/`dirname $$this_file` ;\
	done


install_static_files:	extractsrc
	$(INSTALL) -d -m 0755 -o root -g root $(BUGZILLA_DATADIR)
	cd $(BUGZILLA_SRCDIR) && $(INSTALL) -m 0644 -o root -g root $(static_files) $(BUGZILLA_WWW)
	@cd $(BUGZILLA_SRCDIR) && for this_file in `find skins -type f` ; do \
		install -m 0644 -o root -g root $$this_file $(BUGZILLA_WWW)/`dirname $$this_file` ;\
	done
	$(INSTALL) -d -m 0755 -o root -g root $(BUGZILLA_WWW)
	$(INSTALL) -m 0644 -o root -g root $(BUGZILLA_SRCDIR)/images/padlock.png $(BUGZILLA_WWW)
	@cd $(BUGZILLA_SRCDIR) && for this_file in `find js -type f` ; do \
		install -m 0644 -o root -g root $$this_file $(BUGZILLA_WWW)/`dirname $$this_file` ;\
	done
	# The tricky point here, is to leave "params" alone, that's a conffile now 
	# We'll handle it by hand in postinst.
	@cd $(BUGZILLA_SRCDIR) && for this_file in `find data -type f -name '!params'` ; do \
		install -m 0644 -o root -g root $$this_file $(BUGZILLA_DATADIR)/`dirname $$this_file` ;\
	done
	@cd $(BUGZILLA_SRCDIR) && for this_file in `find skins -type f` ; do \
		install -m 0644 -o root -g root $$this_file $(BUGZILLA_WWW)/`dirname $$this_file` ;\
	done


install_lib_files:	extractsrc
	cd $(BUGZILLA_SRCDIR) && $(INSTALL) -m 0644 -o root -g root $(lib_files) $(BUGZILLA_ROOT)
	cd $(BUGZILLA_SRCDIR) && $(INSTALL) -m 0755 -o root -g root $(pl_files) $(BUGZILLA_ROOT)
	$(INSTALL) -d -m 0755 -o root -g root $(BUGZILLA_ROOT)/lib
	cd $(BUGZILLA_SRCDIR) && $(INSTALL) -m 0755 -o root -g root $(lib_perlscripts) $(BUGZILLA_ROOT)/lib
	$(INSTALL) -d -m 0755 -o root -g root $(BUGZILLA_PERLDIR)
	@cd $(BUGZILLA_SRCDIR) && for this_dir in `find $(lib_dir) -type d` ; do \
		install -d -m 0755 -o root -g root $(BUGZILLA_PERLDIR)/$$this_dir ;\
	done
	@cd $(BUGZILLA_SRCDIR) && for this_file in `find $(lib_dir) -type f` ; do \
		install -m 0644 -o root -g root $$this_file $(BUGZILLA_PERLDIR)/`dirname $$this_file` ;\
	done
	$(INSTALL) -d -m 0755 -o root -g root $(BUGZILLA_DATADIR)


install_cgi:	extractsrc
	$(INSTALL) -d -m 0755 -o root -g root $(BUGZILLA_CGIDIR)
	cd $(BUGZILLA_SRCDIR) && $(INSTALL) -m 0755 -o root -g root $(cgi_files) $(BUGZILLA_CGIDIR)


clean:
	rm -rf $(BUGZILLA_TARDIR) $(BUGZILLA_SRCDIR) bugzilla-srcdir

orig:	../$(ORIGTARGZ)
../$(ORIGTARGZ):
	tar -czf ../$(ORIGTARGZ) Makefile *.gz *.bz2

deb:	orig
	dpkg-buildpackage -rfakeroot -us -uc

