#!/usr/bin/make -f
# 							-*- makefile -*-
# debian/rules file for the Debian/GNU Linux mirror package
# Copyright 1995-98 by Dirk Eddelbuettel <edd@debian.org>
# Copyright 2001-06 by Ian Maclaine-cross <iml@debian.org>

package = mirror
debcwd  := $(shell pwd)
debcon  := $(debcwd)/debian
deblog  := $(debcon)/changelog
debdir	:= $(debcon)/$(package)
debin  := $(debdir)/usr/bin
debdoc	:= $(debdir)/usr/share/doc/$(package)
debpat  := $(debdoc)/applied-patches
deblmjm := $(debdoc)/examples/lmjm
deblib	:= $(debdir)/usr/share/$(package)
debman  := $(debdir)/usr/share/man
debsrc  := $(debcwd)/dsrc
srcdoc  := $(debsrc)/usr/share/doc/$(package)

# New Perl Policy by Raphael
ifndef PERL
PERL = /usr/bin/perl
endif

TMP     =`pwd`/debian/tmp
archlib =`$(PERL) -MConfig -e 'print $$Config{installarchlib}'`
config  =INSTALLDIRS=perl INSTALLMAN1DIR=$(TMP)/usr/share/man/man1 INSTALLMAN3DIR=$(TMP)/usr/share/man/man3 INSTALLPRIVLIB=$(TMP)/usr/lib/perl5 INSTALLARCHLIB=$(TMP)$(archlib)

# export DH_VERBOSE=1

build: build-stamp
build-stamp:
	dh_testdir
	# nothing to be made here  -$(MAKE)
	#$(PERL) Makefile.PL $(config)
	#$(MAKE) CFLAGS="-O2 -g -Wall"
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	dh_clean

binary-indep: build
#	dh_testversion ge 2.0.40
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	#
	## Copy the Debian source tree to the package.
	(cd $(debsrc); cp -a * $(debdir))
	chmod a+rx $(debin)/*
	install -p -m 0755 mirror.pl		$(deblib)
	install -p -m 0644 dateconv.pl      $(deblib)
	install -p -m 0644 ftp.pl  			$(deblib)
	install -p -m 0644 lchat.pl 		$(deblib)
	install -p -m 0644 lsparse.pl 		$(deblib)
	dh_link usr/share/mirror/mirror.pl usr/bin/mirror
	dh_link usr/bin/mirrors usr/bin/debian-mirrors\
         usr/share/man/man1/mirrors.1 usr/share/man/man1/debian-mirrors.1
	install -p -m 0755 mm.pl		$(debin)/mirror-master
	install -p -m 0755 do_unlinks.pl	$(debin)/do_unlinks
	perl -p -i -e 's|/usr/local/bin/perl|/usr/bin/perl|;' \
                $(debin)/do_unlinks
	install -p -m 0755 pkgs_to_mmin.pl	$(debin)/pkgs_to_mmin
	install -p -m 0644 mm.man 		$(debman)/man1/mirror-master.1
	install -p -m 0644 packages/* 		$(deblmjm)/packages/
	install -p -m 0644 mmin	 		$(deblmjm)/mm/
	install -p -m 0644 mirror.defaults	$(deblmjm)
	install -p -m 0644 mirror.nightly	$(deblmjm)
	install -p -m 0644 support/*		$(deblmjm)
	install -p -m 0644 mirror-faq.html		$(debdoc)/html
	install -p -m 0644 mirror-on-dusk.gif	$(debdoc)/html
#	install -p -m 0644 mm.html		$(debdoc)/html/mirror-mm.html
	install -p -m 0644 mirror.html		$(debdoc)/html/mirror-ref.html
	#
	install -p -m 0644 debian/doc-base $(debdir)/usr/share/doc-base/$(package)
	install -p -m 0644 debian/Debian $(debdir)/etc/mirror/sites/Debian
	install -p -m 0644 debian/CTAN $(debdir)/etc/mirror/sites/CTAN
	install -p -m 0644 debian/CPAN $(debdir)/etc/mirror/sites/CPAN
	#
	chmod -R u+w $(debdir)/usr/share $(debdir)/usr/share/doc
	##
	dh_installdocs -n	mirror.txt
	##
	## Make patches for Debian files which differ from upstream.
	(cd $(debsrc)/usr/bin; ! diff -u - mirror-master <$(debcwd)/mm.pl >$(debpat)/mirror-master_2.9.dpat)
	(cd $(debsrc)/usr/bin; ! diff -u - do_unlinks <$(debin)/do_unlinks >$(debpat)/do_unlinks_2.9.dpat)
	(cd $(debsrc)/usr/bin; ! diff -u - pkgs_to_mmin <$(debcwd)/pkgs_to_mmin.pl >$(debpat)/pkgs_to_mmin_2.9.dpat)
	## Writes version into mirror. Displayed by -v after installation.
	! perl -pe'BEGIN{open(F,"$(deblog)");@m=split(/ /,<F>);close(F);@m[0].=" @m[1]  ".gmtime()." UTC"};s|Debian version VERSION|Debian patch version: @m[0]|;'  $(debsrc)/usr/share/mirror/mirror.pl | diff -u mirror.pl - >$(debpat)/mirror.pl_2.9.dpat
	! diff -u ftp.pl - <$(debsrc)/usr/share/mirror/ftp.pl >$(debpat)/ftp.pl_2.9.dpat
	! diff -u lchat.pl - <$(debsrc)/usr/share/mirror/lchat.pl >$(debpat)/lchat.pl_2.9.dpat
	! diff -u dateconv.pl - <$(debsrc)/usr/share/mirror/dateconv.pl >$(debpat)/dateconv.pl_2.9.dpat
	! diff -u lsparse.pl - <$(debsrc)/usr/share/mirror/lsparse.pl >$(debpat)/lsparse.pl_2.9.dpat
	! diff -u mirror.txt - <$(srcdoc)/mirror.txt >$(debpat)/mirror.txt_2.9.dpat
	(cd $(debman)/man1; ! diff -u mirror-master.1 - <$(debcwd)/man/mirror-master.1 >$(debpat)/mirror-master.1_2.9.dpat)
	(cd $(srcdoc)/html; ! diff -u - mirror-ref.html <$(debcwd)/mirror.html >$(debpat)/mirror.html_2.9.dpat)
	install -p -m 0644 debian/README.patch-n $(debpat)
	##
	dh_perl
	## fix the wrong perl path
	perl -p -i -e 's|/usr/local/bin/perl|/usr/bin/perl|;' \
                $(deblmjm)/lstest.pl
#       dh_installmenu
#       dh_installinit
#       dh_installcron
	dh_installman $(debcwd)/man/*.*
#	dh_undocumented
	dh_installchangelogs -k CHANGES-since-2.8.txt
	dh_strip
	dh_compress
	dh_fixperms
#	dh_suidregister
	dh_installdeb
#	dh_shlibdeps
	dh_gencontrol
#	dh_makeshlibs
#	dh_md5sums
	dh_builddeb

binary-arch: build

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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

