--- a/debian/base-files.dirs
+++ b/debian/base-files.dirs
@@ -1,4 +1,3 @@
-bin
 boot
 dev
 etc
@@ -8,19 +7,14 @@
 etc/skel
 etc/update-motd.d
 home
-lib
 proc
 root
 run
-sbin
 sys
 tmp
 usr
-usr/bin
 usr/games
 usr/include
-usr/lib
-usr/sbin
 usr/share
 usr/share/base-files
 usr/share/common-licenses
--- a/debian/base-files.lintian-overrides
+++ b/debian/base-files.lintian-overrides
 base-files: extra-license-file [usr/share/common-licenses/LGPL-2]
 base-files: extra-license-file [usr/share/common-licenses/LGPL-2.1]
 base-files: extra-license-file [usr/share/common-licenses/LGPL-3]
+
+# Yes, these links really should be relative
+base-files: relative-symlink usr/bin [bin]
+base-files: relative-symlink usr/lib [lib]
+base-files: relative-symlink usr/lib64 [lib64]
+base-files: relative-symlink usr/libx32 [libx32]
+base-files: relative-symlink usr/sbin [sbin]
--- a/debian/clean
+++ b/debian/clean
@@ -1 +1,2 @@
 debian/postinst
+debian/triggers
--- a/debian/control
+++ b/debian/control
@@ -7,7 +7,7 @@
 Rules-Requires-Root: binary-targets

 Package: base-files
-Provides: base
+Provides: base, usr-is-merged
 Architecture: any
 Pre-Depends: awk
 Depends: ${misc:Depends}
--- a/debian/postinst.in
+++ b/debian/postinst.in
@@ -106,3 +106,15 @@
     install_directory mnt 755 root
   fi
 fi
+
+if [ "$1" = triggered ]; then
+	for d in lib32 lib64 libo32 libx32; do
+		if test -d "$DPKG_ROOT/usr/$d"; then
+			test -h "$DPKG_ROOT/$d" && continue
+			ln -s "usr/$d" "$DPKG_ROOT/$d"
+		else
+			test -h "$DPKG_ROOT/$d" || continue
+			rm "$DPKG_ROOT/$d"
+		fi
+	done
+fi
--- a/debian/postrm
+++ b/debian/postrm
@@ -0,0 +1,13 @@
+#!/bin/sh
+set -e
+
+USR_MERGE="bin lib lib32 lib64 libo32 lib64 sbin"
+
+if [ "$1" = remove ]; then
+	for d in $USR_MERGE; do
+		# Remove DEP17 M4 protective diversions
+		dpkg-divert --quiet --package base-files --remove --no-rename --divert /$d.usr-is-merged /$d
+	done
+fi
+
+#DEBHELPER#
--- a/debian/preinst
+++ b/debian/preinst
@@ -0,0 +1,34 @@
+#!/bin/sh
+set -e
+
+USR_MERGE="bin lib lib32 lib64 libo32 lib64 sbin"
+
+if [ "$1" = install ] || [ "$1" = upgrade ]; then
+	for d in $USR_MERGE; do
+		if [ -d "$DPKG_ROOT/$d" ] && ! [ -h "$DPKG_ROOT/$d" ]; then
+			cat <<EOF
+
+
+******************************************************************************
+*
+* The base-files package cannot be installed because this system has a
+* split /usr.
+*
+* Please install the usrmerge package to convert this system to merged-/usr.
+*
+* For more information please read https://wiki.debian.org/UsrMerge.
+*
+******************************************************************************
+
+
+EOF
+			exit 1
+		fi
+	done
+	for d in $USR_MERGE; do
+		# Install DEP17 M4 protective diversions
+		dpkg-divert --quiet --package base-files --add --no-rename --divert /$d.usr-is-merged /$d
+	done
+fi
+
+#DEBHELPER#
--- a/debian/rules
+++ b/debian/rules
@@ -1,5 +1,7 @@
 #!/usr/bin/make -f

+include /usr/share/dpkg/architecture.mk
+
 OSNAME = "GNU/`uname | sed -e 's/GNU\///'`"
 ifeq ($(DEB_HOST_GNU_SYSTEM),linux)
   OSNAME=GNU/Linux
@@ -11,12 +13,28 @@
 VENDORFILE = debian
 DESTDIR = debian/base-files

+USR_MERGE = bin lib sbin
+ifneq (,$(filter $(DEB_HOST_ARCH),amd64 loong64 mips64el ppc64 ppc64el sparc64))
+USR_MERGE += lib64
+else ifneq (,$(filter $(DEB_HOST_ARCH),x32))
+USR_MERGE += libx32
+endif
+
 %:
 	dh $@

 override_dh_auto_build:
 	sh debian/check-md5sum-etc profile
 	sed -e "s/#VENDORFILE#/$(VENDORFILE)/g" debian/postinst.in > debian/postinst
+	set -e; { \
+		echo "# Triggers for creating multilib aliasing symlinks on demand"; \
+		for d in $(foreach d,lib32 lib64 libo32 libx32,$(if $(filter $(d),$(USR_MERGE)),,$(d))); do \
+			echo "interest-noawait $$d"; \
+		done; \
+	} > debian/triggers
+
+execute_after_dh_installdirs:
+	dh_installdirs $(foreach d,$(USR_MERGE),usr/$(d))

 override_dh_auto_install:
 	install -p -m 644 etc/*      $(DESTDIR)/etc
@@ -40,6 +58,8 @@

 override_dh_link:
 	dh_link -X os-release
+	# We want these links to be relative, so we cannot use dh_link.
+	$(foreach d,$(USR_MERGE),ln -s usr/$(d) debian/base-files/$(d) &&) :

 override_dh_compress:
 	dh_compress -X README
