#!/bin/sh
set -eu
if [ "$1" = install ] || [ "$1" = upgrade ]; then
  if [ -d /run/systemd/system ]; then
  if [ -S /run/mission-core-sensors/driver.sock ]; then
    if ! /usr/bin/python3 -I -c 'import http.client,json,socket; c=http.client.HTTPConnection("driver",timeout=5); c.sock=socket.socket(socket.AF_UNIX); c.sock.settimeout(5); c.sock.connect("/run/mission-core-sensors/driver.sock"); c.request("GET","/prepare-safe"); r=c.getresponse(); assert r.status==200 and json.load(r).get("safe") is True'; then
      echo "Mission Core Node: остановите захват или просмотр записи перед обновлением или удалением." >&2
      exit 1
    fi
  fi
  mc_node_device_job=$(systemctl show --property=ActiveState --value mission-core-node-realsense-prepare.service 2>/dev/null || true)
  case "$mc_node_device_job" in
    active|activating) echo "Mission Core Node: дождитесь завершения подготовки устройства." >&2; exit 1 ;;
  esac

    mc_node_environment_state=$(systemctl show --property=ActiveState --value mission-core-node-environment.service 2>/dev/null || true)
    case "$mc_node_environment_state" in
      active|activating)
        echo "Mission Core Node: дождитесь завершения настройки окружения в приложении." >&2
        exit 1
        ;;
    esac
  fi
  . /etc/os-release
  if [ "${ID:-}" != ubuntu ] || [ "${VERSION_ID:-}" != 24.04 ]; then
    echo "Mission Core Node: this package does not support the installed system." >&2
    exit 1
  fi
fi
