#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
export DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
        confflags += --build $(DEB_HOST_GNU_TYPE)
else
        confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

config.status: configure
	dh_testdir

        # Apply Debian specific patches
	cp $(CURDIR)/ccache.c $(CURDIR)/ccache.c.unpatched
	cp $(CURDIR)/util.c $(CURDIR)/util.c.unpatched
	cp $(CURDIR)/ccache.1 $(CURDIR)/ccache.1.unpatched
	cp $(CURDIR)/ccache.h $(CURDIR)/ccache.h.unpatched
	cp $(CURDIR)/ccache.yo $(CURDIR)/ccache.yo.unpatched
	cp $(CURDIR)/config.h.in $(CURDIR)/config.h.in.unpatched
	cp $(CURDIR)/configure $(CURDIR)/configure.unpatched
	cp $(CURDIR)/configure.in $(CURDIR)/configure.in.unpatched
	cp $(CURDIR)/Makefile.in $(CURDIR)/Makefile.in.unpatched
	if test ! -f patch-stamp; then \
		for patch in $(CURDIR)/debian/patches/*.diff ;\
		do \
			echo APPLYING PATCH\: $${patch##*/};\
			patch -p0 < $$patch ;\
		done ;\
		touch patch-stamp ;\
	fi
	chmod +x $(CURDIR)/manage-cache.sh

	./configure $(confflags) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info

build: build-stamp

build-stamp:  config.status
	dh_testdir

	$(MAKE)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Unapply patches
	-test -r $(CURDIR)/ccache.c.unpatched && mv $(CURDIR)/ccache.c.unpatched $(CURDIR)/ccache.c
	-test -r $(CURDIR)/util.c.unpatched && mv $(CURDIR)/util.c.unpatched $(CURDIR)/util.c
	-test -r $(CURDIR)/ccache.1.unpatched && mv $(CURDIR)/ccache.1.unpatched $(CURDIR)/ccache.1
	-test -r $(CURDIR)/ccache.h.unpatched && mv $(CURDIR)/ccache.h.unpatched $(CURDIR)/ccache.h
	-test -r $(CURDIR)/ccache.yo.unpatched && mv $(CURDIR)/ccache.yo.unpatched $(CURDIR)/ccache.yo
	-test -r $(CURDIR)/config.h.in.unpatched && mv $(CURDIR)/config.h.in.unpatched $(CURDIR)/config.h.in
	-test -r $(CURDIR)/configure.unpatched && mv $(CURDIR)/configure.unpatched $(CURDIR)/configure
	-test -r $(CURDIR)/configure.in.unpatched && mv $(CURDIR)/configure.in.unpatched $(CURDIR)/configure.in
	-test -r $(CURDIR)/Makefile.in.unpatched && mv $(CURDIR)/Makefile.in.unpatched $(CURDIR)/Makefile.in
	-rm -f $(CURDIR)/manage-cache.sh
	-rm -f patch-stamp

	[ ! -f Makefile ] || $(MAKE) distclean

	dh_clean

	# Update config.sub and config.guess
	-test -r /usr/share/misc/config.sub && \
	  cp -f /usr/share/misc/config.sub config.sub
	-test -r /usr/share/misc/config.guess && \
	  cp -f /usr/share/misc/config.guess config.guess


install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/ccache.
	$(MAKE) install prefix=$(CURDIR)/debian/ccache/usr

	ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/$(DEB_BUILD_GNU_TYPE)-gcc
	ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/$(DEB_BUILD_GNU_TYPE)-g++
	set -e; for ver in 2.95 3.0 3.2 3.3 3.4 4.0 4.1 4.2 4.3; do \
		ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/$(DEB_BUILD_GNU_TYPE)-gcc-$$ver; \
		ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/gcc-$$ver; \
		ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/$(DEB_BUILD_GNU_TYPE)-g++-$$ver; \
		ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/g++-$$ver; \
	done
	ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/cc
	ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/c++
	ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/gcc
	ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/g++
	ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/i586-mingw32msvc-c++
	ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/i586-mingw32msvc-cc
	ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/i586-mingw32msvc-g++
	ln -s ../../bin/ccache $(CURDIR)/debian/ccache/usr/lib/ccache/i586-mingw32msvc-gcc

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installcron
	dh_installman
	dh_installinfo
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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