#!/bin/sh
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# dpkg has already put the files and the mask links in place. What is left is
# what a file list cannot express: creating the etcd user and directory,
# retiring the copies that provision.sh used to install into /etc, enabling the
# conntrack unit, and applying the settings now, so an in-place update does not
# wait for the next boot.
set -e

[ "$1" = "configure" ] || exit 0

PACKAGE="nvke-node-sysconfig"

# The etcd user, then the directory it owns. Both are no-ops when they already
# exist with the right owner; a root:root directory from an earlier kubeadm init
# is corrected in place, and its contents are never touched. Neither runs
# through systemd, so this works in the Packer build VM and a chroot alike.
systemd-sysusers /usr/lib/sysusers.d/50-nvke-etcd.conf
systemd-tmpfiles --create /usr/lib/tmpfiles.d/50-nvke-etcd.conf

# Images built before this package existed carry these from provision.sh. All
# three are removed only while they are the bytes provision.sh wrote; an
# operator's edit is left alone. The unit under /etc masks the vendor unit in
# /lib, so an edited copy keeps applying until the operator removes it.
old_unit=/etc/systemd/system/conntrack-tune.service
if [ -f "${old_unit}" ]; then
  case "$(sha256sum "${old_unit}" | cut -d' ' -f1)" in
  # The unit as provision.sh installed it, in each version it has had.
  04ebbf9d8ff85e0b6fc1990f800d69d459722010020b5250778f28c20119bc5b | \
    ca8e373854fba9b3124723ebd95f236e00282036b1ed5e42755bc211ab153e26 | 5b55f6006ceb3a5fc0d45c9c814cca6b1a767939cb53dc470b94799ff5f87690)
    echo "${PACKAGE}: removing ${old_unit} so the vendor unit in /lib applies."
    rm -f "${old_unit}"
    ;;
  *)
    echo "${PACKAGE}: ${old_unit} was edited, so it is left in place and replaces the packaged unit in /lib. Remove it to use the packaged one." >&2
    ;;
  esac
fi

old_timeouts=/etc/sysctl.d/10-nke-conntrack-timeouts.conf
old_timeouts_sha256=cb2f42623e9407aa55bfe5fb009869d5bd2f1cad0048b03596a9ae5fd408e800
if [ -f "${old_timeouts}" ]; then
  if [ "$(sha256sum "${old_timeouts}" | cut -d' ' -f1)" = "${old_timeouts_sha256}" ]; then
    rm -f "${old_timeouts}"
  else
    echo "${PACKAGE}: ${old_timeouts} was edited, so it is left in place. It sorts before the packaged 50-nvke-conntrack-timeouts.conf, which overrides it; move your value to a later-sorting file such as /etc/sysctl.d/99-local.conf." >&2
  fi
fi

# The generated file's old name. /run is emptied at every boot, so this only
# matters on a node that was running when the package arrived.
rm -f /run/sysctl.d/10-nke-conntrack.conf

# The old name was one an operator could replace by putting a same-named file in
# /etc/sysctl.d. That file now replaces nothing, and sorts before the new one.
if [ -f /etc/sysctl.d/10-nke-conntrack.conf ]; then
  echo "${PACKAGE}: /etc/sysctl.d/10-nke-conntrack.conf no longer replaces the generated file, which is now 50-nvke-conntrack.conf and overrides it. Move your values to a later-sorting file such as /etc/sysctl.d/99-local.conf." >&2
fi

old_modules=/etc/modules-load.d/nf_conntrack.conf
if [ -f "${old_modules}" ] && [ "$(cat "${old_modules}")" = nf_conntrack ]; then
  rm -f "${old_modules}"
fi

# DOCA enables ovs-exporter.service, and on an image baked before this package
# provision.sh masked it afterwards. The mask hides the unit from `systemctl
# disable`, which then leaves DOCA's enablement links behind; when the mask goes
# on remove, the next OVS start would bring the unauthenticated exporter up.
# Drop the links now. The unit file itself is not ours to touch.
for wants in /etc/systemd/system/*.wants/ovs-exporter.service; do
  if [ -L "${wants}" ]; then
    rm -f "${wants}"
  fi
done

# Enabled for the next boot, the way debhelper does it: deb-systemd-helper
# enables the unit on first install and on a reinstall after remove, and leaves
# it disabled on an upgrade or reinstall if the operator disabled it since.
# Starting it is done below, and only where systemd is running.
deb-systemd-helper unmask conntrack-tune.service >/dev/null || true
if deb-systemd-helper --quiet was-enabled conntrack-tune.service; then
  deb-systemd-helper enable conntrack-tune.service >/dev/null
else
  deb-systemd-helper update-state conntrack-tune.service >/dev/null
fi

# Apply now where systemd is running: an in-place update, not the Packer build
# VM's first boot. Failures are not fatal; the next boot applies the same files.
if [ -d /run/systemd/system ]; then
  systemctl daemon-reload || true
  modprobe nf_conntrack || true
  systemctl restart conntrack-tune.service || true
  # No file arguments: systemd-sysctl then reads every sysctl.d file in the
  # normal order, so an operator's later-sorting override, such as
  # /etc/sysctl.d/99-local.conf, still wins over ours. The prefix limits it to
  # the net.netfilter keys, which holds every key this package sets. The prefix
# matches whole path components, so it cannot name nf_conntrack_* alone.
  /lib/systemd/systemd-sysctl --prefix=net/netfilter 2>/dev/null || true
fi
