#!/bin/sh
#
#  This script sets up the ParaVM to use pygrub
#

prefix=$1

#
#  Source our common functions
#
if [ -e /usr/lib/xen-tools/common.sh ]; then
    . /usr/lib/xen-tools/common.sh
else
    . ./hooks/common.sh
fi

echo SCRIPT = $0
dir=`dirname "$0"`


logMessage Script $0 starting

#
# Install the xen kernel.  linux-image-xen is in ubuntu,
# linux-image-xen-{amd64,686} in debian.
#

# First we need to figure out whether we're dealing with Debian or
# Ubuntu. Unfortunately, Debian doesn't install lsb_release by
# default, and its lsb-release package Recommends a bunch of stuff
installDebianPackage ${prefix} -o="Apt::Install-Recommends=false" lsb-release
if [ "Debian" = "$(chroot ${prefix} lsb_release -si)" ]; then
    # One of these exists in the amd64 repo, and one of them exists in
    # the i386 repo. The one that doesn't exist won't get installed
    installDebianPackage ${prefix} linux-image-xen-amd64
    installDebianPackage ${prefix} linux-image-xen-686
else
    installDebianPackage ${prefix} linux-image-xen
fi

installDebianPackage ${prefix} grub

#
# Make the /boot/grub directory
#
mkdir -p ${prefix}/boot/grub

#
# Create stock menu.lst
#
chroot ${prefix} /usr/sbin/update-grub -y

#
# Patches to add xen kernels, use serial console, etc.
#
for patch in $dir/patches/*; do
    patch -l -d ${prefix} -p1 < $patch
done

#
# Regenerate automagic kernels list
#
chroot ${prefix} /usr/sbin/update-grub -y

#
# Log our finish
#
logMessage Script $0 finished

