#! /usr/bin/make -f

package=perl
debian_files = preinst postinst prerm control

version := $(shell awk -- '/PATCHLEVEL/ {patchlevel = $$NF} \
			   /SUBVERSION/ {subversion = $$NF} \
			   END {if (subversion) {printf "5.%03dXX%02d\n", patchlevel, subversion} else {printf "5.%03d\n", patchlevel}}' patchlevel.h)
perlversion := $(subst XX,,$(version))
debversion  := $(subst XX,.,$(version))
expdebver    = 1# only need to set this if you're making a 2+ version of a subversion
arch := $(shell dpkg --print-architecture)
PWD := $(shell pwd)

ifndef NAME
NAME="Hagbard Debian"
endif


BINDIR     = debian/tmp/usr/bin
SUIDBINDIR = debian/tmp-suid/usr/bin
DBGBINDIR  = debian/tmp-dbg/usr/bin
MAN1DIR    = debian/tmp/usr/man/man1
MAN1EXT    = 1p
MAN3DIR    = debian/tmp/usr/man/man3
MAN3EXT    = 3pm
LIBDIR     = debian/tmp/usr/lib/perl5
SUIDLIBDIR = debian/tmp-suid/usr/lib/perl5
DBGLIBDIR  = debian/tmp-dbg/usr/lib/perl5
DOCDIR     = debian/tmp/usr/doc/perl
SUIDDOCDIR = debian/tmp-suid/usr/doc/perl-suid
DBGDOCDIR  = debian/tmp-dbg/usr/doc/perl-debug
CORE       = $(PWD)/$(LIBDIR)/$(arch)-linux/$(perlversion)/CORE

# Since Laziness is a virtue, let's make things a little easier for your 
# humble package maintainer.
HOSTNAME := $(shell hostname)
MAINTAINERHOSTNAME = kitchen

config.over: debian/config.over.MK
	sed -e '1s!#pwd#!$(PWD)!' debian/config.over.MK > config.over

config.stamp Makefile: config.over Configure
	rm -f config.sh
	rm -f t/perl
	./Configure -de -D prefix=/usr \
	  -D perladmin='none' -D man1ext=$(MAN1EXT) -D man3dir='/usr/man/man3' \
	  -D man3ext=$(MAN3EXT) -D ccflags='-I/usr/include/db -D_REENTRANT' \
	  -D sitelib='/usr/local/lib/site_perl' -D d_csh='undef' -D d_dosuid='define' \
	  -D optimize='-O2' -D archname='$(arch)-linux'
	touch config.stamp


build.stamp build: config.stamp
	make
	strip -s perl suidperl
	ln -fs perl perl.dist
	touch build.stamp
ifeq ($(HOSTNAME), $(MAINTAINERHOSTNAME))
	make test
endif


# we need to install this now so that when we do a make clean for the debug perl, it's not all wiped out
$(BINDIR)/perl: build.stamp
	-[ -d debian/tmp/usr/lib/perl5 -a -O debian/tmp/usr/lib/perl5 ] && rm -rf debian/tmp debian/tmp-suid 2>/dev/null
	-[ -d debian/tmp/usr/lib/perl5 ] && su -c 'rm -rf debian/tmp debian/tmp-suid'
	install -d debian/tmp/DEBIAN
	install -d $(BINDIR)
	install -d $(MAN1DIR)
	install -d $(MAN3DIR)
	install -d $(LIBDIR)/$(arch)-linux/$(perlversion)
	install -d $(DOCDIR)/emacs
	install -d $(DOCDIR)/examples

	install -d debian/tmp-suid/DEBIAN
	install -d $(SUIDBINDIR)
	install -d $(SUIDDOCDIR)
	install -d $(SUIDLIBDIR)/$(arch)-linux/$(perlversion)/CORE

	su -c  'make install && \
		gzip -9rf $(MAN1DIR) $(MAN3DIR) && \
		chmod go=rX $(LIBDIR)/$(arch)-linux/$(perlversion)/CORE && \
		rmdir -p debian/tmp/usr/local/lib/site_perl/$(arch)-linux && \
		$(BINDIR)/perl -i -pe "s#$(PWD)/debian/tmp##;" $(CORE)/../Config.pm && \
		chmod -R go=rX debian/.ph; cp -R debian/.ph/* $(CORE)/.. && \
		mv $(CORE)/sperl.o   $(SUIDLIBDIR)/$(arch)-linux/$(perlversion)/CORE && \
		mv $(BINDIR)/suidperl $(BINDIR)/sperl$(perlversion) $(SUIDBINDIR)'

#	install -d debian/tmp-base/DEBIAN
#	$(BINDIR)/perl debian/make.base $(arch)


# we now need to build the debug perl, we do this after we've installed the normal perl so we can blow stuff away 
# during our make

# note that we don't want to strip the binary
$(DBGBINDIR)/debugperl: $(BINDIR)/perl
	-[ -d debian/tmp-dbg -a -O debian/tmp-dbg ] && rm -rf debian/tmp-dbg 2>/dev/null
	-[ -d debian/tmp-dbg ] && su -c 'rm -rf debian/tmp-dbg'

	make clean
	make LIBPERL=libperld.a perl

	install -d debian/tmp-dbg/DEBIAN
	install -d $(DBGBINDIR)
	install -d $(DBGDOCDIR)
	install -d $(DBGLIBDIR)/$(arch)-linux/$(perlversion)/CORE

	mv ./perl $(DBGBINDIR)/debugperl
	ln $(DBGBINDIR)/debugperl $(DBGBINDIR)/dperl$(perlversion)
	mv ./libperld.a $(DBGLIBDIR)/$(arch)-linux/$(perlversion)/CORE	

binary-indep:
	true
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.


binary-arch: build.stamp $(BINDIR)/perl $(SUIDBINDIR)/suidperl $(DBGBINDIR)/debugperl
	$(BINDIR)/perl -pe 's/=version=/$$]/g; s/=arch=/$(arch)/g;' debian/preinst  > debian/tmp/DEBIAN/preinst
	$(BINDIR)/perl -pe 's/=version=/$$]/g; s/=arch=/$(arch)/g;' debian/prerm    > debian/tmp/DEBIAN/prerm
	$(BINDIR)/perl -pe 's/=version=/$$]/g; s/=arch=/$(arch)/g;' debian/postinst > debian/tmp/DEBIAN/postinst

	mv $(BINDIR)/perl $(BINDIR)/perl.dist

	cp Changes* $(DOCDIR)
	cp debian/changelog $(DOCDIR)/changelog.Debian
	cp -R eg/* $(DOCDIR)/examples
	cp -R emacs/* $(DOCDIR)/emacs
	chmod -R go=rX $(DOCDIR)
	gzip -9rf $(DOCDIR) || true
	install -m 644 debian/copyright $(DOCDIR)
	ln -sf ../perl/copyright $(SUIDDOCDIR)
	ln -sf ../perl/copyright $(DBGDOCDIR)
	dpkg-shlibdeps -dPre-Depends $(BINDIR)/perl.dist
	dpkg-gencontrol -pperl
#	dpkg-gencontrol -pperl-base  -Pdebian/tmp-base
	dpkg-gencontrol -pperl-suid  -Pdebian/tmp-suid
	dpkg-gencontrol -pperl-debug -Pdebian/tmp-dbg
	chmod 644 debian/{tmp,tmp-suid,tmp-dbg}/DEBIAN/control
	chmod 755 debian/tmp/DEBIAN/{postinst,prerm,preinst}

	su -c  'chown -R root.root debian/{tmp,tmp-suid,tmp-dbg}/ && \
		dpkg --build debian/tmp .. && \
		dpkg --build debian/tmp-suid .. && \
		dpkg --build debian/tmp-dbg ..'
#		dpkg --build debian/tmp-base ..; \

binary: binary-indep binary-arch

test-binary:
	-mv $(BINDIR)/perl.dist $(BINDIR)/perl
	rm -f $(DOCDIR)/copyright
	debian/rules binary

this-version:
	echo -e "perl ($(debversion)-$(expdebver)) experimental; urgency=low\n" > debian/tmp-changelog
	echo -e "  * New subversion of perl released.  Created .deb for it.\n" >> debian/tmp-changelog
	echo -e " -- $(NAME) <$(LOGNAME)@$(HOSTNAME)>  $(shell date +'%a, %d %b %Y %T %z')\n" >> debian/tmp-changelog
	cat debian/changelog >> debian/tmp-changelog && mv debian/tmp-changelog debian/changelog

clean:
	-$(MAKE) distclean
	-rm -rf config.over .config UU Makefile build.stamp config.stamp config.sh perl.dist t/perl t/c \
		*.old *.bak *~ debian/*~ debian/files* debian/substvars 
#	 note that if we need our own substvars, we need to remove debian/substvars above
	-[ -d debian/tmp -a -O debian/tmp ] && rm -rf debian/tmp debian/tmp-dbg debian/tmp-suid 2>/dev/null
	-[ -d debian/tmp ] && su -c 'rm -rf debian/tmp debian/tmp-dbg debian/tmp-suid'

.PHONY: binary binary-arch binary-indep clean build this-version

# Local Variables:
# mode:makefile
# vc-master-templates:(("%s../../RCS/%s,v" . RCS))
# End:
