#!/usr/bin/env bash
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is part of systemd.
#
# systemd 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.
#
# systemd 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 Lesser General Public License
# along with systemd; If not, see <http://www.gnu.org/licenses/>.

SKIP_REMAINING=77

usage()
{
    echo "Usage:"
    echo "  $0 [OPTIONS...] add KERNEL-VERSION KERNEL-IMAGE [INITRD-FILE ...]"
    echo "  $0 [OPTIONS...] remove KERNEL-VERSION"
    echo "Options:"
    echo "  -h,--help     Print this help"
    echo "  -v,--verbose  Increase verbosity"
}

dropindirs_sort()
{
    local suffix=$1; shift
    local -a files
    local f d i

    readarray -t files <<<"$(
        for d in "$@"; do
            for i in "$d/"*"$suffix"; do
                if [[ -e "$i" ]]; then
                    echo "${i##*/}"
                fi
            done
        done | sort -Vu
    )"

    for f in "${files[@]}"; do
        for d in "$@"; do
            if [[ -e "$d/$f" ]]; then
                echo "$d/$f"
                continue 2
            fi
        done
    done
}

export LC_COLLATE=C

for i in "$@"; do
    if [ "$i" == "--help" -o "$i" == "-h" ]; then
        usage
        exit 0
    fi
done

KERNEL_INSTALL_VERBOSE=0
if [ "$1" == "--verbose" -o "$1" == "-v" ]; then
    shift
    KERNEL_INSTALL_VERBOSE=1
fi
export KERNEL_INSTALL_VERBOSE

if [[ "${0##*/}" == 'installkernel' ]]; then
    COMMAND='add'
    # make install doesn't pass any parameter wrt initrd handling
    INITRD_OPTIONS=()
else
    COMMAND="$1"
    shift
    INITRD_OPTIONS=( "${@:3}" )
fi

KERNEL_VERSION="$1"
KERNEL_IMAGE="$2"

if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
    echo "Not enough arguments" >&2
    exit 1
fi

if [ -r "/etc/kernel/install.conf" ]; then
    . /etc/kernel/install.conf
elif [ -r "/usr/lib/kernel/install.conf" ]; then
    . /usr/lib/kernel/install.conf
fi

# Prefer to use an existing machine ID from /etc/machine-info or /etc/machine-id. If we're using the machine
# ID /etc/machine-id, try to persist it in /etc/machine-info. If no machine ID is found, try to generate
# a new machine ID in /etc/machine-info. If that fails, use "Default".

[ -z "$MACHINE_ID" ] && [ -f /etc/machine-info ] && source /etc/machine-info && MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
[ -z "$MACHINE_ID" ] && [ -f /etc/machine-id ] && read -r MACHINE_ID </etc/machine-id
[ -n "$MACHINE_ID" ] && [ -z "$KERNEL_INSTALL_MACHINE_ID" ] && echo "KERNEL_INSTALL_MACHINE_ID=$MACHINE_ID" >>/etc/machine-info
[ -z "$MACHINE_ID" ] && NEW_MACHINE_ID="$(systemd-id128 new)" && echo "KERNEL_INSTALL_MACHINE_ID=$NEW_MACHINE_ID" >>/etc/machine-info
[ -z "$MACHINE_ID" ] && [ -f /etc/machine-info ] && source /etc/machine-info && MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
[ -z "$MACHINE_ID" ] && MACHINE_ID="Default"

[ -z "$BOOT_ROOT" ] && for suff in "$MACHINE_ID" "loader/entries"; do
    for pref in "/efi" "/boot" "/boot/efi" ; do
        if [ -d "$pref/$suff" ]; then
            BOOT_ROOT="$pref"
            break 2
        fi
    done
done

[ -z "$BOOT_ROOT" ] && for pref in "/efi" "/boot/efi"; do
    if mountpoint -q "$pref"; then
        BOOT_ROOT="$pref"
        break
    fi
done
[ -z "$BOOT_ROOT" ] && BOOT_ROOT="/boot"


ENTRY_DIR_ABS="$BOOT_ROOT/$MACHINE_ID/$KERNEL_VERSION"

export KERNEL_INSTALL_MACHINE_ID="$MACHINE_ID"
export KERNEL_INSTALL_BOOT_ROOT="$BOOT_ROOT"

if [ -z "$layout" ]; then
    # Administrative decision: if not present, some scripts generate into /boot.
    if [ -d "$BOOT_ROOT/$MACHINE_ID" ]; then
        layout="bls"
    else
        layout="other"
    fi
fi


ENTRY_DIR_ABS="$BOOT_ROOT/$MACHINE_ID/$KERNEL_VERSION"

export KERNEL_INSTALL_MACHINE_ID="$MACHINE_ID"
export KERNEL_INSTALL_BOOT_ROOT="$BOOT_ROOT"
export KERNEL_INSTALL_LAYOUT="$layout"

[ "$layout" = "bls" ]
MAKE_ENTRY_DIR_ABS=$?


ret=0

readarray -t PLUGINS <<<"$(
    dropindirs_sort ".install" \
        "/etc/kernel/install.d" \
        "/usr/lib/kernel/install.d"
)"

case $COMMAND in
    add)
        if [[ ! "$KERNEL_IMAGE" ]]; then
            echo "Command 'add' requires an argument" >&2
            exit 1
        fi

        if [[ ! -f "$KERNEL_IMAGE" ]]; then
            echo "Kernel image argument ${KERNEL_IMAGE} not a file" >&2
            exit 1
        fi

        if [ "$MAKE_ENTRY_DIR_ABS" -eq 0 ]; then
            # Compatibility with earlier versions that used the presence of $BOOT_ROOT/$MACHINE_ID
            # to signal to 00-entry-directory to create $ENTRY_DIR_ABS
            # to serve as the indication to use or to not use the BLS
            if [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ]; then
                echo "+mkdir -v -p $ENTRY_DIR_ABS"
                mkdir -v -p "$ENTRY_DIR_ABS"
            else
                mkdir -p "$ENTRY_DIR_ABS"
            fi
        fi

        for f in "${PLUGINS[@]}"; do
            if [[ -x $f ]]; then
                [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
                    echo "+$f add $KERNEL_VERSION $ENTRY_DIR_ABS $KERNEL_IMAGE ${INITRD_OPTIONS[@]}"
                "$f" add "$KERNEL_VERSION" "$ENTRY_DIR_ABS" "$KERNEL_IMAGE" "${INITRD_OPTIONS[@]}"
                x=$?
                if [[ $x == $SKIP_REMAINING ]]; then
                    break
                fi
                ((ret+=$x))
            fi
        done
        ;;

    remove)
        for f in "${PLUGINS[@]}"; do
            if [[ -x $f ]]; then
                [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
                    echo "+$f remove $KERNEL_VERSION $ENTRY_DIR_ABS"
                "$f" remove "$KERNEL_VERSION" "$ENTRY_DIR_ABS"
                x=$?
                if [[ $x == $SKIP_REMAINING ]]; then
                    break
                fi
                ((ret+=$x))
            fi
        done

        if [ "$MAKE_ENTRY_DIR_ABS" -eq 0 ]; then
            [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && echo "Removing $ENTRY_DIR_ABS/"
            rm -rf "$ENTRY_DIR_ABS"
        fi
        ;;

    *)
        echo "Unknown command '$COMMAND'" >&2
        exit 1
        ;;
esac

exit $ret
