#!/bin/sh
set -eu
case "$1" in
  configure)
    if ! getent passwd mission-core-node >/dev/null; then
      adduser --system --group --home /var/lib/mission-core-node --no-create-home --disabled-login mission-core-node
    fi
    if ! getent passwd mission-core-sensors >/dev/null; then
      adduser --system --group --home /var/lib/mission-core-sensors --no-create-home --disabled-login mission-core-sensors
    fi
    # Only bootstrap required to open the GUI. Operational configuration is a
    # versioned job started by «Настройка окружения → Сконфигурировать».
    if [ -d /run/systemd/system ]; then
      systemctl daemon-reload
      systemctl enable mission-core-node.service
      systemctl restart mission-core-node.service
      systemctl try-restart mission-core-realsense.service
      if [ -f /run/mission-core-node-k1-upgrade-active ]; then
        # A jointly upgraded plugin starts itself after its own configuration.
        # Restore it here only when that package is already configured.
        if [ "$(dpkg-query -W -f='${Status}' mission-core-xgrids-k1 2>/dev/null || true)" = "install ok installed" ]; then
          systemctl start mission-core-k1.service
        fi
        rm -f /run/mission-core-node-k1-upgrade-active
      fi
    fi
    ;;
esac
