# Maintainer: Sami Ahmed <sami@sami-ahmed.net>
# SamiOS branding package — logos, icons, boot themes, default configs
#
# Build: Copy the branding assets into this directory alongside the PKGBUILD,
#   then run `makepkg -f` from within it. Alternatively run the build helper
#   packaging/packages/samios-branding/fetch-assets.sh to pull them in from
#   the repo branding/ tree.
#
# ── Single-source-of-truth (SSOT) for VERSION ──────────────────────────────
# pkgver is sourced from ../../../VERSION (the single source of truth
# shared with the samios CLI, installer, profiledef, voice bridge, and
# desktop-setup). Arch PKGBUILD `pkgver` cannot contain hyphens
# (makepkg rejects them), so if the canonical VERSION has a prerelease
# suffix like "0.1.0-rc1", we rewrite the hyphen to an underscore
# ("0.1.0_rc1") for the Arch package version. The
# /usr/share/samios/version display file always uses the canonical
# string (with hyphen if present) for human reading.
#
# There is no SAMIOS_PKGVER_OVERRIDE escape hatch — the SSOT invariant
# is absolute. Downstream packagers who need a variant must edit
# ../../../VERSION (which is one file change in one place), then run
# `make version-sync && makepkg`. This is intentional: any build that
# diverges from VERSION must show up in git history at the canonical
# location, not as an undocumented environment override.

pkgname=samios-branding
# Resolve pkgver from ../../../VERSION (single source of truth). Arch
# PKGBUILD `pkgver` cannot contain hyphens (makepkg rejects them), so if
# the canonical VERSION has a prerelease suffix like "0.1.0-rc1", we
# rewrite the hyphen to an underscore ("0.1.0_rc1") for the Arch
# package version. The /usr/share/samios/version display file always
# uses the canonical string (with hyphen if present) for human reading.
#
# Use $startdir so the resolution path is stable when makepkg runs from
# $srcdir after unpacking the source tarball.
#
# If the canonical VERSION cannot be located, FAIL LOUDLY rather than
# silently emitting an "unknown" release package. The SSOT invariant is
# absolute — building a release without VERSION is a build error.
_samios_ver_path="$startdir/../../../VERSION"
if [ ! -r "$_samios_ver_path" ]; then
    _samios_ver_path="../../../VERSION"
fi
if [ ! -r "$_samios_ver_path" ]; then
    echo "FATAL: canonical VERSION file not found at \$startdir/../../../VERSION or ../../../VERSION" >&2
    echo "       SSOT invariant violated — refusing to build an unknown-version package" >&2
    exit 1
fi
_samios_ver="$(head -n1 "$_samios_ver_path")"
pkgver="${_samios_ver//-/_}"
unset _samios_ver _samios_ver_path
pkgrel=1
pkgdesc="SamiOS branding assets — logos, icons, GRUB theme, and default configuration"
arch=('any')
url="https://sami-ahmed.net"
license=('GPL3')
depends=('filesystem')
optdepends=('grub: for GRUB theme files')
install=samios-branding.install

# Source assets — these files live alongside the PKGBUILD in this directory.
# Run fetch-assets.sh to copy them from ../../branding/ before building.
source=(
    "samios-logo.png"
    "samios-logo-text.png"
    "samios-icon-256.png"
    "samios-icon-512.png"
    "samios-icon-1024.png"
    "samios-icon-launcher-256.png"
    "samios-icon-launcher-512.png"
    "samios-icon-launcher-1024.png"
    "samios-icon.svg"
)
sha256sums=(
    'd5fdf1dcfbba1f1b1e56d0325963ef540e7da3c0f6824672105a0480ba02b26d'  # samios-logo.png
    '45238639444b666c824e7abacb18bc69d4d1c6cc01b7a30d75862c2a52f82343'  # samios-logo-text.png
    'e8781728b5dad6c6047e360bd6f7189599d5e058097c980b5a26c2e3d7a00fd0'  # samios-icon-256.png
    '841be1020c53a7ef67d5807c67e484f29a1db4b92ced3b08082866dab8b6ebb2'  # samios-icon-512.png
    '13b4231b144e0bbedc4d8ab052d5dbcc2d5aab69c2854a9e681b75140715a72d'  # samios-icon-1024.png
    '39a02a848db661a3b2de29643802d781e9745bd209c9c067d4d62495c4f32954'  # samios-icon-launcher-256.png
    'cd3a73c8106e92abf7082638ee2d3193f40b0454b289e75292a3793ba04666b1'  # samios-icon-launcher-512.png
    '77234f406e6077b751a900a26c17eda280ef3345e56a4b6230efefa997416618'  # samios-icon-launcher-1024.png
    '011bece75f08db7795858052a2999e5acbf7196b79702cda087cbbbe205843f7'  # samios-icon.svg
)

package() {
    # Re-derive the canonical VERSION display string inside package() so
    # the installed /usr/share/samios/version matches what `samios
    # version` would print on a live system. (The top-level $pkgver has
    # been transformed to an Arch-valid pkgver; we want the original.)
    #
    # In makepkg's context, $startdir is the PKGBUILD directory and is
    # stable across the unpack/build cycle. Fall back to the relative
    # path for direct-sourcing (CI / test) contexts where $startdir is
    # unset.
    #
    # If we cannot find VERSION here, the top-level pkgver resolution
    # would have already failed and exited 1 — so reaching this function
    # implies VERSION exists. But we still guard against edge cases.
    _samios_ver_display_path="$startdir/../../../VERSION"
    if [ ! -r "$_samios_ver_display_path" ]; then
        _samios_ver_display_path="../../../VERSION"
    fi
    if [ ! -r "$_samios_ver_display_path" ]; then
        echo "FATAL: canonical VERSION not found in package()" >&2
        exit 1
    fi
    _samios_ver_display="$(head -n1 "$_samios_ver_display_path")"
    unset _samios_ver_display_path

    # === Directory structure ===
    install -d "${pkgdir}/usr/share/samios"
    install -d "${pkgdir}/usr/share/samios/logos"
    install -d "${pkgdir}/usr/share/samios/icons"
    install -d "${pkgdir}/usr/share/icons/hicolor/256x256/apps"
    install -d "${pkgdir}/usr/share/icons/hicolor/512x512/apps"
    install -d "${pkgdir}/usr/share/icons/hicolor/1024x1024/apps"
    install -d "${pkgdir}/usr/share/icons/hicolor/scalable/apps"
    install -d "${pkgdir}/usr/share/pixmaps"
    install -d "${pkgdir}/etc/samios"

    # === Logos (full lockups with face) ===
    install -m644 "${srcdir}/samios-logo.png"      "${pkgdir}/usr/share/samios/logos/samios-logo.png"
    install -m644 "${srcdir}/samios-logo-text.png"  "${pkgdir}/usr/share/samios/logos/samios-logo-text.png"

    # Copy logo to pixmaps for legacy lookups
    install -m644 "${srcdir}/samios-logo.png" "${pkgdir}/usr/share/pixmaps/samios-logo.png"

    # === Icons (pyramid-only, no face) ===
    # Square icons in hicolor theme
    install -m644 "${srcdir}/samios-icon-256.png"  "${pkgdir}/usr/share/icons/hicolor/256x256/apps/samios.png"
    install -m644 "${srcdir}/samios-icon-512.png"  "${pkgdir}/usr/share/icons/hicolor/512x512/apps/samios.png"
    install -m644 "${srcdir}/samios-icon-1024.png" "${pkgdir}/usr/share/icons/hicolor/1024x1024/apps/samios.png"

    # Scalable SVG icon
    install -m644 "${srcdir}/samios-icon.svg"      "${pkgdir}/usr/share/icons/hicolor/scalable/apps/samios.svg"

    # Launcher icons (rounded-square variant) stored under samios assets
    install -m644 "${srcdir}/samios-icon-launcher-256.png"  "${pkgdir}/usr/share/samios/icons/samios-icon-launcher-256.png"
    install -m644 "${srcdir}/samios-icon-launcher-512.png"  "${pkgdir}/usr/share/samios/icons/samios-icon-launcher-512.png"
    install -m644 "${srcdir}/samios-icon-launcher-1024.png" "${pkgdir}/usr/share/samios/icons/samios-icon-launcher-1024.png"

    # Also store source PNG icons in samios assets directory
    install -m644 "${srcdir}/samios-icon-256.png"  "${pkgdir}/usr/share/samios/icons/samios-icon-256.png"
    install -m644 "${srcdir}/samios-icon-512.png"  "${pkgdir}/usr/share/samios/icons/samios-icon-512.png"
    install -m644 "${srcdir}/samios-icon-1024.png" "${pkgdir}/usr/share/samios/icons/samios-icon-1024.png"
    install -m644 "${srcdir}/samios-icon.svg"      "${pkgdir}/usr/share/samios/icons/samios-icon.svg"

    # === Branding metadata ===
    # Display file uses the canonical VERSION verbatim (with hyphen if
    # any). The Arch package version is in $pkgver (hyphen rewritten to
    # underscore), and we want users reading /usr/share/samios/version
    # to see the original string, not the package version.
    cat > "${pkgdir}/usr/share/samios/version" <<EOF
SamiOS ${_samios_ver_display}
EOF

    cat > "${pkgdir}/usr/share/samios/branding.json" <<EOF
{
    "name": "SamiOS",
    "version": "${_samios_ver_display}",
    "color_primary": "#1424CE",
    "color_secondary": "#3B4ED8",
    "publisher": "Sami Ahmed",
    "website": "https://sami-ahmed.net"
}
EOF

    # === Default configuration ===
    cat > "${pkgdir}/etc/samios/samios.conf" <<EOF
# SamiOS configuration
SAMIOS_VERSION=${_samios_ver_display}
SAMIOS_COLOR_PRIMARY=#1424CE
SAMIOS_COLOR_SECONDARY=#3B4ED8
# Font policy: exclude fonts with 7777 in the name (personal branding)
SAMIOS_FONT_EXCLUDE_PATTERN=7777
EOF
}
