#!/bin/sh

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

SNKVER=$1
if [ -z "${SNKVER}" ]
then
	# Nothing on the command line
	exit 0
fi

if [ ! -f /.rw/snkernels/vmlinuz-${SNKVER} ]
then
	echo "Kernel binary /.rw/snkernels/vmlinuz-${SNKVER} missing!"
	exit 1
fi

if [ ! -d /.rw/snkernels/${SNKVER} ]
then
	echo "Modules directory /.rw/snkernels/${SNKVER} missing!"
	exit 1
fi

mkdir -pv /onappstore/initrd || exit 1
cd /onappstore/initrd || exit 1
zcat ../initrd.img | cpio -id
mkdir -pv modules/${SNKVER} || exit 1
tar -C /.rw/snkernels/${SNKVER} -cpf - . | tar -C modules/${SNKVER} -xpf -
find . | cpio -H newc --quiet -o | gzip -9 > ../initrd.img
cd .. || exit 1
rm -fr initrd

if [ -e /onappstore/vmlinuz-kvm ]
then
	cp -fv /.rw/snkernels/vmlinuz-${SNKVER} /onappstore/vmlinuz-${SNKVER}-kvm
fi

if [ -e /onappstore/vmlinuz-xen ]
then
	cp -fv /.rw/snkernels/vmlinuz-${SNKVER} /onappstore/vmlinuz-${SNKVER}-xen
fi
