#!/usr/bin/make -f

define checkdir
	test -f debian/rules
endef

CFLAGS = -Wall -W -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

ifeq (,$(DEB_BUILD_ARCH))
	DEB_BUILD_ARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
endif

configure: stamp-configure
stamp-configure:
	$(checkdir)

	touch configure aclocal.m4 stamp-h.in Makefile.in config.h.in
	CFLAGS="$(CFLAGS)" ./configure --prefix=/usr \
		--bindir=/bin --mandir=/usr/share/man

	touch stamp-configure

build: build-stamp
build-stamp: stamp-configure
	$(checkdir)

	$(MAKE)
	LANG=C $(MAKE) check

	touch build-stamp

clean: checkroot
	$(checkdir)
	# Disable the posh -i tests due to sbuild issues
	rm -f tests/ttylovers.t
	rm -f build-stamp stamp-configure

	# Add here commands to clean up after the build process.
	if [ -f Makefile ]; then \
	  $(MAKE) distclean; \
	fi

	rm -rf debian/posh debian/posh.substvars
	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 {} \;

install: build checkroot
	$(checkdir)

	# Add here commands to install the package into debian/<packagename>
	$(MAKE) install LDFLAGS="" INSTALL_PROGRAM='$(INSTALL_PROGRAM)' \
		DESTDIR=$(CURDIR)/debian/posh

# 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 checkroot
	$(checkdir)

	strip --remove-section=.comment --remove-section=.note debian/posh/bin/posh

	install -d debian/posh/DEBIAN
	$(INSTALL_DIR) debian/posh/usr/share/doc/posh debian/posh/usr/share/menu

	$(INSTALL_SCRIPT) debian/posh.config debian/posh/DEBIAN/config
	# Changed for po-debconf switch - see po-debconf(7)
	#debconf-mergetemplate  debian/posh.templates.de debian/posh.templates.es debian/posh.templates.fr debian/posh.templates.ja debian/posh.templates.ru debian/posh.templates.sv debian/posh.templates.pt_BR debian/posh.templates > debian/posh/DEBIAN/templates
	po2debconf debian/posh.templates >debian/posh/DEBIAN/templates
	# End of po-debconf switch changes

	$(INSTALL_FILE) debian/menu debian/posh/usr/share/menu/posh
	$(INSTALL_FILE) debian/changelog debian/posh/usr/share/doc/posh/changelog
	gzip -9f debian/posh/usr/share/doc/posh/changelog
	$(INSTALL_FILE) debian/copyright debian/posh/usr/share/doc/posh/copyright
	chmod a-x debian/posh/usr/share/man/man1/posh.1
	gzip -9f debian/posh/usr/share/man/man1/posh.1

	$(INSTALL_SCRIPT) debian/posh.postinst debian/posh/DEBIAN/postinst
	$(INSTALL_SCRIPT) debian/posh.prerm debian/posh/DEBIAN/prerm
	$(INSTALL_SCRIPT) debian/posh.postrm debian/posh/DEBIAN/postrm

	dpkg-shlibdeps -Tdebian/posh.substvars -dDepends debian/posh/bin/posh
	dpkg-gencontrol -ldebian/changelog -isp -pposh -Tdebian/posh.substvars -Pdebian/posh
	cd debian/posh && find * -type f  ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums

	chown -R root:root debian/posh
	chmod -R g-w,a+rX debian/posh

	dpkg --build debian/posh ..

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

prebuild:
	autoreconf -fi

checkroot:
	$(checkdir)
	test root = "`whoami`"
