#!/bin/bash
# -*- coding: utf-8 -*-
#
# univention-prune-old-kernel
#
# Copyright 2011 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.
#

UPDATER_LOG="/var/log/univention/updater.log"
eval "$(univention-config-registry shell)" >>"$UPDATER_LOG" 2>&1

echo "--------------------------------------------" >>"$UPDATER_LOG" 2>&1
echo "Starting $0 at $(date)" >>"$UPDATER_LOG" 2>&1

echo "Package name of currently running kernel: linux-image-$(uname -r)"
echo

dependencyCheck () {
	# this function checks if deinstallation of $1 would remove any reverse dependencies
	# "apt-cache rdepends" displays any reverse dependency found in all packages files.
    # Since prior versions of current kernel meta packages also hold dependencies to older
    # kernel packages, a rdepends call would always return reverse dependencies.
	# Thus a "apt-cache depends" call is done for the reverse dependency. If the original
    # package is within the dependency list, we found a kernel meta package that would be
	# removed if the kernel package gets removed.
	local pkg="$1"
	local ok=""
	for rdep in $(LANG=C apt-cache rdepends "$pkg" | sed -rne 's/^[ ]+//p') ; do
		if dpkg -l "$rdep" 2>&1 | grep -q "^ii" ; then
			ok=""
			for dep in $(LANG=C apt-cache depends "$rdep" | sed -rne 's/^[ ]+Depends:[ ]//p') ; do
				if [ "$dep" = "$pkg" ] ; then
					ok="1"
				fi
			done
		fi
	done
	# function returns exitcode of last command
	[ -n "$ok" ]
}

export IFS=$'\n'
echo "Checking for old kernel packages..."
for pkg in $(COLUMNS=230 dpkg -l linux-image-\* 2>/dev/null | grep ^ii | awk '{ print $2 }' | grep -v "linux-image-$(uname -r)") ; do
	if dependencyCheck "$pkg" ; then
		echo
		echo "WARNING: $pkg seems to be a default kernel!"
		echo "WARNING: Removing this kernel package could prevent automatic"
		echo "WARNING: kernel updates during next release update!"
	fi
	echo -n "Remove $pkg? [yN] "
	read answer
	if [ "$answer" = "y" -o "$answer" = "Y" ] ; then
		echo -n "Removing kernel package $pkg ..."
		DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Options::=--force-confold -y --force-yes remove --purge "$pkg" >>"$UPDATER_LOG" 2>&1
		echo " done."
	fi
done
echo "done."

exit 0
