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

set -e

# Summary of how this script can be called:
#        * <new-preinst> 'install'
#        * <new-preinst> 'install' <old-version>
#        * <new-preinst> 'upgrade' <old-version>
#        * <old-preinst> 'abort-upgrade' <new-version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package.

# Source setup configuration if present. The DEB-conventional location is
# /etc/default/salt-setup; /etc/sysconfig/salt-minion-setup is honored as
# well for cross-distro parity with the RPM packaging. Either file may
# pre-set SALT_HOME, SALT_USER, SALT_GROUP, SALT_NAME, SALT_EXTRAS_DIR,
# or SALT_ONEDIR_HARDEN. Values in the sourced files win over inherited
# env vars; the [ -n ... ] || guards below preserve whatever was set,
# falling back to the hardcoded defaults only when nothing was supplied.
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=1 opts in to relocating the salt daemon's writable
# state out of /opt/saltstack/salt into /var/lib/salt/minion/ so the
# onedir tree can stay root-owned and 0755. On 3006.x the default is
# unset (legacy layout preserved). The default flips to hardened on
# 3009.0. See issue #70198.

[ -n "$SALT_USER" ] || SALT_USER=salt
[ -n "$SALT_NAME" ] || SALT_NAME="Salt"
[ -n "$SALT_GROUP" ] || SALT_GROUP=salt

# In hardened mode, the salt user's home defaults to per-daemon
# /var/lib/salt/minion/home. Explicit SALT_HOME (env var or setup file)
# always wins. SALT_EXTRAS_DIR default is deferred to postinst -- it
# depends on the onedir Python interpreter, which isn't unpacked yet on
# a fresh install (see issue #68460).
if [ "$SALT_ONEDIR_HARDEN" = "1" ]; then
    [ -n "$SALT_HOME" ] || SALT_HOME=/var/lib/salt/minion/home
else
    [ -n "$SALT_HOME" ] || SALT_HOME=/opt/saltstack/salt
fi

case "$1" in
  install)
    # Propagate the resolved SALT_USER (from /etc/default/salt-setup,
    # /etc/sysconfig/salt-minion-setup, or the env var) into debconf so
    # the postinst's `db_get salt-minion/user` returns the override
    # instead of the template default ``salt``. Without this, a fresh
    # install with SALT_USER=alt_salt would create the alt_salt user
    # but the postinst would still try ``chown -R salt:salt ...`` and
    # fail.
    . /usr/share/debconf/confmodule
    db_set salt-minion/user $SALT_USER
  ;;

  upgrade)
    . /usr/share/debconf/confmodule

    # Determine the current minion user. The configured user in
    # /etc/salt/minion (or a drop-in under /etc/salt/minion.d) is the
    # authoritative source; only fall back to filesystem ownership if no
    # user is configured. The previous logic looked only at filesystem
    # ownership, which caused upgrades to reset state directories to the
    # wrong user when the minion was configured to run as a non-root user.
    CFG_USER=""
    if [ -f /etc/salt/minion ]; then
        CFG_USER=$(grep -E "^[[:space:]]*user:" /etc/salt/minion 2>/dev/null \
            | head -1 | cut -d ':' -f 2 | tr -d '[:space:]')
    fi
    if [ -z "$CFG_USER" ] && [ -d /etc/salt/minion.d ]; then
        CFG_USER=$(grep -r -h -E "^[[:space:]]*user:" /etc/salt/minion.d/ 2>/dev/null \
            | head -1 | cut -d ':' -f 2 | tr -d '[:space:]')
    fi

    if [ -n "$CFG_USER" ]; then
        CUR_USER=$CFG_USER
        CUR_GROUP=$(id -gn "$CFG_USER" 2>/dev/null || echo "$CFG_USER")
    elif [ -f /run/salt-minion.pid ]; then
        CUR_USER=$(ls -dl /run/salt-minion.pid | cut -d ' ' -f 3)
        CUR_GROUP=$(ls -dl /run/salt-minion.pid | cut -d ' ' -f 4)
    elif [ -d /etc/salt/pki/minion ]; then
        CUR_USER=$(ls -dl /etc/salt/pki/minion | cut -d ' ' -f 3)
        CUR_GROUP=$(ls -dl /etc/salt/pki/minion | cut -d ' ' -f 4)
    elif [ -d /var/cache/salt/minion ]; then
        CUR_USER=$(ls -dl /var/cache/salt/minion | cut -d ' ' -f 3)
        CUR_GROUP=$(ls -dl /var/cache/salt/minion | cut -d ' ' -f 4)
    else
        CUR_USER=$SALT_USER
        CUR_GROUP=$SALT_GROUP
    fi
    db_set salt-minion/user $CUR_USER
    chown -R $CUR_USER:$CUR_GROUP /etc/salt/pki/minion /etc/salt/minion.d \
                                  /var/log/salt/minion /var/cache/salt/minion \
                                  /var/run/salt/minion \
                                  || true

    if [ ! -f /var/lib/systemd/deb-systemd-helper-enabled/salt-minion.service.dsh-also ]
    then
        # Workaround service status detection
        echo /etc/systemd/system/multi-user.target.wants/salt-minion.service > /var/lib/systemd/deb-systemd-helper-enabled/salt-minion.service.dsh-also
    fi
  ;;

  abort-upgrade)
  ;;

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

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

# Automatically added by dh_installinit/13.24.2
if [ "$1" = "install" ] && [ -n "$2" ] && [ -e "/etc/init.d/salt-minion" ] ; then
	chmod +x "/etc/init.d/salt-minion" >/dev/null || true
fi
# End automatically added section


exit 0
