#!/bin/sh
# Copyright (c) 2020-2025 TUXEDO Computers GmbH <tux@tuxedocomputers.com>
#
# This file is part of TUXEDO CoreFix for Clevo NH5xAx.
#
# This file is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TUXEDO CoreFix for Clevo NH5xAx is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TUXEDO CoreFix for Clevo NH5xAx.  If not, see <https://www.gnu.org/licenses/>.
#
# This files borrows some codelines from
# amd64-microcode initramfs-tools hook script
# Copyright (C) 2012-2016 Henrique de Moraes Holschuh <hmh@debian.org>
# Released under the GPL v2 or later license
# included in Ubuntu 20.04.1

if ! grep -q NH5xAx "/sys/class/dmi/id/board_name"; then
    echo "fix not needed"
    exit 0
else
    PREREQ=""

    prereqs()
    {
        echo "$PREREQ"
    }

    case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
    esac

    . /usr/share/initramfs-tools/hook-functions

    # Begin real processing below this line

    WORKDIR=$(mktemp -d "${TMPDIR:-/var/tmp}/mkinitramfs-EFW_XXXXXXXXXX") || {
        echo "E: tuxedo-fix-xa15-core: cannot create temporary directory" >&2
        exit 1
    }
    ACPIDIR=${WORKDIR}/kernel/firmware/acpi
    DSTFILE=${WORKDIR}/tuxedo-fix-xa15-core.cpio

    mkdir -p "${ACPIDIR}" && \
    cp /lib/firmware/tuxedo-fix-xa15-core/nh5xax.aml "${ACPIDIR}" && \
    cd "${WORKDIR}" && \
    find kernel | cpio -H newc -o --quiet > "${DSTFILE}" && \
    prepend_earlyinitramfs "${DSTFILE}" || {
        # something failed somewhere in that pipeline
        [ -d "${WORKDIR}" ] && rm -rf "${WORKDIR}"
        echo "E: tuxedo-fix-xa15-core: failed to create or prepend the early initramfs to the initramfs" >&2
        exit 1
    }

    [ -d "${WORKDIR}" ] && rm -rf "${WORKDIR}"
fi
