#!/bin/sh

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

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

if [ ! -f /onappstore/${RDIMG}.img ]
then
        echo "Ramdisk image /onappstore/${RDIMG}.img missing!"
	exit 1
fi

mkdir -p /onappstore/initrd || exit 1
cd /onappstore/initrd || exit 1
zcat ../${RDIMG}.img | cpio -id &> /dev/null
mkdir -p root/.ssh || exit 1
chmod 700 root/.ssh || exit 1
for ((i = 2; i <= $#; i++)); do
        echo "${!i}" >> root/.ssh/authorized_keys
done
chmod 600 root/.ssh/authorized_keys
filename=`date +%s`
find . | cpio -H newc --quiet -o | gzip -9 > /tmp/${filename}
cd .. || exit 1
rm -fr initrd
echo "/tmp/${filename}"
