#!/bin/sh -efu

PROG="${0##*/}"
PROG_VERSION="0.8.4"

. shell-error
. shell-getopt
. shell-args

show_help()
{
	cat <<-EOF
	Usage: $PROG [options] <initrd-image> <kernel-version>

	Options:
	  --fstab=FILE       use FILE instead of /etc/fstab;
	  -f, --force        force initrd image creation;
	  -v, --verbose      print a message for each action;
	  -V, --version      print program version and exit;
	  -h, --help         show this text and exit.

	Report bugs to http://bugzilla.altlinux.org/

	EOF
	exit
}

print_version()
{
	cat <<-EOF
	$PROG version $PROG_VERSION
	Written by Alexey I. Froloff <raorn@altlinux.org>

	Copyright (C) 2009  Alexey I. Froloff <raorn@altlinux.org>
	This is free software; see the source for copying conditions.  There is NO
	warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	EOF
	exit
}

TEMP=`getopt -n $PROG -o a:,f,V,v,d,h -l type:,fstab:,preload:,with:,extra:,with-raid,omit-scsi-modules,omit-ide-modules,omit-raid-modules,pause,nocompress,nobootsplash,strict,image-version,ifneeded,version,add:,force,verbose,debug,help -- "$@"` ||
	show_usage
eval set -- "$TEMP"

FORCE=
while :; do
	case "$1" in
		--fstab) shift
			export FSTAB="$1"
			;;
		-v|--verbose)
			export VERBOSE=1
			;;
		-a|--type|--preload|--with|--extra|--add)
			message "Ignoring mkinitrd option \`$1=$2'..."
			shift
			;;
		-d|--with-raid|--omit-scsi-modules|--omit-ide-modules|--omit-raid-modules|--pause|--nocompress|--nobootsplash|--strict|--image-version|--ifneeded|--debug)
			message "Ignoring mkinitrd option \`$1'..."
			;;
		-f|--force)
			FORCE=1
			;;
		-V|--version)
			print_version
			;;
		-h|--help)
			show_help
			;;
		--)
			shift
			break
			;;
	esac
	shift
done

[ $# -eq 2 ] ||
	show_usage

export IMAGEFILE="$1"

if [ -f "$IMAGEFILE" -a -z "$FORCE" ]; then
	message "$IMAGEFILE already exists."
	exit 1
fi

exec make-initrd -k "$2"
