#! /usr/bin/make -f

b = $(shell pwd | sed 's,\(.*/\)*\([^/].*\),\2,')
# The name of the package (for example, `emacs').
# The version of the package (for example, `19.28').
s = $(shell echo $(b) | sed 's,\(.*-\)*-\([0-9][0-9]*\(\.[0-9][0-9]*\)*\),\1,')
ifeq ($(s),)
s = compress
else
#v = $(patsubst $(s)-%,%,$(b))
v = $(shell echo $(b) | sed 's/.*-//')
endif
ifeq ($(s),compress)
override s = bsd-compress
endif
ifeq ($(v),)
v = 4.0
else
ifeq ($(v),$(b))
v = 4.0
endif
endif
ifeq ($(p),)
p = $(s)
endif
pp = $(p)
ifneq ($(k),)
kk = -$(k)
override p := $(p)-$(k)
endif
# The debian version of the package.
d =
ifneq ($(d),)
dd = -$(d)
endif
# The name of the copyright file.
c = $(p)
# The architecture of the package.
a = $(shell dpkg --print-architecture)

# The maintainer.
m = $(shell w=`who am i | sed 's/\(.*!\)*\([^ ]*\) .*/\2/'`; if [ -z "`which ypcat`" ]; then showpass() { cat /etc/passwd }; else showpass() { 2>/dev/null ypcat passwd || cat /etc/passwd }; fi; f=`showpass | grep "^$${w}:" | cut -d: -f5 | sed 's/,.*//'`; : $${f:=$$NAME}; : $${f:=Unknown}; echo "$$f <$$w@`hostname -f`>")

# The kind of package is in $(k) which must never be setup by the user!

# Available docs.
docs = $(wildcard README* NEWS* CHANGES* 0_*)

#

installtarget = install

#

debdir = /usr/lib/compress-package

ifneq ($(words $(cmakefile)),0)
makefilename = $(word $(words $(cmakefile)),$(cmakefile))
endif

ifeq ($(makefilename),)
makefilename = $(wildcard GNUmakefile)
endif
ifeq ($(makefilename),)
makefilename = $(wildcard makefile)
endif
ifeq ($(makefilename),)
makefilename = $(wildcard Makefile)
endif

ifeq ($(cmakefilename),)
ifeq ($(makefilename),)
makefilename = $(debdir)/Makefile.debian
endif
cmakefile = -f $(makefilename)
endif

build:
# Builds the binary package.
	-test ! -r USERMEM -o -z "`cat USERMEM | sed 's/[0-9 	\
]//g' USERMEM`" && cp $(debdir)/USERMEM .
	$(MAKE) $(cmakefile) $(buildtarget)
	touch stamp-build

clean:
# Undoes the effect of `make -f debian/rules build'.
	@if [ -d debian-tmp ]; then \
	  if [ "`whoami`" = root ]; then \
	    rm -rf debian-tmp; \
	  else \
	    echo "Enter the root password to clean up the debian-tmp dir...";\
	    su -c "rm -rf debian-tmp";\
	  fi; \
	fi
	-test -f USERMEM && mv USERMEM USERMEM-
	-if [ ! -z "$(cleantarget)" ]; then \
	   $(MAKE) $(cmakefile) $(cleantarget); \
	 else \
	   $(MAKE) $(cmakefile) distclean || $(MAKE) $(cmakefile) clean; \
	 fi
	-$(MAKE) -f $(debdir)/Makefile.debian clean
	-test -f USERMEM- && mv USERMEM- USERMEM
	-rm -rf *~ *.orig ./#*# tmp.*
	-rm -f config.cache config.status config.h install config.log
	-rm -f debian/preinst debian/postinst \
	    debian/prerm debian/postrm debian/control
	-rmdir debian
	find -name '*~' -print0 | xargs -r0 rm --
	rm -f stamp-build stamp-historic stamp-basic

binary: binary-regular binary-historic binary-basic

binary-regular:
	test -f stamp-build || $(MAKE) -f $(debdir)/debian/rules build
	@if [ "`whoami`" = root ]; then \
	    $(MAKE) -f $(debdir)/debian/rules binary-regular-root; \
	else \
	  echo "Enter the root password to build the .deb file..."; \
	  su -c "exec $(MAKE) -f $(debdir)/debian/rules binary-regular-root"; \
	fi

binary-historic:
	test -f stamp-build || $(MAKE) -f $(debdir)/debian/rules build
	@if [ "`whoami`" = root ]; then \
	    $(MAKE) -f $(debdir)/debian/rules binary-historic-root; \
	else \
	  echo "Enter the root password to build the .deb file..."; \
	  su -c "exec $(MAKE) -f $(debdir)/debian/rules binary-historic-root"; \
	fi

binary-basic:
	test -f stamp-build || $(MAKE) -f $(debdir)/debian/rules build
	@if [ "`whoami`" = root ]; then \
	    $(MAKE) -f $(debdir)/debian/rules binary-basic-root; \
	else \
	  echo "Enter the root password to build the .deb file..."; \
	  su -c "exec $(MAKE) -f $(debdir)/debian/rules binary-basic-root"; \
	fi

binary-regular-root: binary-package-start
	$(MAKE) -f $(debdir)/debian/rules binary-package \
	    "p=$(p)" "pp=$(pp)" "v=$(v)" "d=$(d)" "c=$(c)" \
	    "a=$(a)" "m=$(m)"

binary-historic-root: binary-package-start
	if $(debdir)/debian/testhistoric.sh; \
 	then \
	    $(MAKE) -f $(debdir)/debian/rules binary-package \
		"p=$(p)" "pp=$(pp)" "v=$(v)" "d=$(d)" "c=$(c)" \
		"a=$(a)" "m=$(m)" k=historic; \
	fi

binary-basic-root: binary-package-start
	if $(debdir)/debian/testbasic.sh; \
 	then \
	    $(MAKE) -f $(debdir)/debian/rules binary-package \
		"p=$(p)" "pp=$(pp)" "v=$(v)" "d=$(d)" "c=$(c)" \
		"a=$(a)" "m=$(m)" k=basic; \
	fi

binary-package-start:
# Makes the start of a binary package.
	rm -rf debian-tmp
	install -d -g root -m 755 -o root debian-tmp
	chmod g-s debian-tmp
	install -d -g root -m 755 -o root debian-tmp/DEBIAN
	install -d -g root -m 755 -o root debian-tmp/usr/bin
	install -d -g root -m 755 -o root debian-tmp/usr/man/man1
	$(MAKE) $(cmakefile) $(installtarget) bindir=debian-tmp/usr/bin \
	    mandir=debian-tmp/usr/man BIN=debian-tmp/usr/bin \
	    MAN=debian-tmp/usr/man/man1
	if [ ! -f debian-tmp/usr/bin/compress ]; \
	then \
	    echo; echo "*** [$(debdir)/debian/rules] compress was not installed!"; exit 1; \
	fi
	if [ -z "`grep man Makefile`" ]; then \
	    $(MAKE) -f $(debdir)/Makefile.debian install-man \
		bindir=debian-tmp/usr/bin \
		mandir=debian-tmp/usr/man; \
	fi

binary-package:
# Makes a binary package.
	if [ ! -d debian ]; then mkdir debian; fi
	$(debdir)/debian/mkdebcleanup.sh $(pp) \
	    $(debdir)/debian/control$(kk) $(k) >debian/control
	find debian-tmp/usr/man ! -type d -a -name '*.[0-9]*' -print | \
	    xargs gzip -9f
	$(debdir)/debian/mkdebscripts.sh $(p) $(v)$(dd) "$(k)" \
	    debian-tmp $(debdir)/debian debian
	-test -f debian/preinst && \
	  install -g root -m 755 -o root debian/preinst \
	    debian-tmp/DEBIAN/preinst
	-test -f debian/postinst && \
	  install -g root -m 755 -o root debian/postinst \
	    debian-tmp/DEBIAN/postinst
	-test -f debian/prerm && \
	  install -g root -m 755 -o root debian/prerm \
	    debian-tmp/DEBIAN/prerm
	-test -f debian/postrm && \
	  install -g root -m 755 -o root debian/postrm \
	    debian-tmp/DEBIAN/postrm
	if [ ! -z "$(docs)" ]; then \
	  install -d -g root -m 755 -o root debian-tmp/usr/doc/$(p); \
	  install -g root -m 644 -o root $(docs) \
	    debian-tmp/usr/doc/$(p); \
	fi
	install -d -g root -m 755 -o root debian-tmp/usr/doc/copyright
	install -g root -m 644 -o root $(debdir)/debian/README \
	  debian-tmp/usr/doc/copyright/$(c)
	sed -e '1s/=P/$(p)/' -e "2s/=M/$(m)/" \
	    -e '3s/=V/$(v)/' -e '3s/=DD/$(dd)/' \
	    -e 's/=A/$(a)/' -e 's/=DEP/$(pp) (=$(v))/' \
		debian/control > debian-tmp/DEBIAN/control
	chmod 644 debian-tmp/DEBIAN/control
	dpkg --build debian-tmp
	mv debian-tmp.deb ../$(p)_$(v)$(dd)_$(a).deb && \
	    dpkg-name --overwrite ../$(p)_$(v)$(dd)_$(a).deb

source:
# Makes a source package.
	-test -f stamp-build && make -f $(debdir)/debian/rules clean
	( cd .. && tar cf - $(p)-$(v) | gzip -9f > $(pp)_$(v)$(dd).tar.gz )

diff:

dist: binary source diff
# Prepares the package for distribution.

orig:
# Prepares the original package from the previous
# Debian revision source package and context diff.
	( cd .. \
	  && mkdir $(pp).orig \
	  && cd $(pp).orig \
	  && tar xzf ../$(pp)_$(pv)-`expr $(pd) - 1`.tar.gz \
	  && cd $(pp)-$(pv) \
	  && (gzip -dc ../../$(pp)_$(pv)-`expr $(pd) - 1`.diff.gz \
	    | patch -sER -p1 ) \
	  && find . -name "*.orig" -exec rm -f {} \; \
	  && cd .. \
	  && mv $(pp)-$(pv) ../$(pp)-$(pv).orig \
	  && cd .. \
	  && rmdir $(pp).orig )
	touch stamp-orig

