#!/bin/bash -e
# Environment variables are set by the calling script

version="${1:-}"
bootopt=""

command -v update-initramfs >/dev/null 2>&1 || exit 0

# passing the kernel version is required
if [ -z "${version}" ]; then
    echo >&2 "W: initramfs-tools: ${DPKG_MAINTSCRIPT_PACKAGE:-kernel package} did not pass a version number"
    exit 2
fi

# exit if kernel does not need an initramfs
if [ "${INITRD:-}" = 'No' ]; then
    # delete initramfs entries in /boot/config.txt
    /bin/sed -i '/^initramfs /d' /boot/config.txt
    exit 0
fi


# there are two kernel types: with and without postfix "-v7+"
# (or apparently "-v8+" for Pi 3 or Compute Module 3, as per https://www.raspberrypi.org/documentation/configuration/config-txt/boot.md )
# we do nothing if the new kernel is not for the same kernel type then the current
currentversion="$(uname -r)"
currenttype="${currentversion: -4}"
currentkernelpostfix="$(echo "${currenttype}" | grep -Ec -- "-v[78]+" || true)"
newtype="${version: -4}"
if [ "$currentkernelpostfix" -gt 0 ] ; then
    # current kernel is of the postfixed variety, ignore new if the postfix doesn't match
    if [ "$newtype" != "$currenttype" ]; then
        exit 0
    fi
else
    # current kernel is the old variety without postfix, ignore new if that's postfixed
    newkernelpostfix="$(echo "${newtype}" | grep -Ec -- "-v[78]+" || true)"
    if [ "$newkernelpostfix" -gt 0 ]; then
        exit 0
    fi
fi
# absolute file name of kernel image may be passed as a second argument;
# create the initrd in the same directory
if [ -n "${2:-}" ]; then
    bootdir=$(dirname "$2")
    bootopt="-b ${bootdir}"
fi

# avoid running multiple times
if [ -n "${DEB_MAINT_PARAMS:-}" ]; then
    eval set -- "$DEB_MAINT_PARAMS"
    if [ -z "$1" ] || [ "$1" != "configure" ]; then
        exit 0
    fi
fi

# we're good - create initramfs.  update runs do_bootloader
INITRAMFS_TOOLS_KERNEL_HOOK=1 update-initramfs -c -t -k "${version}" ${bootopt} >&2

# delete initramfs entries in /boot/config.txt
/bin/sed -i '/^initramfs /d' /boot/config.txt

# insert initramfs entry in /boot/config.txt
INITRD_ENTRY="initramfs initrd.img-${version} followkernel"
echo >&2 $(basename "$0"): insert \'"$INITRD_ENTRY"\' into /boot/config.txt
/bin/sed -i "1i $INITRD_ENTRY" /boot/config.txt
