#!/bin/bash
# -*- mode: sh; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# vim: et sts=4 sw=4

#  SPDX-License-Identifier: LGPL-2.1+
#
#  Copyright © 2020-2021 Collabora Ltd.
#  Copyright © 2020-2021 Valve Corporation.
#
#  This file is part of steamos-customizations.
#
#  steamos-customizations is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation; either version 2.1 of the License,
#  or (at your option) any later version.

set -e
set -o pipefail
set -u

usage() {
    cat <<EOF
Usage: ${0##*/} [--roothash=HASH] [--no-bootloaders] [--no-kernel]

Install the boot artifacts on your device.
EOF
}

while [[ "$#" -ne 0 ]]
do
    if [[ "$1" =~ ^(-h|--help)$ ]]
    then
        usage
        exit
    elif [[ "$1" =~ ^(--roothash)$ ]]
    then
        shift
        roothash="$1"
    elif [[ "$1" =~ ^(--no-bootloaders)$ ]]
    then
        no_bootloaders=1
    elif [[ "$1" =~ ^(--no-kernel)$ ]]
    then
        no_kernel=1
    else
        usage
        echo "$1: Too many arguments" >&2
        exit 1
    fi
    shift
done

if [[ ! "${no_bootloaders:-}" ]]
then
    typeset ident=$(steamos-bootconf this-image)
    # fallback if there are no bootconfs in /esp, so we can't determine our image name
    if [[ -z ${ident} ]]
    then
        rootdsk=$(findmnt --noheadings -o PARTLABEL / )
        ident=${rootdsk#rootfs-}
    fi

    if [[ -z ${ident} ]]
    then
        echo "Failed to determine new boot identifier"
        exit 1
    fi

    steamcl-install
    if mountpoint -q /boot
    then
        grub-install
    else
        echo "Warning: Skipping installation artifacts to /boot!" >&2
        grub-mkimage
    fi

    if [[ ! -e /efi/SteamOS ]]
    then
        mkdir -p /efi/SteamOS
    fi

    if [[ ! -e /esp/SteamOS/conf/${ident}.conf ]]
    then
        echo "Initializing boot configuration at /esp/SteamOS/conf for ${ident}"
        mkdir -p /esp/SteamOS/conf
        steamos-bootconf --conf-dir /esp/SteamOS/conf create --image ${ident}
    fi
fi

if [[ ! "${no_kernel:-}" ]]
then
    mapfile -t moduledirs < <(ls -1d /usr/lib/steamos/modules/* 2>/dev/null)
    for moduledir in ${moduledirs[@]}
    do
        if ! read -r pkgbase > /dev/null 2>&1 < "$moduledir/pkgbase"
        then
            # if the kernel has no pkgbase, we skip it
            continue
        fi

        kver="${moduledir##*/}"
        install -Dm644 "/usr/share/factory/var/lib/modules/$kver/vmlinuz" "/boot/vmlinuz-$pkgbase"
        mkdir -p "/var/lib/modules/$kver"
        cp -a "/usr/share/factory/var/lib/modules/$kver" "/var/lib/modules/"
        dracut --force --hostonly "/boot/initramfs-$pkgbase.img" "$kver"
        dracut --force --no-hostonly "/boot/initramfs-$pkgbase-fallback.img" "$kver"
    done
fi
if [[ "${roothash:-}" ]]
then
    mkdir -p /efi/SteamOS
    echo "$roothash" >/efi/SteamOS/roothash
fi
mapfile -t linux < <(ls -1 /boot/vmlinuz-* 2>/dev/null)
if [[ "${#linux[*]}" -eq 0 ]]
then
    echo "Warning: /boot: No such vmlinuz!" >&2
fi
mapfile -t initramfs < <(ls -1 /boot/initramfs-*.img 2>/dev/null)
if [[ "${#initramfs[*]}" -eq 0 ]]
then
    echo "Warning: /boot: No such initramfs!" >&2
fi
mapfile -t modules < <(ls -d1 /usr/lib/modules/* 2>/dev/null)
if [[ "${#modules[*]}" -eq 0 ]]
then
    echo "Warning: /usr/lib/modules: No such modules!" >&2
fi
update-grub
