#!/bin/sh
set -eu
if [ -d /run/systemd/system ]; then
  if [ -S /run/mission-core-k1/driver.sock ]; then
    if ! /usr/bin/python3 -I -c 'import http.client,json,socket; c=http.client.HTTPConnection("k1",timeout=5); c.sock=socket.socket(socket.AF_UNIX); c.sock.settimeout(5); c.sock.connect("/run/mission-core-k1/driver.sock"); c.request("GET","/prepare-safe"); r=c.getresponse(); assert r.status==200 and json.load(r).get("safe") is True'; then
      echo "Завершите подключение или запись K1 перед удалением интеграции." >&2
      exit 1
    fi
  fi
  systemctl stop mission-core-k1.service
  case "$1" in remove|deconfigure) systemctl disable mission-core-k1.service || true ;; esac
fi
