#!/usr/bin/make -f

# Debian package information
source		:= $(shell grep "^Source: " debian/control|head -1|sed 's/Source: \(.*\)/\1/g')
PACKAGE 	:= $(shell grep "^Package: " debian/control|head -1|sed 's/Package: \(.*\)/\1/g')
version		:= $(shell grep "^$(source) " debian/changelog|head -1 |sed 's/.*(\(.*\)\-[^\-]*).*/\1/g')
revision	:= $(shell grep "^$(source) " debian/changelog|head -1 |sed 's/.*([^\-]*\-\(.*\)).*/\1/g')

bindir		:= /usr/bin/
docdir		:= /usr/share/doc/$(PACKAGE)
ETCDIR		:=
mandir		:= /usr/share/man/man1/
DTMPDIR		:= $(CURDIR)/debian/tmp/
SRCDIR		:= ./

CLEANFILES	:= Crux/gtk/gtkrc capplet/Makefile
DOCFILES	:=
UPSTREAM	:= src/.libs/libeazel-engine.so
program		:= $(SRCDIR)$(UPSTREAM)
APPNAME		:= /usr/lib/gtk/themes/engines/libeazel-engine.so
FILES		:=
LOCATION	:=
MANPAGE		:=

INSTALL		:= install
INSTALL_DIR	:= $(INSTALL) -p -d	-o root -g root -m 755
INSTALL_FILE	:= $(INSTALL) -p	-o root -g root -m 644
INSTALL_PROGRAM	:= $(INSTALL) -p	-o root -g root -m 755
INSTALL_SCRIPT	:= $(INSTALL) -p	-o root -g root -m 755

CFLAGS		:= -O2 -Wall

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

debian_patches = 001-cvs 005-gcc34 50build 75install gtkrc


build: patched-stamp $(program)
$(program): config.status
	test -d debian
	cd $(SRCDIR) && $(MAKE)

config.status:
	test -d debian
	cd $(SRCDIR) && CFLAGS="$(CFLAGS)" ./configure --prefix=/usr


# Patches *must* be applied before running make clean, since they patch the Makefile.
clean: $(PACKAGE)clean reverse-patches
	-rm -f src/eazel-theme-draw.c.orig
	-rm -f *~
$(PACKAGE)clean: debclean patched-stamp
	-(cd $(SRCDIR) && $(MAKE) distclean)
	-(if [ -n "$(CLEANFILES)" ]; then rm -f $(CLEANFILES); fi)


checkroot:
	test 0 = "`id -u`"


debclean: checkroot
	test -d debian
	rm -rf $(DTMPDIR)
	rm -rf debian/files* debian/substvars


binary-indep:
	@echo Nothing to do


binary-arch: debclean build
# Setup directories
	$(INSTALL_DIR) $(DTMPDIR)/DEBIAN $(DTMPDIR)$(docdir) $(DTMPDIR)$(ETCDIR)

	$(MAKE) install DESTDIR=$(DTMPDIR)

# Install documentation
	if [ -n "$(DOCFILES)" ]; then \
		set -e ; for i in "$(DOCFILES)" ; do \
			$(INSTALL_FILE) $$i $(DTMPDIR)$(docdir)/ ; \
		done \
	fi
	if [ -n "$(MANPAGE)" -a -f "$(MANPAGE)" ]; then \
		$(INSTALL_DIR) $(DTMPDIR)$(mandir) ; \
		$(INSTALL_FILE) $(MANPAGE) $(DTMPDIR)$(mandir) ; \
		find $(DTMPDIR)$(mandir) -type f -exec gzip -9v {} \; ; \
	fi
# Install Debian-only documentation
	$(INSTALL_FILE) debian/changelog \
			$(DTMPDIR)$(docdir)/changelog.Debian
	gzip -9f $(DTMPDIR)/$(docdir)/*Debian
	$(INSTALL_FILE) debian/copyright $(DTMPDIR)$(docdir)

# Special case rules
	-rm -f $(DTMPDIR)/usr/lib/gtk/themes/engines/libeazel-engine.la
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	strip --strip-unneeded --remove-section=.comment --remove-section=.note $(DTMPDIR)$(APPNAME)
endif
	chmod 644 $(DTMPDIR)$(APPNAME)

# Install package scripts and other management files
	for i in debian/postinst debian/prerm; do \
		if [ -f $$i ]; then \
			TARGET=$(DTMPDIR)/DEBIAN/`basename $$i` ; \
			#sh $$i > $$TARGET $(PACKAGE) ; \
			cp $$i $$TARGET $ ; \
			chmod 755 $$TARGET ; \
		fi \
	done
	if [ -f debian/conffiles ]; then \
		$(INSTALL_FILE) debian/conffiles $(DTMPDIR)/DEBIAN ; \
	fi

# Build package
	dpkg-shlibdeps $(DTMPDIR)$(APPNAME)
	dpkg-gencontrol -isp -p$(PACKAGE)
	chown -R root:root $(DTMPDIR)
	chmod -R go=rX $(DTMPDIR)
	dpkg --build $(DTMPDIR) ..


binary: binary-indep binary-arch


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

# ---------------------------------------------------------------------------
# various rules to unpack addons and (un)apply patches.
# borrowed from egcs package

patch_dir	= debian/patches

apply-patches: patched-stamp
reverse-patches:
	@for stamp in none patched-*; do \
	  case "$$stamp" in none|patched-stamp|patched-\*) continue; esac; \
	  patch=`echo $$stamp | sed -e 's/patched-//'`; \
	  echo "trying to revert patch $$patch ..."; \
	  if [ -x $(patch_dir)/$$patch.dpatch ]; then true; else \
	    chmod +x $(patch_dir)/$$patch.dpatch; fi; \
	  if $(patch_dir)/$$patch.dpatch -unpatch; then \
	    echo "reverted $$patch patch."; \
	    rm -f $$stamp; \
	  else \
	    echo "error in reverting $$patch patch."; \
	    exit 1; \
	  fi; \
	done
	@echo "Removing patched-stamp"
	@rm -f patched-stamp

patched-%: $(patch_dir)/%.dpatch
	@if [ -x $< ]; then true; else chmod +x $<; fi
	@if [ -f $@ ]; then \
	  echo "$* patches already applied."; exit 1; \
	fi
	$< -patch
	@echo "$* patches applied." > $@

patched-stamp: $(foreach p,$(debian_patches),patched-$(p))
	@echo -e "\nPatches applied:" >> pxxx
	@for i in none $(debian_patches); do \
	  if [ -r debian/patches/$$i.dpatch ]; then \
	    echo -e "\n$$i:" >> pxxx; \
	    sed -n 's/^# *DP: */  /p' debian/patches/$$i.dpatch >> pxxx; \
	  fi \
	done
	@echo "Writing patched-stamp"
	@mv -f pxxx patched-stamp

# vi: ts=8 sw=8 ai nowrap
