#!/bin/sh

# 07gnucego-keyring — parte do projeto GNUcego.
# Copyright (C) 2026 GNUcego contributors
#
# Este programa é SOFTWARE LIVRE: você pode redistribuí-lo e/ou modificá-lo
# sob os termos da GNU General Public License (GPL) publicada pela
# Free Software Foundation — versão 3 da Licença, ou (à sua escolha)
# qualquer versão posterior.
#
# Este programa é distribuído na esperança de ser útil, mas SEM QUALQUER
# GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de
# ADEQUAÇÃO A UM DETERMINADO FIM. Veja a GNU General Public License
# para mais detalhes.
#
# Você deve ter recebido uma cópia da GNU General Public License junto
# com este programa. Se não, veja <https://www.gnu.org/licenses/>.

# Hook executado no post-base-installer.d, DEPOIS do install_kernel e ANTES
# do grub-installer. Prepara o TARGET:
# 1. instala a chave GNUcego + repo 'extra'
# 2. apt-get update (para o pick_kernel achar o linux-image-7.3.1-gnu)
# 3. garante initramfs-tools e GERA o initramfs do kernel linux-libre.

SRC=/usr/share/keyrings/gnucego-keyring.gpg

if [ ! -f "$SRC" ] || [ ! -d /target ]; then
    exit 0
fi

# 1) chave GNUcego no target
mkdir -p /target/etc/apt/trusted.gpg.d /target/usr/share/keyrings 2>/dev/null
cp "$SRC" /target/etc/apt/trusted.gpg.d/gnucego.gpg 2>/dev/null
cp "$SRC" /target/usr/share/keyrings/gnucego-keyring.gpg 2>/dev/null

# 2) componente 'extra' (kernel linux-libre, icecat, pentest) no sources.list
if [ -f /target/etc/apt/sources.list ]; then
    # pegar o codinome atual do sources (trixie/testing) e adicionar o 'extra'
    if grep -q "repo.gnucego.org .* extra" /target/etc/apt/sources.list 2>/dev/null; then
        : # ja tem o componente extra
    else
        # extrai o codinome da linha 'deb ... <codinome> main' e adiciona 'extra'
        CODENAME=$(grep -oE "repo.gnucego.org [a-z]+ main" /target/etc/apt/sources.list 2>/dev/null | head -1 | awk '{print $2}')
        if [ -n "$CODENAME" ]; then
            echo "deb http://repo.gnucego.org $CODENAME extra" >> /target/etc/apt/sources.list
        fi
    fi
else
    echo "deb http://repo.gnucego.org exu main extra" > /target/etc/apt/sources.list 2>/dev/null
fi

# 3) apt-get update no target
if [ -x /target/usr/bin/apt-get ]; then
    if [ -f /etc/resolv.conf ] && [ ! -s /target/etc/resolv.conf ]; then
        cp /etc/resolv.conf /target/etc/resolv.conf 2>/dev/null
    fi
    chroot /target apt-get update 2>/dev/null || true
fi

# 4) garantir initramfs-tools e gerar o initramfs do kernel linux-libre
if [ -x /target/usr/sbin/update-initramfs ]; then
    KVER=$(ls /target/boot/vmlinuz-* 2>/dev/null | head -1 | sed 's/.*vmlinuz-//')
    if [ -n "$KVER" ]; then
        chroot /target update-initramfs -u -k "$KVER" 2>/dev/null || true
    fi
else
    if [ -x /target/usr/bin/apt-get ]; then
        chroot /target apt-get install -y initramfs-tools 2>/dev/null || true
        KVER=$(ls /target/boot/vmlinuz-* 2>/dev/null | head -1 | sed 's/.*vmlinuz-//')
        [ -n "$KVER" ] && chroot /target update-initramfs -u -k "$KVER" 2>/dev/null || true
    fi
fi

exit 0
