#!/usr/bin/make -f

CC =cc
CFLAGS =-g -O2 -Wall
CCUDEB =diet -v -Os gcc
CFLAGSUDEB =-nostdinc -g -DREALLY_SMALL -Wall
STRIP =strip

DIET_ARCHS =alpha amd64 arm hppa i386 ia64 mips mipsel powerpc ppc64 s390 sparc
ARCH ?=$(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq (,$(findstring $(ARCH),$(DIET_ARCHS)))
  CCUDEB =$(CC)
  CFLAGSUDEB =-g -Os -DREALLY_SMALL -Wall
endif
ifneq (,$(findstring diet,$(DEB_BUILD_OPTIONS)))
  CC =diet -v -Os gcc
  CFLAGS =-nostdinc -Wall
endif
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  STRIP =: strip
endif

DIR =$(shell pwd)/debian/dash
DIRA =$(shell pwd)/debian/ash

patch: deb-checkdir patch-stamp
patch-stamp:
	for i in `ls -t debian/diff/*.diff || :`; do \
	  patch -p1 <$$i || exit 1; \
	done
	touch patch-stamp

configure: deb-checkdir configure-stamp configure-udeb-stamp
configure-stamp: patch-stamp
	mkdir -p build-tmp
	(cd build-tmp && CC='$(CC)' CFLAGS='$(CFLAGS)' \
	  exec ../configure --host='$(DEB_HOST_GNU_TYPE)')
	touch configure-stamp
configure-udeb-stamp: patch-stamp
	mkdir -p build-udeb-tmp
	(cd build-udeb-tmp && CC='$(CCUDEB)' CFLAGS='$(CFLAGSUDEB)' \
	  exec ../configure --host='$(DEB_HOST_GNU_TYPE)')
	touch configure-udeb-stamp

build: deb-checkdir build-stamp build-udeb-stamp
build-stamp: configure-stamp
	-gcc -v
	(cd build-tmp && exec $(MAKE) CFLAGS='$(CFLAGS)')
	touch build-stamp
build-udeb-stamp: configure-udeb-stamp
	-gcc -v
	(cd build-udeb-tmp && exec $(MAKE) CFLAGS='$(CFLAGSUDEB)')
	touch build-udeb-stamp

po-templates: po-templates-stamp
po-templates-stamp: deb-checkdir
	po2debconf debian/dash.templates.in >debian/dash.templates
	touch po-templates-stamp

clean: deb-checkdir deb-checkuid
	rm -rf build-tmp build-udeb-tmp
	test ! -e patch-stamp || \
	  for i in `ls -tr debian/diff/*.diff || :`; do patch -p1 -R <$$i; done
	rm -f configure-stamp configure-udeb-stamp patch-stamp build-stamp \
	  build-udeb-stamp po-templates-stamp
	rm -rf '$(DIR)' '$(DIR)'-udeb '$(DIRA)'
	rm -f debian/files debian/substvars debian/dash.templates changelog

install: install-indep install-arch
install-indep: deb-checkdir deb-checkuid
	rm -rf '$(DIRA)'
	install -d -m0755 '$(DIRA)'/bin
	ln -s dash '$(DIRA)'/bin/ash
	install -d -m0755 '$(DIRA)'/usr/share/man/man1/
	ln -s dash.1.gz '$(DIRA)'/usr/share/man/man1/ash.1.gz
	# changelog
	test -r changelog || ln -s ChangeLog changelog
install-arch: deb-checkdir deb-checkuid build-stamp build-udeb-stamp
	# dash
	rm -rf '$(DIR)'
	install -d -m0755 '$(DIR)'/bin
	install -m0755 build-tmp/src/dash '$(DIR)'/bin/dash
	$(STRIP) -R .comment -R .note '$(DIR)'/bin/dash
	install -d -m0755 '$(DIR)'/usr/share/man/man1/
	install -m0644 src/dash.1 '$(DIR)'/usr/share/man/man1/dash.1
	gzip -9 '$(DIR)'/usr/share/man/man1/dash.1
	install -d -m0755 '$(DIR)'/usr/share/menu
	install -m0644 debian/dash.menu '$(DIR)'/usr/share/menu/dash
	# dash-udeb
	rm -rf '$(DIR)'-udeb
	install -d -m0755 '$(DIR)'-udeb/bin
	install -m0755 build-udeb-tmp/src/dash '$(DIR)'-udeb/bin/dash
	$(STRIP) -R .comment -R .note '$(DIR)'-udeb/bin/dash
	ln -s dash '$(DIR)'-udeb/bin/sh
	# changelog
	test -r changelog || ln -s ChangeLog changelog

binary: binary-indep binary-arch
binary-indep: install-indep ash.deb
	dpkg-gencontrol -isp -pash -P'$(DIRA)'
	dpkg -b '$(DIRA)' ..
binary-arch: install-arch po-templates dash.deb dash-udeb.udeb
	# dash
	rm -f debian/substvars
	test '$(CC)' != 'cc' || dpkg-shlibdeps '$(DIR)'/bin/dash
	dpkg-gencontrol -isp -pdash -P'$(DIR)'
	dpkg -b '$(DIR)' ..
	# dash-udeb
	rm -f debian/substvars
	test '$(CCUDEB)' != 'cc' || dpkg-shlibdeps '$(DIR)'-udeb/bin/dash
	dpkg-gencontrol -isp -pdash-udeb -P'$(DIR)'-udeb
	dpkg -b '$(DIR)'-udeb ..
	for i in ../dash-udeb_*.deb; do mv $$i $${i%deb}udeb; done
	sed -e '/^[^ ]*\.udeb/d;s/^\(dash-udeb_[^ ]*\.\)deb/\1udeb/' \
	  <debian/files >debian/files.new
	mv debian/files.new debian/files

.PHONY: configure build po-templates clean patch install install-indep \
	  install-arch binary binary-indep binary-arch

include debian/implicit
