#!/bin/sh
# Run by the owner-facing release installer after hash verification. Scope is
# this product's packages and its dedicated cluster; never remove another DB.
set -eu
test "$(id -u)" = 0
mc_release_dir=$1
cd "$mc_release_dir"
sha256sum --check SHA256SUMS
mc_pg_guard=/etc/postgresql-common/createcluster.d/60-mission-core-install.conf
mc_created_guard=0
cleanup() {
  if [ "$mc_created_guard" = 1 ]; then
    if [ "$(cat "$mc_pg_guard")" = 'create_main_cluster = false' ]; then
      rm "$mc_pg_guard"
    fi
  fi
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' HUP TERM
if ! dpkg-query -W -f='${Status}' postgresql-16 2>/dev/null | grep -qx 'install ok installed'; then
  # postgresql-common explicitly supports this drop-in. Suppress only automatic
  # creation of the unrelated default main cluster during the first install.
  test ! -e "$mc_pg_guard"
  install -d -m 0755 /etc/postgresql-common/createcluster.d
  printf '%s\n' 'create_main_cluster = false' > "$mc_pg_guard"
  chmod 0644 "$mc_pg_guard"
  mc_created_guard=1
fi
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-remove \
  "$mc_release_dir/timescaledb-2-loader-postgresql-16_2.29.2~ubuntu24.04-1615_amd64.deb" \
  "$mc_release_dir/timescaledb-2-oss-postgresql-16_2.29.2~ubuntu24.04-1615_amd64.deb" \
  "$mc_release_dir/mission-core-node_0.8.16_amd64.deb" \
  "$mc_release_dir/mission-core-xgrids-k1_0.1.14_amd64.deb"
systemctl is-active --quiet mission-core-node.service
systemctl is-active --quiet mission-core-k1.service
runuser -u mission-core-node -- /usr/bin/python3 - <<'K1CHECK'
import http.client,json,socket,time
deadline=time.monotonic()+20
while True:
    c=http.client.HTTPConnection('k1',timeout=3)
    c.sock=socket.socket(socket.AF_UNIX);c.sock.settimeout(3)
    try:
        c.sock.connect('/run/mission-core-k1/driver.sock')
        c.request('GET','/status');r=c.getresponse();v=json.load(r)
        if r.status==200 and v.get('available') is True:break
    except (OSError, ValueError, http.client.HTTPException):pass
    finally:c.close()
    if time.monotonic()>deadline:raise SystemExit('K1 enrollment service did not become ready')
    time.sleep(1)
print('K1 enrollment service: ready')
K1CHECK
systemctl is-active --quiet postgresql@16-ndcmonitor.service
systemctl is-active --quiet mission-core-node-monitor.service
runuser -u mission-core-monitor -- /usr/bin/python3 - <<'PY'
import http.client,json,socket,time
deadline=time.monotonic()+20
while True:
    c=http.client.HTTPConnection('monitor',timeout=3)
    c.sock=socket.socket(socket.AF_UNIX);c.sock.settimeout(3)
    try:
        c.sock.connect('/run/mission-core-monitor/monitor.sock')
        c.request('GET','/status');r=c.getresponse();v=json.load(r)
        if r.status==200 and v['storage']=='ready' and v['latest']['seq']>0:break
    except (OSError, ValueError, KeyError, TypeError, http.client.HTTPException):
        pass
    finally:c.close()
    if time.monotonic()>deadline:raise SystemExit('Local telemetry did not become ready')
    time.sleep(1)
print('Local telemetry: ready')
PY
printf '%s\n' 'Mission Core Node R19: package and local archive checks passed.'
