#!/bin/bash
# /etc/kernel/postinst.d/zzz-synex-boot-redundancy
# Sync boot partitions to redundant disks after kernel installation

set -e

# Only run if synex-boot-redundancy is available
command -v synex-boot-redundancy >/dev/null 2>&1 || exit 0

# Only run on ZFS systems with an active pool
command -v zpool >/dev/null 2>&1 || exit 0
zpool list >/dev/null 2>&1 || exit 0

# Only run if pool has more than one disk (redundancy)
POOL=$(zpool list -H -o name | head -n1)
DISK_COUNT=$(zpool status "$POOL" 2>/dev/null | grep -cE '^\s+(sd|vd|nvme|hd)[a-z0-9]+' || echo 0)
[[ "$DISK_COUNT" -gt 1 ]] || exit 0

echo "Syncing boot partitions to redundant disks..."
synex-boot-redundancy sync 2>&1 | logger -t synex-boot-redundancy || true

exit 0
