#! /usr/bin/make -f
# Generated automatically from debian/packages
# by yada v0.9.7, of Tue, 07 Dec 1999
# Modified by Piotr Roszatycki <dexter@debian.org>, Mon,  9 Apr 2001 23:41:30 +0200

DEB_HOST_GNU_CPU    := $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
DEB_HOST_GNU_TYPE   := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_GNU_SYSTEM := $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)

DEB_BUILD_GNU_CPU    := $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)
DEB_BUILD_GNU_TYPE   := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_GNU_SYSTEM := $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)

VERSION:=$(shell LC_ALL=C dpkg-parsechangelog | sed -ne 's/^Version: *\([^2]*\)/\1/p')

.PHONY: default
default:
	@echo "Specify a target:"; \
	echo " build              compile the package"; \
	echo " binary             make all binary packages"; \
	echo " binary-arch        make all architecture-dependent binary packages"; \
	echo " binary-indep       make all architecture-independent binary packages"; \
	echo " clean              clean up the source package"; \
	echo; \
	echo " patch              apply patches from debian/*.diff"; \
	echo " unpatch            unapply patches from debian/*.diff{,disabled}"; \
	echo " install-tree       compile the package and create the install trees"; \
	echo " clean-install-tree clean up only under debian/"; \
	echo

.PHONY: patch unpatch
# Apply patches matching `debian/*.diff' to the source tree,
# and unapply any applied patches `debian/*.diff.disabled'
patch: debian/patch-stamp
debian/patch-stamp:
	@set -e; backupdirs=$$(ls -d debian/backup-* 2>/dev/null | wc -l); \
	if test $$backupdirs -gt 1; then \
	  echo "*** Yada error: There are multiple debian/backup-* directories."; \
	  echo "***             I can't cope.  Please clean up for me."; \
	  exit 1; \
	fi; \
	if test $$backupdirs = 1; then \
	  patchname="$$(echo debian/backup-* | sed 's=^debian/backup-==')"; \
	  echo "*** Cleaning up after interrupted patching run for $$patchname"; \
	  if test -f "debian/patch-$$patchname-applied"; then \
	    rm -rf "debian/backup-$$patchname"; \
	  else \
	    (cd "debian/backup-$$patchname"; find . -type f -print0) | xargs -0ri mv -f -- debian/backup-$$patchname/{} {}; \
	    find "debian/backup-$$patchname" -depth -type d -exec rmdir '{}' \;; \
	    if test -e "debian/backup-$$patchname"; then \
	      echo "*** Yada error: I could not recover cleanly from an interrupted patch."; \
	      echo "***             I can't cope.  Please clean up for me."; \
	      exit 1; \
	    fi; \
	  fi; \
	fi

	@set -e; backdowndirs=$$(ls -d debian/backdown-* 2>/dev/null | wc -l); \
	if test $$backdowndirs -gt 1; then \
	  echo "*** Yada error: There are multiple debian/backdown-* directories."; \
	  echo "***             I can't cope.  Please clean up for me."; \
	  exit 1; \
	fi; \
	if test $$backdowndirs = 1; then \
	  patchname="$$(echo debian/backdown-* | sed 's=^debian/backdown-==')"; \
	  echo "*** Cleaning up after interrupted unpatching run for $$patchname"; \
	  if test ! -f "debian/patch-$$patchname-applied"; then \
	    rm -rf "debian/backdown-$$patchname"; \
	  else \
	    (cd "debian/backdown-$$patchname"; find . -type f -print0) | xargs -0ri mv -f -- debian/backdown-$$patchname/{} {}; \
	    find "debian/backdown-$$patchname" -depth -type d -exec rmdir '{}' \;; \
	    if test -e "debian/backdown-$$patchname"; then \
	      echo "*** Yada error: I could not recover cleanly from an interrupted patch."; \
	      echo "***             I can't cope.  Please clean up for me."; \
	      exit 1; \
	    fi; \
	  fi; \
	fi

	@set -e; for stampfile in debian/patch-*-applied; do \
	  if test -f "$$stampfile"; then \
	    patchname="$$(echo "$$stampfile" | sed 's=^debian/patch-\(.*\)-applied$$=\1=')"; \
	    patchfile="debian/$$patchname.disabled"; \
	    if test -f "$$patchfile"; then \
	      echo "*** $$patchfile is now disabled; patching it out"; \
	      patchoptions="$$(sed -n '/^#PATCHOPTIONS:/{;s/^[^:]*://;p;q;}' <"$$patchfile")"; \
	      patch -R -f -i "$$patchfile" -b -V simple -B "debian/backdown-$$patchname/" $$patchoptions; \
	      rm -f "$$stampfile"; \
	      rm -rf "debian/backdown-$$patchname"; \
	    fi; \
	  fi; \
	done

	@set -e; for patchfile in debian/*.diff; do \
	  if test -f "$$patchfile"; then \
	    patchname="$$(echo "$$patchfile" | sed 's=^debian/==')"; \
	    stampfile="debian/patch-$$patchname-applied"; \
	    if test ! -f "$$stampfile"; then \
	      echo "*** Applying patch from $$patchname"; \
	      patchoptions="$$(sed -n '/^#PATCHOPTIONS:/{;s/^[^:]*://;p;q;}' <"$$patchfile")"; \
	      patch -N -f -i "$$patchfile" -b -V simple -B "debian/backup-$$patchname/" $$patchoptions; \
	      touch "$$stampfile"; \
	      rm -rf "debian/backup-$$patchname"; \
	    fi; \
	  fi; \
	done

	touch debian/patch-stamp

# Remove all managed patches from the source tree
unpatch:
	@set -e; backupdirs=$$(ls -d debian/backup-* 2>/dev/null | wc -l); \
	if test $$backupdirs -gt 1; then \
	  echo "*** Yada error: There are multiple debian/backup-* directories."; \
	  echo "***             I can't cope.  Please clean up for me."; \
	  exit 1; \
	fi; \
	if test $$backupdirs = 1; then \
	  patchname="$$(echo debian/backup-* | sed 's=^debian/backup-==')"; \
	  echo "*** Cleaning up after interrupted patching run for $$patchname"; \
	  if test -f "debian/patch-$$patchname-applied"; then \
	    rm -rf "debian/backup-$$patchname"; \
	  else \
	    (cd "debian/backup-$$patchname"; find . -type f -print0) | xargs -0ri mv -f -- debian/backup-$$patchname/{} {}; \
	    find "debian/backup-$$patchname" -depth -type d -exec rmdir '{}' \;; \
	    if test -e "debian/backup-$$patchname"; then \
	      echo "*** Yada error: I could not recover cleanly from an interrupted patch."; \
	      echo "***             I can't cope.  Please clean up for me."; \
	      exit 1; \
	    fi; \
	  fi; \
	fi

	@set -e; backdowndirs=$$(ls -d debian/backdown-* 2>/dev/null | wc -l); \
	if test $$backdowndirs -gt 1; then \
	  echo "*** Yada error: There are multiple debian/backdown-* directories."; \
	  echo "***             I can't cope.  Please clean up for me."; \
	  exit 1; \
	fi; \
	if test $$backdowndirs = 1; then \
	  patchname="$$(echo debian/backdown-* | sed 's=^debian/backdown-==')"; \
	  echo "*** Cleaning up after interrupted unpatching run for $$patchname"; \
	  if test ! -f "debian/patch-$$patchname-applied"; then \
	    rm -rf "debian/backdown-$$patchname"; \
	  else \
	    (cd "debian/backdown-$$patchname"; find . -type f -print0) | xargs -0ri mv -f -- debian/backdown-$$patchname/{} {}; \
	    find "debian/backdown-$$patchname" -depth -type d -exec rmdir '{}' \;; \
	    if test -e "debian/backdown-$$patchname"; then \
	      echo "*** Yada error: I could not recover cleanly from an interrupted patch."; \
	      echo "***             I can't cope.  Please clean up for me."; \
	      exit 1; \
	    fi; \
	  fi; \
	fi

	@set -e; for stampfile in debian/patch-*-applied; do \
	  if test -f "$$stampfile"; then \
	    patchname="$$(echo "$$stampfile" | sed 's=^debian/patch-\(.*\)-applied$$=\1=')"; \
	    patchfile="debian/$$patchname"; \
	    if test ! -f "$$patchfile"; then \
	      patchfile="$$patchfile.disabled"; \
	    fi; \
	    if test -f "$$patchfile"; then \
	      echo "*** Removing patch from $$patchname"; \
	      patchoptions="$$(sed -n '/^#PATCHOPTIONS:/{;s/^[^:]*://;p;q;}' <"$$patchfile")"; \
	      patch -R -f -i "$$patchfile" -b -V simple -B "debian/backdown-$$patchname/" $$patchoptions; \
	      rm -f "$$stampfile"; \
	      rm -rf "debian/backdown-$$patchname"; \
	    else \
	      echo "*** Yada warning: Cannot find a patchfile named \`$$patchname' to unapply"; \
	    fi; \
	  fi; \
	done

	rm -f debian/patch-stamp

# Build the package and prepare the install tree

.PHONY: build-only build
build-only: debian/build-stamp
build: build-only

# Make sure these rules and the control file are up-to-date

.PHONY: rules control
rules: debian/rules
debian/rules: ./yada debian/packages
	./yada rebuild rules

control: debian/control
debian/control: ./yada debian/packages
	./yada rebuild control

debian/build-stamp: debian/patch-stamp
	@[ -f ./yada -a -f debian/rules ]
	touch debian/build-stamp

.PHONY: install-tree
install-tree: install-tree-any
install-tree-any: \
	debian/tmp-yada/DEBIAN/control

debian/tmp-yada/DEBIAN/control: debian/build-stamp debian/control
	rm -rf debian/tmp-yada
	umask 022 && install -d debian/tmp-yada/DEBIAN
	install -d debian/tmp-yada/usr/share/doc/yada
	umask 022; ./yada generate copyright \
	                       >debian/tmp-yada/usr/share/doc/yada/copyright
	install -m 644 -p debian/changelog \
	                  debian/tmp-yada/usr/share/doc/yada/changelog
	@umask 022 \
	    && export PACKAGE="yada" \
	    && export ROOT="$$(pwd)/debian/tmp-yada" \
	    && export CONTROL="$$(pwd)/debian/tmp-yada/DEBIAN" \
	    && export VERSION="$(VERSION)" \
	    && export DEB_HOST_GNU_CPU="$(DEB_HOST_GNU_CPU)" \
	    && export DEB_HOST_GNU_TYPE="$(DEB_HOST_GNU_TYPE)" \
	    && export DEB_HOST_GNU_SYSTEM="$(DEB_HOST_GNU_SYSTEM)" \
	    && export DEB_BUILD_GNU_CPU="$(DEB_BUILD_GNU_CPU)" \
	    && export DEB_BUILD_GNU_TYPE="$(DEB_BUILD_GNU_TYPE)" \
	    && export DEB_BUILD_GNU_SYSTEM="$(DEB_BUILD_GNU_SYSTEM)" \
 && (\
	echo -E 'eval "yada () { perl $$(pwd)/yada \"\$$@\"; }"; set -e; set -v';\
	echo -E 'yada install -script yada';\
	echo -E 'yada install -man yada.1 packages.7';\
	echo -E 'yada install -doc yada.txt';\
	echo -E 'yada install -doc -subdir examples debian/packages -as packages.yada';\
	echo -E 'yada install -doc -subdir examples debian/foo.diff -as foo.diff';\
	echo -E 'yada install -doc announce -as README.Debian') | /bin/sh
	./yada compress yada
	./yada generate maintscripts yada
	find debian/tmp-yada -type f -print \
	  | sed -n 's/^debian\/tmp-yada\(\/etc\/.*\)$$/\1/p' \
	    > debian/tmp-yada/DEBIAN/conffiles
	if test ! -s debian/tmp-yada/DEBIAN/conffiles; then rm -f debian/tmp-yada/DEBIAN/conffiles; fi
	./yada rebuild control
	./yada generate substvars yada
	umask 022 && dpkg-gencontrol -isp -pyada -Pdebian/tmp-yada

# Build package files

.PHONY: binary binary-arch binary-indep
binary: binary-arch binary-indep
binary-arch: binary-arch-any

.PHONY: binary-arch-any
binary-arch-any:
binary-indep: \
	binary-package-yada

.PHONY: binary-package-yada
binary-package-yada: check-root debian/tmp-yada/DEBIAN/control
	@[ -f ./yada -a -f debian/rules ]
	chown -R 0.0 debian/tmp-yada
	chmod -R u=rwX,go=rX debian/tmp-yada
	@if [ -d debian/tmp-yada/usr/doc/yada ]; then \
	  echo "*** Yada warning: /usr/doc/yada should be /usr/share/doc/yada";\
	fi
	dpkg-deb --build debian/tmp-yada ..

.PHONY: check-root
check-root:
	@[ `id -u` = 0 ] || (echo "You must be root to do this!"; false)

# Clean up afterwards

.PHONY: clean clean-install-tree clean-build
clean: clean-install-tree clean-build unpatch debian/control debian/rules

clean-build:
	@[ -f ./yada -a -f debian/rules ]
	rm -f debian/build-stamp debian/depends-stamp
	@umask 022 && (\
	echo -E 'eval "yada () { perl $$(pwd)/yada \"\$$@\"; }"; set -e; set -v';\
	echo -E '# This shouldn'\''t find its way into the source package...';\
	echo -E 'rm -f .*.prcs_aux') | /bin/sh

clean-install-tree: debian/rules
	@[ -f ./yada -a -f debian/rules ]
	rm -f debian/install-tree-stamp
	rm -rf debian/tmp* debian/files* debian/substvars
