#!/bin/sh
# postinst script for salt-minion.
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
  configure)
    . /usr/share/debconf/confmodule
    # Source setup configuration if present so SALT_ONEDIR_HARDEN /
    # SALT_HOME / SALT_EXTRAS_DIR are visible. /etc/default/salt-setup
    # is the DEB-conventional location; /etc/sysconfig/salt-minion-setup
    # is honored for cross-distro parity with the RPM packaging.
    if [ -f /etc/default/salt-setup ]; then
        . /etc/default/salt-setup
    fi
    if [ -f /etc/sysconfig/salt-minion-setup ]; then
        . /etc/sysconfig/salt-minion-setup
    fi
    # SALT_ONEDIR_HARDEN default is unset on 3006.x (legacy layout
    # preserved). Operators opt in explicitly via SALT_ONEDIR_HARDEN=1
    # to get the hardened layout early on LTS. The default flips on
    # 3009.0. See issue #70198.
    if [ "$SALT_ONEDIR_HARDEN" = "1" ]; then
        [ -n "$SALT_HOME" ] || SALT_HOME=/var/lib/salt/minion/home
        if [ -z "$SALT_EXTRAS_DIR" ]; then
            PY_VER=$(/opt/saltstack/salt/bin/python3 -c "import sys; sys.stdout.write('{}.{}'.format(*sys.version_info)); sys.stdout.flush()" 2>/dev/null || echo "")
            [ -n "$PY_VER" ] && SALT_EXTRAS_DIR=/var/lib/salt/minion/extras-${PY_VER}
        fi
    fi
    if db_get salt-minion/user
    then
      if [ "$RET" != "root" ]; then
        if [ ! -e "/var/log/salt/minion" ]; then
          touch /var/log/salt/minion
          chmod 640 /var/log/salt/minion
        fi
        if [ ! -e "/var/log/salt/key" ]; then
          touch /var/log/salt/key
          chmod 640 /var/log/salt/key
        fi
        if [ "$SALT_ONEDIR_HARDEN" = "1" ]; then
            # Hardened: create per-daemon writable dirs and chown ONLY
            # those. /opt/saltstack/salt stays root:root 0755.
            install -d -m 0755 -o "$RET" -g "$RET" /var/lib/salt/minion
            [ -n "$SALT_HOME" ] && install -d -m 0755 -o "$RET" -g "$RET" "$SALT_HOME"
            [ -n "$SALT_EXTRAS_DIR" ] && install -d -m 0755 -o "$RET" -g "$RET" "$SALT_EXTRAS_DIR"
            chown -R $RET:$RET /etc/salt/pki/minion /etc/salt/minion.d \
                               /var/log/salt/minion /var/cache/salt/minion \
                               /var/run/salt/minion \
                               || true
            # Upgrade migration: if the legacy extras dir has contents
            # and the new location is empty, move contents across. This
            # is a one-shot no-op-if-already-migrated migration.
            if [ -n "$PY_VER" ] \
               && [ -d "/opt/saltstack/salt/extras-${PY_VER}" ] \
               && [ -n "$(ls -A /opt/saltstack/salt/extras-${PY_VER} 2>/dev/null)" ] \
               && [ -d "$SALT_EXTRAS_DIR" ] \
               && [ -z "$(ls -A ${SALT_EXTRAS_DIR} 2>/dev/null)" ]; then
                mv /opt/saltstack/salt/extras-${PY_VER}/* "$SALT_EXTRAS_DIR"/ 2>/dev/null || true
                mv /opt/saltstack/salt/extras-${PY_VER}/.[!.]* "$SALT_EXTRAS_DIR"/ 2>/dev/null || true
                rmdir /opt/saltstack/salt/extras-${PY_VER} 2>/dev/null || true
                chown -R $RET:$RET "$SALT_EXTRAS_DIR" || true
            fi
        else
            # Legacy layout (default on 3006.x). No deprecation warning
            # yet -- the default flip lands on 3009.0.
            chown -R $RET:$RET /etc/salt/pki/minion /etc/salt/minion.d \
                               /var/log/salt/minion /var/cache/salt/minion \
                               /var/run/salt/minion \
                               /opt/saltstack/salt \
                               || true
            # If SALT_EXTRAS_DIR points outside /opt/saltstack/salt,
            # chown it explicitly so the relocated tree is writable too.
            if [ -n "$SALT_EXTRAS_DIR" ] && [ -d "$SALT_EXTRAS_DIR" ]; then
                chown -R $RET:$RET "$SALT_EXTRAS_DIR" || true
            fi
        fi
      fi
    fi
  ;;

  abort-upgrade|abort-remove|abort-deconfigure)
  ;;

  *)
    echo "postinst called with unknown argument '$1'" >&2
    exit 1
  ;;
esac

# Terminate the debconf protocol and close fd 3 before the
# debhelper-generated section runs. Without this, fd 3 (opened by
# confmodule above) leaks into commands such as deb-systemd-helper and
# produces "Bad file descriptor" errors in non-interactive preseed
# late_command chroot installs where the parent debconf frontend is
# not reachable. See https://github.com/saltstack/salt/issues/68269.
if [ "$1" = "configure" ] && command -v db_stop >/dev/null 2>&1; then
    db_stop
    exec 3>&- || true
fi

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

# Automatically added by dh_systemd_enable/13.24.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	# The following line should be removed in trixie or trixie+1
	deb-systemd-helper unmask 'salt-minion.service' >/dev/null || true

	# was-enabled defaults to true, so new installations run enable.
	if deb-systemd-helper --quiet was-enabled 'salt-minion.service'; then
		# Enables the unit on first installation, creates new
		# symlinks on upgrades if the unit file has changed.
		deb-systemd-helper enable 'salt-minion.service' >/dev/null || true
	else
		# Update the statefile to add new symlinks (if any), which need to be
		# cleaned up on purge. Also remove old symlinks.
		deb-systemd-helper update-state 'salt-minion.service' >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installinit/13.24.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -z "$DPKG_ROOT" ] && [ -x "/etc/init.d/salt-minion" ]; then
		update-rc.d salt-minion defaults >/dev/null
		if [ -n "$2" ]; then
			_dh_action=restart
		else
			_dh_action=start
		fi
		invoke-rc.d salt-minion $_dh_action || exit 1
	fi
fi
# End automatically added section


exit 0
