#!/bin/sh
#
# mkinitrd - emulate mkinitrd with yaird for kernel-image installs.
#
# Copyright (C) 2001-2003 Herbert Xu <herbert@debian.org>
# Copyright (C) 2005 Erik van Konijnenburg <ekonijn@xs4all.nl>
#		- ripped out everything but the options.
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

PROG=mkinitrd.yaird
outfile=''

TEMP=`getopt -o d:km:o:r: --long supported-host-version:,supported-target-version: -n "$prg" -- "$@"`

# Check for non-GNU getopt
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

eval set -- "$TEMP"

while true ; do
	case "$1" in
	-d)
		echo "$PROG: $1 unsupported" >&2
		exit 1
		;;
	-k)
		echo "$PROG: $1 unsupported" >&2
		exit 1
		;;
	-m)
		# ignore
		shift 2
		;;
	-o)
		outfile="$2"
		shift 2
		;;
	-r)
		echo "$PROG: $1 unsupported" >&2
		exit 1
		;;
	--supported-host-version)
		supported_host_version="$2"
		shift 2
		;;
	--supported-target-version)
		supported_target_version="$2"
		shift 2
		;;
	--)
		shift
		break
		;;
	*)
		echo "Internal error!" >&2
		exit 1
		;;
	esac
done

if [ -n "$supported_host_version" ] || [ -n "$supported_target_version" ]; then
	if [ -n "$supported_host_version" ]; then
		host_upstream_version=${supported_host_version%%-*}
		if dpkg --compare-versions "$host_upstream_version" lt "2.6.8"; then
			echo "$PROG: host version too old" >&2
			exit 1
		fi
	fi
	if [ -n "$supported_target_version" ]; then
		target_upstream_version=${supported_target_version%%-*}
		if dpkg --compare-versions "$target_upstream_version" lt "2.6.8"; then
			echo "$PROG: target version too old" >&2
			exit 1
		fi
	fi
	exit 0
fi

if [ "$outfile" = "" ]; then
	echo "$PROG: no output file specified" >&2
	exit 1
fi
if [ $# -gt 1 ]; then
	echo "$PROG: extra arguments found" >&2
	exit 1
fi

# Note that version may be a pathname;
# this is used by the installer for Debian kernel image packages.
VERSION=$1
[ $# -gt 0 ] || unset VERSION
case $VERSION in
/lib/modules/*/[!/]*)
	;;
/lib/modules/[!/]*)
	VERSION=${VERSION#/lib/modules/}
	VERSION=${VERSION%%/*}
	;;
esac

case $VERSION in
*/*)
	echo "$PROG: $VERSION is not a valid kernel version" >&2
	exit 1
	;;
esac

VERSION="${VERSION-$(uname -r)}"

exec /usr/sbin/yaird --output "$outfile" "$VERSION"
