#!/bin/sh
set -e

# Author: Steve Langasek <steve.langasek@canonical.com>
#
# Mark as not-for-autoremoval those kernel packages that are:
#  - the currently booted version
#  - the kernel version we've been called for
#  - the latest kernel version (determined using rules copied from the grub
#    package for deciding which kernel to boot)
#  - the second-latest kernel version, if the booted kernel version is
#    already the latest and this script is called for that same version,
#    to ensure a fallback remains available in the event the newly-installed
#    kernel at this ABI fails to boot
# In the common case, this results in exactly two kernels saved, but it can
# result in three kernels being saved.  It's better to err on the side of
# saving too many kernels than saving too few.
#
# We generate this list and save it to /etc/apt/apt.conf.d instead of marking
# packages in the database because this runs from a postinst script, and apt
# will overwrite the db when it exits.
#
# June 2017: Modified for OSMC to take account of the unusual naming scheme
# adopted for OSMC kernel images and associated headers.
#
# Whereas most linux kernel images and header packages are named:
#   linux-image-<something> 
#   linux-headers-<something>
# OSMC prefixes the package with the kernel's architecture and removes the
# linux part. For example, packages for the Raspberry Pi 1 architecture are
# naned:
#   rbp1-image-<something>-osmc
#   rbp1-headers-<something>-osmc
# This naming standard breaks the standard Debian code, resulting in all but
# the most recently installed kernel and headers becoming autoremove candidates.
#
# The code obtains its list of OSMC architectures from the configuration
# parameter APT::OSMCKernelPackages, which can be found in 
# /etc/apt/apt.conf.d/01autoremove-osmc. Whenever a new machine architecture
# is added to OSMC that requires a different kernel, this file should be
# amended with details of the new package's prefixes.
#
# The original code has only been modified to accommodate the different kernel
# naming schemes.

# List of suitable kernels for removable is introduced by *-device-osmc, so
# check that we have that, or leave the cleanup for another day
#
# Jan 2018: Amended to allow for a quirk introduced on Debian stretch WRT the
# Vero4K. In stretch, package names from a foreign architecture have the 
# architecture appended to the package name when running "dpkg -l". 
# So while the Vero4K declares an architecture of arnhf, kernel packages are 
# built as arm64, which is treated as a foreign architecture.
#
# For example, on the Pi2/3 a kernel package will be named something like:
#   rbp2-image-4.9.29-13-osmc
# whereas on a Vero4K, under Debian stretch it will be something like:
#   vero364-image-3.14.29-51-osmc:arm64
#
# The workaround chosen is to add an addition field separator into an awk
# command so that spaces and a colon are both used as field separators.
# This will work as long as the package name does not contain any colons.

if [ ! -f /etc/apt/apt.conf.d/01autoremove-osmc ] ; then exit 0; fi

eval $(apt-config shell APT_CONF_D Dir::Etc::parts/d)
test -n "${APT_CONF_D}" || APT_CONF_D="/etc/apt/apt.conf.d"
config_file=${APT_CONF_D}/01autoremove-kernels-osmc

eval $(apt-config shell DPKG Dir::bin::dpkg/f)
test -n "$DPKG" || DPKG="/usr/bin/dpkg"

installed_version="$1"
running_version="$(uname -r)"


version_test_gt ()
{
	local version_test_gt_sedexp="s/[._-]\(pre\|rc\|test\|git\|old\|trunk\)/~\1/g"
	local version_a="`echo "$1" | sed -e "$version_test_gt_sedexp"`"
	local version_b="`echo "$2" | sed -e "$version_test_gt_sedexp"`"
	$DPKG --compare-versions "$version_a" gt "$version_b"
	return "$?"
}

# Remove hard-coding of kernel architectures for OSMC.
# Instead, it is derived from configuration parameter APT::OSMCKernelPackages.
# list="$(${DPKG} -l | awk '/^ii[ ]+(linux|kfreebsd|gnumach)-image-[0-9]+\./ && $2 !~ /-dbg$/ { print $2 }' | sed -e 's#\(linux\|kfreebsd\|gnumach\)-image-##')"

# First get a list of all OSMC image prefixes, eg rbp1-image, rbp2-image, vero364-image
image_list=$(apt-config dump --no-empty --format '%v%n' 'APT::OSMCKernelPackages' | grep ".*-image")

# Reformat the image list for use in regular expressions
image_regexp=$(echo $image_list | sed 's/ /\|/g')

# Get list of installed kernel images based on regular expression
# Jan 2018: Add additional field separator to awk command. Now uses (one or more) spaces or a single colon.
list=$(dpkg -l | egrep "^ii  *(${image_regexp})-[0-9][0-9]*.*-osmc$" | awk -F " +?|:" '{print $2}' | sed 's/.*-image-//')

latest_version=""
previous_version=""
for i in $list; do
	if version_test_gt "$i" "$latest_version"; then
		previous_version="$latest_version"
		latest_version="$i"
	elif version_test_gt "$i" "$previous_version"; then
		previous_version="$i"
	fi
done

if [ "$latest_version" != "$installed_version" ] \
   || [ "$latest_version" != "$running_version" ] \
   || [ "$installed_version" != "$running_version" ]
then
	# We have at least two kernels that we have reason to think the
	# user wants, so don't save the second-newest version.
	previous_version=
fi

kernels="$(echo "$latest_version
$installed_version
$running_version
$previous_version" | sort -u | sed -e 's#\.#\\.#g' )"

generateconfig() {
	cat <<EOF
// DO NOT EDIT! File autogenerated by $0
APT::NeverAutoRemove
{
EOF
	apt-config dump --no-empty --format '%v%n' 'APT::OSMCKernelPackages' | while read package; do
		for kernel in $kernels; do
			echo "   \"^${package}-${kernel}$\";"
		done
	done
	echo '};'
}
generateconfig > "${config_file}.dpkg-new"
mv "${config_file}.dpkg-new" "$config_file"
