#! /usr/bin/make -f

BINARY_PACKAGE = kylin-debianutils
COMPAT_DATA_DIR = debianutils
INSTALL_LOC = debian/$(BINARY_PACKAGE)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -Wall -g -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-z,relro

INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -m  755
INSTALL_DIR     = $(INSTALL) -p -d -m  755

# don't create translation tarball (useful for Ubuntu)
export NO_PKG_MANGLE=1

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure

override_dh_auto_build:
	dh_auto_build
	cd rust && cargo build --release --bins

override_dh_auto_install:
	dh_auto_install
	$(INSTALL_DIR) $(INSTALL_LOC)/bin $(INSTALL_LOC)/usr/bin
	$(INSTALL_PROGRAM) rust/target/release/run-parts $(INSTALL_LOC)/bin/run-parts
	$(INSTALL_PROGRAM) rust/target/release/tempfile $(INSTALL_LOC)/bin/tempfile
	$(INSTALL_PROGRAM) rust/target/release/ischroot $(INSTALL_LOC)/usr/bin/ischroot

execute_after_dh_auto_install:
	$(INSTALL_DIR) $(INSTALL_LOC)/bin \
		       $(INSTALL_LOC)/sbin \
		       $(INSTALL_LOC)/usr/bin \
		       $(INSTALL_LOC)/usr/sbin \
		       $(INSTALL_LOC)/usr/share/$(COMPAT_DATA_DIR)/shells.d \
		       $(INSTALL_LOC)/usr/share/$(COMPAT_DATA_DIR)
	mv $(INSTALL_LOC)/usr/bin/which.debianutils $(INSTALL_LOC)/bin/which
	mv $(INSTALL_LOC)/usr/share/man/man1/which.debianutils.1 $(INSTALL_LOC)/usr/share/man/man1/which.1
	mv $(INSTALL_LOC)/usr/share/man/de/man1/which.debianutils.1 $(INSTALL_LOC)/usr/share/man/de/man1/which.1
	mv $(INSTALL_LOC)/usr/share/man/es/man1/which.debianutils.1 $(INSTALL_LOC)/usr/share/man/es/man1/which.1
	mv $(INSTALL_LOC)/usr/share/man/fr/man1/which.debianutils.1 $(INSTALL_LOC)/usr/share/man/fr/man1/which.1
	mv $(INSTALL_LOC)/usr/share/man/it/man1/which.debianutils.1 $(INSTALL_LOC)/usr/share/man/it/man1/which.1
	mv $(INSTALL_LOC)/usr/share/man/ja/man1/which.debianutils.1 $(INSTALL_LOC)/usr/share/man/ja/man1/which.1
	mv $(INSTALL_LOC)/usr/share/man/pl/man1/which.debianutils.1 $(INSTALL_LOC)/usr/share/man/pl/man1/which.1
	mv $(INSTALL_LOC)/usr/share/man/sl/man1/which.debianutils.1 $(INSTALL_LOC)/usr/share/man/sl/man1/which.1
	mv $(INSTALL_LOC)/usr/sbin/installkernel $(INSTALL_LOC)/sbin/installkernel
	$(INSTALL_FILE) debian/shells $(INSTALL_LOC)/usr/share/$(COMPAT_DATA_DIR)

ifneq ($(DEB_HOST_ARCH_OS),linux)
	rm $(INSTALL_LOC)/sbin/installkernel \
	   $(INSTALL_LOC)/usr/share/man/man8/installkernel.8
endif
