#!/usr/bin/env bash
# Installed as /usr/lib/bluerune/os-release, run by the pacman hook bluerune-os-release.hook
# whenever Arch's /usr/lib/os-release is installed or changes: writes /etc/os-release as
# Arch's with BlueRune's name, so the system calls itself "BlueRune (Arch Linux)" while
# staying Arch (ID=arch) for everything that checks. systemd reads /etc/os-release before
# /usr/lib/os-release; Arch makes /etc/os-release a link to it only when it is missing
# (tmpfiles), so a file there stays.
#
#   os-release [<Arch's os-release> [<file to write>]]
set -euo pipefail

src=${1:-/usr/lib/os-release}
out=${2:-/etc/os-release}

# brand <os-release>: the same lines without the ones BlueRune sets, then BlueRune's.
brand() {
    grep -Ev '^(PRETTY_NAME|VARIANT|VARIANT_ID|LOGO)=' "$1" || true
    printf '%s\n' 'PRETTY_NAME="BlueRune (Arch Linux)"' 'VARIANT="BlueRune"' 'VARIANT_ID=bluerune' 'LOGO=bluerune'
}

tmp=$(mktemp "$out.XXXXXX")
trap 'rm -f "$tmp"' EXIT
brand "$src" >"$tmp"
chmod 644 "$tmp"
# A rename: the link Arch made is replaced by the file, never written through.
mv -f "$tmp" "$out"
trap - EXIT
