#!/bin/sh

set -e

is_merged() {
  directories="/bin /sbin /lib"
  for dir in $directories; do
    [ -e "$DPKG_ROOT$dir" ] || continue
    [ "$(readlink -f "$DPKG_ROOT$dir")" = "$DPKG_ROOT/usr$dir" ] || return 1
  done

  # Avoid an exact match, as the target might vary depending on the tool
  # building the image. For example, systemd-nspawn links /lib64 to
  # /usr/lib/aarch64-linux-gnu on arm64, while on amd64 debootstrap links it to
  # /usr/lib64 and doesn't create it at all on arm64.
  # See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1019575
  arch_directories="/lib64 /lib32 /libo32 /libx32"
  for dir in $arch_directories; do
    [ -e "$DPKG_ROOT$dir" ] || continue
    case "$(readlink -f "$DPKG_ROOT$dir")" in
        "$DPKG_ROOT/usr/lib"*) ;;
        *) return 1;;
    esac
  done

  return 0
}

fail_if_unmerged() {
  if is_merged; then return; fi

  cat << END


******************************************************************************
*
* The systemd package cannot be installed because this system does
* not have a merged /usr.
*
* Please install the usrmerge package to convert this system to merged-/usr.
*
* For more information please read https://wiki.debian.org/UsrMerge.
*
******************************************************************************


END
  exit 1
}

# begin-remove-after: released:trixie
handle_tmp() {
  # We used to ship tmp.mount in /usr/share as an example, and some users link it in /etc/,
  # remove the link as a workaround until Trixie ships. This method would also have created
  # a link in local-fs.target.wants, so remove that too.
  if [ -L "$DPKG_ROOT/etc/systemd/system/tmp.mount" ] && [ "$(readlink "$DPKG_ROOT/etc/systemd/system/tmp.mount")" = "/usr/share/systemd/tmp.mount" ]; then
    rm -f "$DPKG_ROOT/etc/systemd/system/tmp.mount"
    rm -f "$DPKG_ROOT/etc/systemd/system/local-fs.target.wants/tmp.mount"
  fi

  # The user may have used 'systemctl enable /usr/share/systemd/tmp.mount', which
  # would have created a symlink in the local-fs.target.wants directory (as the
  # pre-Trixie tmp.mount included an [install] section for that purpose). If it
  # exists, remove it.
  if [ -L "$DPKG_ROOT/etc/systemd/system/local-fs.target.wants/tmp.mount" ] && [ "$(readlink "$DPKG_ROOT/etc/systemd/system/local-fs.target.wants/tmp.mount")" = "/usr/share/systemd/tmp.mount" ]; then
    rm -f "$DPKG_ROOT/etc/systemd/system/local-fs.target.wants/tmp.mount"
  fi

  # Unless it's already running, ensure /tmp/ does not get overwritten by
  # the tmpfs from tmp.mount in case a unit is later activated that implicitly
  # depends on it (for example with PrivateTmp=yes) by runtime masking it
  # Do not use systemctl here, as libsystemd-shared might not be unpacked yet
  if [ -z "$DPKG_ROOT" ] && [ -d /run/systemd/system ] && ! df --output=fstype /tmp | grep -q tmpfs; then
    touch /run/systemd/system/tmp.mount
  fi
}
# end-remove-after

case "$1" in
    install|upgrade)
    fail_if_unmerged
    # begin-remove-after: released:trixie
    if [ -n "$2" ] && [ -n "$3" ]; then
      handle_tmp
    fi
    # end-remove-after
    ;;
esac

# Automatically added by dh_installdeb/13.24.2
dpkg-maintscript-helper rm_conffile /etc/systemd/resolved.conf 251.3-2\~ -- "$@"
# End automatically added section

