#! /bin/sh
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# Abort if any command returns an error value
set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

case "$1" in
	configure)
		# Make our version of a program available
		update-alternatives						\
		   --install /usr/bin/linux linux /usr/bin/linux.uml 10		\
		   --slave   /usr/share/man/man1/linux.1.gz linux.1.gz		\
			     /usr/share/man/man1/linux.uml.1.gz
	;;

	abort-upgrade|abort-remove|abort-deconfigure)
	;;

	*)	
		echo "postinst called with unknown argument \`$1'" >&2
		exit 1
	;;
esac

# Create the uml-net group.
#
# This is the snippet dh_installsysusers would have generated, with a guard
# around it.  It is called with --no-scripts in debian/rules, so it installs
# /usr/lib/sysusers.d/user-mode-linux.conf without also adding a hard
# dependency on systemd-sysusers to ${misc:Depends} -- that dependency is the
# reason for the flag, not a side effect of it.
#
# The group only matters for the optional tap networking integration, and
# user-mode-linux is fully usable without it, so systemd-sysusers is a
# Recommends.  A missing one must therefore not fail the install: we skip,
# and the admin can create the group by hand.  The shipped .conf is still
# declarative, so a systemd host creates the group at its next boot anyway.
if command -v systemd-sysusers >/dev/null 2>&1; then
	if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
		systemd-sysusers ${DPKG_ROOT:+--root="$DPKG_ROOT"} user-mode-linux.conf
	fi
fi

#DEBHELPER#

exit 0
