#! /usr/bin/make -f

# Debian package information
package		= sed
docdir		= /usr/share/doc/$(package)
tmpdir		= $(CURDIR)/debian/tmp

DEB_BUILD_GNU_TYPE = $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CONFARGS = --host=$(DEB_HOST_GNU_TYPE)
endif

CFLAGS = -Wall -g
INSTALL = install
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
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif


all build: Makefile
	make INSTALL_PROGRAM="$(INSTALL_PROGRAM)"
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	LANG=C make check
endif
	touch build

clean:
	rm -f debian/substvars debian/files build config.log config.cache 
	rm -f doc/sed.info*
#	rm -f po/ca.gmo
	rm -rf debian/tmp autom4te.cache
	find . -type f -a \( -name \#\*\# -o -name .\*\~ -o -name \*\~ -o -name DEADJOE -o -name \*.orig -o -name \*.rej -o -name \*.bak -o -name .\*.orig -o -name .\*.rej -o -name .SUMS -o -name TAGS -o -name core -o \( -path \*/.deps/\* -a -name \*.P \) \) -exec rm -f {} \;
	-make distclean

Makefile: Makefile.in
	CFLAGS="$(CFLAGS)" ./configure --prefix=/usr \
			--exec-prefix=/ \
			--datadir=/usr/share \
			--mandir=/usr/share/man \
			--infodir=/usr/share/info \
			--without-included-regex $(CONFARGS)


binary: binary-indep binary-arch

binary-indep:

binary-arch: build checkroot
	-rm -rf debian/tmp debian/{files,substvars}
	install -d -o root -g root -m 755 $(tmpdir)$(docdir)/examples

# Install sed
	make DESTDIR=`pwd`/debian/tmp install
	strip --remove-section=.comment --remove-section=.note \
		--strip-unneeded debian/tmp/bin/sed
	rm -f $(tmpdir)/usr/share/info/dir*
	gzip -9 $(tmpdir)/usr/share/man/man1/*
	gzip -9 $(tmpdir)/usr/share/info/sed.info*

# Install some documentation
	install -p -o root -g root -m 644 AUTHORS BUGS README THANKS \
		NEWS $(tmpdir)$(docdir)
	install -p -o root -g root -m 644 ChangeLog $(tmpdir)$(docdir)/changelog
	install -p -o root -g root -m 644 debian/changelog \
		$(tmpdir)$(docdir)/changelog.Debian
	# We expect an error here for the examples-subdir
	-gzip -9 $(tmpdir)$(docdir)/*
	install -p -o root -g root -m 644 debian/copyright $(tmpdir)$(docdir)
	install -p -o root -g root -m 644 testsuite/dc.sed $(tmpdir)$(docdir)/examples/
	install -p -o root -g root -m 644 debian/my/sedfaq.txt $(tmpdir)$(docdir)

# Install Debian-specific stuff
	install -d -o root -g root -m 755 $(tmpdir)/DEBIAN
	install -p -o root -g root -m 755 debian/preinst $(tmpdir)/DEBIAN
	install -p -o root -g root -m 755 debian/postinst $(tmpdir)/DEBIAN
	install -p -o root -g root -m 755 debian/prerm $(tmpdir)/DEBIAN

# Build the packgae
	dpkg-shlibdeps -dPre-Depends $(tmpdir)/bin/sed
	dpkg-gencontrol -isp
	dpkg --build debian/tmp ..

checkroot:
	test root = "`whoami`"
