#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

BUILDDIR=$(CURDIR)/debian/build

VERSION := $(shell dpkg-parsechangelog | awk '/^Version:/ { print $$2 }')

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_CPU ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU 2>/dev/null)

# Take account of old dpkg-architecture output.
ifeq ($(DEB_HOST_ARCH_CPU),)
  DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
  ifeq ($(DEB_HOST_ARCH_CPU),x86_64)
    DEB_HOST_ARCH_CPU := amd64
  endif
endif

ifneq (,$(findstring :$(DEB_HOST_ARCH_CPU):,:amd64:i386:))
  bootloader-depends := grub
else
ifeq ($(DEB_HOST_ARCH_CPU),powerpc)
  bootloader-depends := yaboot, hfsutils
else
ifeq ($(DEB_HOST_ARCH_CPU),sparc)
  bootloader-depends := silo
endif
endif
endif

update:
	$(MAKE) -C d-i update

build: build-stamp

build-stamp:
	dh_testdir

ifndef UBIQUITY_NO_GTK
	./configure --prefix=/usr
	$(MAKE)
endif
	$(MAKE) -C d-i build

	mkdir -p $(BUILDDIR)/ubuntu
	mkdir -p $(BUILDDIR)/guadalinex

ifndef UBIQUITY_NO_KDE
	cd ubiquity/frontend && kdepyuic liveinstaller.ui && kdepyuic crashdialog.ui
endif
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -fr build-stamp $(BUILDDIR) ubiquity/frontend/liveinstaller.py \
		ubiquity/frontend/crashdialog.py
	find -type f \( -name \*.pyc -o -name \*.pyo \) -print0 | xargs -0r rm -f

	$(MAKE) -C d-i clean
	-$(MAKE) distclean

	dh_clean debian/ubiquity.install

install:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -A

	# For the meantime, nearly everything automake wants to install is
	# part of the GTK frontend.
ifndef UBIQUITY_NO_GTK
	$(MAKE) install DESTDIR=$(CURDIR)/debian/ubiquity-frontend-gtk
	rm -rf debian/ubiquity-frontend-gtk/usr/share/applications \
		debian/ubiquity-frontend-gtk/usr/share/ubiquity/pixmaps
endif
	$(MAKE) -C d-i install

	cp debian/ubiquity.install-any debian/ubiquity.install
ifneq (,$(wildcard debian/ubiquity.install-$(DEB_HOST_ARCH)))
	cat debian/ubiquity.install-$(DEB_HOST_ARCH) >> debian/ubiquity.install
endif
ifdef UBIQUITY_NO_GTK
	dh_install -Nubiquity-frontend-gtk
else
ifdef UBIQUITY_NO_KDE
	dh_install -Nubiquity-frontend-kde
else
	dh_install
endif
endif

	# Bits of manual installation that can't be done by dh_install
	sed 's/@VERSION@/$(VERSION)/g' installer \
		> debian/ubiquity/usr/bin/ubiquity
	chmod +x debian/ubiquity/usr/bin/ubiquity
	sed 's,/usr/lib/apt-setup,/usr/lib/ubiquity/apt-setup,g' \
		d-i/source/apt-setup/apt-setup \
		> debian/ubiquity/usr/lib/ubiquity/apt-setup/apt-setup
	chmod +x debian/ubiquity/usr/lib/ubiquity/apt-setup/apt-setup
	if [ -e "d-i/source/base-installer/kernel/$(DEB_HOST_ARCH).sh" ]; then \
		install -m644 d-i/source/base-installer/kernel/$(DEB_HOST_ARCH).sh \
			debian/ubiquity/usr/lib/ubiquity/base-installer/kernel.sh; \
	fi
	install d-i/source/clock-setup/debian/clock-setup.postinst \
		debian/ubiquity/usr/lib/ubiquity/clock-setup/clock-setup
	chmod +x debian/ubiquity/usr/lib/ubiquity/clock-setup/clock-setup
	install d-i/source/clock-setup/prebaseconfig.d/10clock-setup \
		debian/ubiquity/usr/lib/ubiquity/clock-setup/prebaseconfig
	chmod +x debian/ubiquity/usr/lib/ubiquity/clock-setup/prebaseconfig
	sed 's,\(prebaseconfig=\).*,\1/dev/null,' \
		d-i/source/hw-detect/debian/hw-detect/bin/hw-detect \
		> debian/ubiquity/bin/hw-detect
	chmod +x debian/ubiquity/bin/hw-detect
	install d-i/source/kbd-chooser/debian/prebaseconfig \
		debian/ubiquity/usr/lib/ubiquity/kbd-chooser/
	install d-i/source/localechooser/post-base-installer \
		d-i/source/localechooser/prebaseconfig \
		debian/ubiquity/usr/lib/ubiquity/localechooser/
	install d-i/source/tzsetup/prebaseconfig.d/*tzsetup \
		debian/ubiquity/usr/lib/ubiquity/tzsetup/prebaseconfig
	set -e; for x in user-setup user-setup-ask user-setup-apply; do \
		sed 's,/usr/lib/user-setup,/usr/lib/ubiquity/user-setup,g' \
			d-i/source/user-setup/$$x \
			> debian/ubiquity/usr/lib/ubiquity/user-setup/$$x; \
		chmod +x debian/ubiquity/usr/lib/ubiquity/user-setup/$$x; \
	done
ifeq ($(DEB_HOST_ARCH),powerpc)
	dh_installdirs -pubiquity usr/lib/ubiquity/yaboot-installer
	install d-i/source/yaboot-installer/debian/postinst \
		debian/ubiquity/usr/lib/ubiquity/yaboot-installer/yaboot-installer
endif
ifeq ($(DEB_HOST_ARCH),sparc)
	dh_installdirs -pubiquity usr/lib/ubiquity/silo-installer
	install d-i/source/silo-installer/debian/silo-installer.postinst \
		debian/ubiquity/usr/lib/ubiquity/silo-installer/silo-installer
endif

	set -e; for size in 16 22 32 48 64 128; do \
		mkdir -p debian/ubiquity-frontend-kde/usr/share/icons/crystalsvg/$${size}x$${size}/apps; \
		cp desktop/cr$${size}-app-ubiquity.png debian/ubiquity-frontend-kde/usr/share/icons/crystalsvg/$${size}x$${size}/apps/ubiquity.png; \
	done

# Build architecture-independent files here.
binary-indep: install
	dh_testdir
	dh_testroot
	dh_installchangelogs -i Changelog.Guadalinex
	dh_installdocs -i
#	dh_installexamples -i
	dh_installdebconf -i
#	dh_installman -i
	dh_strip -i
	dh_compress -i
	dh_fixperms -i
	dh_python -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a Changelog.Guadalinex
	dh_installdocs -a
#	dh_installexamples -a
	dh_installdebconf -a
	echo >> debian/ubiquity/DEBIAN/templates
	grep-dctrl -XFTemplate base-installer/kernel/linux/link_in_boot \
		d-i/templates \
		>> debian/ubiquity/DEBIAN/templates
	if [ "$$(tail -n1 debian/ubiquity/DEBIAN/templates)" ]; then \
		echo >> debian/ubiquity/DEBIAN/templates; \
	fi
	grep-dctrl -v -FTemplate base-installer/ d-i/templates \
		>> debian/ubiquity/DEBIAN/templates
#	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_python -a
	dh_installdeb -a
	dh_shlibdeps -a
ifdef bootloader-depends
	dh_gencontrol -a -- -V'bootloader-depends=$(bootloader-depends)'
else
	dh_gencontrol -a
endif
	dh_md5sums -a
	dh_builddeb -a

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