#!/bin/sh
#http://www.debian.org/releases/stable/i386/apds03.html.en

#SRC=/root/debootstrap.d
#SRCHOSTNAME=moo17
#HOSTNAME=debootstrap-test
#pick a hostname!

HOSTNAME=moo19
DIST=etch
IP=18.181.0.186

PARTITIONS=/root/neboat/testpart

VOLUME=/dev/mapper/xenvg-d_${HOSTNAME}_hda

###
###  Create the device for SIPB-Xen purposes
###
sfdisk -H 255 -S 63 $VOLUME <$PARTITIONS
kpartx -a $VOLUME
#SWAPDEV=$(sipb-xen-losetup $VOLUME $(( 7903980 * 512 )))
#IMAGEDEV=$(sipb-xen-losetup $VOLUME $(( 63 * 512 )))
# mkfs.ext3 -b 1024 $IMAGEDEV $(( 7903917 / 2 ))

#CREATE_IMAGE_CONFIG=./paravm-create.conf

#xen-create-image --image-dev $IMAGEDEV --swap-dev $SWAPDEV --fs ext3 --config $CREATE_IMAGE_CONFIG \
#	--dist $DIST --hostname $HOSTNAME

# xen-create-image --partitions sipb-xen --memory=256MB --ide --arch amd64 --dist etch --hostname moo19 --mirror http://debian.lcs.mit.edu/debian --ip=18.181.0.186 --netmask=255.255.0.0 --gateway=18.181.0.1 --cache

xen-create-image --image-dev ${VOLUME}1 --swap-dev ${VOLUME}2 --fs ext3 --ide --memory 256M --arch amd64 --dist etch --hostname $HOSTNAME --mirror http://debian.lcs.mit.edu/debian --ip=$IP --netmask=255.255.0.0 --gateway=18.181.0.1 --cache

kpartx -d $VOLUME
#losetup -d $SWAPDEV
#losetup -d $IMAGEDEV

### This should be the end of the script
### The material past this point is solely for reference purposes


# time debootstrap --arch amd64 etch "$DEST" http://debian.lcs.mit.edu/debian

# cp -a $SRC/fstab $DEST/etc/fstab
# cp -a $SRC/interfaces $DEST/etc/network/interfaces
# cp -a $SRC/sources.list $DEST/etc/apt/sources.list
# cp -a $SRC/update-grub.diff $DEST/root/update-grub.diff
# echo $HOSTNAME > $DEST/etc/hostname
# sed s/$SRCHOSTNAME/$HOSTNAME/g $SRC/hosts > $DEST/etc/hosts

# mount -t proc proc $DEST/proc

# LANG=C chroot "$DEST" <<ENDCHROOT

# export TERM=xterm-color
# mount -a

# cd /media
# mkdir cdrom0
# ln -s cdrom0 cdrom
# cd /
# ln -s media/cdrom

# echo $'y\n3\nEastern' | tzconfig
# export DEBIAN_FRONTEND=noninteractive

# echo 'locales locales/locales_to_be_generated multiselect     en_US ISO-8859-1' | debconf-set-selections


# aptitude update
# aptitude -y install locales linux-image-xen-amd64
# aptitude --without-recommends -y install ~pstandard ~prequired ~pimportant
# #strace tasksel install standard 2>&1 | less
# aptitude clean

# mkdir /boot/grub
# aptitude install grub
# patch /usr/sbin/update-grub </root/update-grub.diff
# update-grub
# patch /boot/grub/menu.lst </root/menu.lst.diff
# patch /etc/inittab </root/inittab.diff

# rm /root/update-grub.diff
# ENDCHROOT

# ## kill atd and rpc.statd that are using $DEST
# umount $DEST/proc
# umount $DEST
# losetup -d $LODEV
