#!/bin/sh # postinst — deb install and upgrade paths (spec §7). # # dpkg calls: postinst configure [most-recently-configured-version] # fresh install: $1 = "configure", $2 = "" # upgrade: $1 = "configure", $2 = old version set -eu STORE_DIR="/var/lib/fenris" STORE_DB="${STORE_DIR}/observations.db" STORE_BAK="${STORE_DIR}/observations.db.bak" VENV_PYTHON="/opt/fenris/bin/python3" case "${1:-}" in configure) if [ -n "${2:-}" ]; then # Upgrade — snapshot, migration, daemon-reload, conditional timer restart if [ -f "${STORE_DB}" ]; then cp "${STORE_DB}" "${STORE_BAK}" 2>/dev/null || true fi if [ -x "${VENV_PYTHON}" ] && [ -f "${STORE_DB}" ]; then "${VENV_PYTHON}" -c " from fenris.store import migrate_to_latest from pathlib import Path n = migrate_to_latest(Path('${STORE_DB}')) print(f'Fenris migration: {n} step(s) applied') if n else None " 2>&1 || echo "Fenris: migration skipped (store not yet initialized)" fi # Capture running unit content BEFORE daemon-reload (spec §7) RUNNING_UNITS="" for unit in fenris-collect.timer; do if systemctl is-active --quiet "${unit}" 2>/dev/null; then RUNNING_UNITS="${RUNNING_UNITS} ${unit}" fi done systemctl daemon-reload 2>/dev/null || true # Restart timer only if unit contents changed AND active for unit in ${RUNNING_UNITS}; do OLD_CONTENT="$(mktemp)" NEW_PATH="/usr/lib/systemd/system/${unit}" systemctl cat "${unit}" > "${OLD_CONTENT}" 2>/dev/null || true if ! diff -q "${OLD_CONTENT}" "${NEW_PATH}" > /dev/null 2>&1; then systemctl restart "${unit}" 2>/dev/null || true fi rm -f "${OLD_CONTENT}" done fi # sysusers, tmpfiles, daemon-reload (both fresh install and upgrade) systemd-sysusers || true systemd-tmpfiles --create || true systemctl daemon-reload || true ;; abort-upgrade|abort-install|disappear) ;; esac