#!/bin/sh
# systemd generator to bind per-slot /var based on the persistent by-slot alias

set -eu

OUT_DIR="/run/systemd/generator"
mkdir -p "$OUT_DIR"

# fsck unit
path=$(systemd-escape --path "/dev/disk/by-slot/persistent")
FSCK_UNIT="systemd-fsck@${path}.service"

# 1) Determine current slot
SLOT=""
ROOT_DEV="$(findmnt -no SOURCE / || true)"
if [ -n "$ROOT_DEV" ]; then
  PARTLABEL="$(blkid -s PARTLABEL -o value -- "$ROOT_DEV" 2>/dev/null || true)"
  case "$(printf '%s' "$PARTLABEL" | tr '[:upper:]' '[:lower:]')" in
    system_a) SLOT="system_a" ;;
    system_b) SLOT="system_b" ;;
  esac
fi

# Optional fallback if label is missing/unexpected
: "${SLOT:=${SLOT_FALLBACK:-system_a}}"

VAR_SRC="/persistent/slots/${SLOT}/var"

# 2) Emit persistent.mount (mount the shared persistent partition by label)
# Mount with:
# noatime: avoids atime writes entirely (better than relatime)
# lazytime: defer inode timestamps to memory, flushed with fsync/mtime changes / 24h.
# commit=60: batches journal commits (fewer writes, risk up to 60s metadata loss on power loss).
# errors=remount-ro: safer failure mode
cat >"$OUT_DIR/persistent.mount" <<EOF
[Unit]
Description=Persistent data partition
Before=local-fs.target
Conflicts=umount.target
Requires=${FSCK_UNIT}
After=${FSCK_UNIT}

[Mount]
What=/dev/disk/by-slot/persistent
Where=/persistent
Type=ext4
Options=rw,noatime,lazytime,commit=60,errors=remount-ro

[Install]
WantedBy=local-fs.target
EOF

# 3) Emit fsck settings
fsck_dir="/run/systemd/system/${FSCK_UNIT}.d"
mkdir -p "$fsck_dir"
cat >"$fsck_dir/repair.conf" <<EOF
[Service]
# Automatic non-interactive repair always
Environment=SYSTEMD_FSCK_FORCE=yes
Environment=SYSTEMD_FSCK_REPAIR=yes
EOF

# 4) Emit var.mount (bind per-slot /var)
cat >"$OUT_DIR/var.mount" <<EOF
[Unit]
Description=Per-slot /var for ${SLOT}
Requires=persistent.mount
After=persistent.mount
Before=local-fs.target
ConditionPathExists=${VAR_SRC}

[Mount]
What=${VAR_SRC}
Where=/var
Type=none
Options=bind

[Install]
WantedBy=local-fs.target
EOF
