#!/bin/bash -x

set -e

export WHERE=$(pwd)

if [ $# -ne 1 ]; then
	echo "" >&2
	echo "Usage: $(basename $0) image_path" >&2
	echo "" >&2
	exit 1
fi
export IMAGE_PATH="$1"		# Created; not clobbered if exists

# The root of your ceph source tree, and its scripts subdirectory
CEPH_SRC_ROOT="/home/elder/ceph/ceph"
export CEPH_SCRIPT="${CEPH_SRC_ROOT}/src/script"

# Mount point to use for building up the image (deleted when done)
export IMAGE_ROOT="/tmp/ubuntu_image.$$"
# Size in MB of root image
export IMAGE_SIZE=1536	
# Information about the release to install
export IMAGE_RELEASE=precise	# Ubuntu release codename (lsb_release -sc)
export IMAGE_ARCH=amd64	# For now, "amd64" is all that works

# Create empty image file
[ -e "${IMAGE_PATH}" ] && echo ${IMAGE_PATH} exists && exit 1
dd if=/dev/zero of="${IMAGE_PATH}" bs=1M seek=$((IMAGE_SIZE - 1)) count=1
mkfs.ext3 -F "${IMAGE_PATH}"
mkdir -p "${IMAGE_ROOT}"

sudo -i -E <<!

set -x

cd "${WHERE}"

function cleanup() {
	umount "${IMAGE_ROOT}/proc"	|| true
	umount "${IMAGE_ROOT}/sys"	|| true
	umount "${IMAGE_ROOT}/dev/pts"	|| true
	umount "${IMAGE_ROOT}"
}
trap cleanup HUP INT TERM EXIT

mount -o loop "${IMAGE_PATH}" "${IMAGE_ROOT}"

debootstrap \
	--arch "${IMAGE_ARCH}" \
	"${IMAGE_RELEASE}" \
	"${IMAGE_ROOT}" \
	http://us.archive.ubuntu.com/ubuntu

echo "uml" > "${IMAGE_ROOT}/etc/hostname"

# Create an fstab file
cat >> "${IMAGE_ROOT}/etc/fstab" <<-EOF
	/dev/ubd0	/       	xfs     defaults     0 1
	none		/dev/pts	devpts	gid=5,mode=620	0 0
	proc		/proc		proc	defaults	0 0
	sysfs		/sys		sysfs	defaults	0 0
	none		/sys/kernel/debug debugfs defaults 0 0
	tmpfs		/tmp		tmpfs	defaults,size=768M	0 0
	none		/host		hostfs	defaults	0 0
EOF

# set up chroot
mkdir "${IMAGE_ROOT}/host"
mkdir -p "${IMAGE_ROOT}/dev/pts"
chgrp 5 "${IMAGE_ROOT}/dev/pts"
chmod 620 "${IMAGE_ROOT}/dev/pts"

mount -t devpts devptr "${IMAGE_ROOT}/dev/pts" -o gid=5,mode=620
mount -t proc proc "${IMAGE_ROOT}/proc"
mount -t sysfs sysfs "${IMAGE_ROOT}/sys"

# Create the device file for the root file system and configure permissions
mknod --mode=660 "${IMAGE_ROOT}/dev/ubd0" b 98 0
chown root:disk "${IMAGE_ROOT}/dev/ubd0"

# Create a hosts file
echo "127.0.0.1 localhost" > "${IMAGE_ROOT}/etc/hosts"

# set up network and a few other installation-related things
cp "${CEPH_SCRIPT}/network-from-cmdline" "${IMAGE_ROOT}/etc/init.d/"
chroot "${IMAGE_ROOT}" <<-EOF
	update-rc.d network-from-cmdline defaults 20

	# package repositories
	wget -q --no-check-certificate -O- \
		https://raw.github.com/ceph/ceph/master/keys/autobuild.asc |
	apt-key add -
	wget -q --no-check-certificate -O- \
		https://raw.github.com/ceph/ceph/master/keys/release.asc |
	apt-key add -
	echo deb http://us.archive.ubuntu.com/ubuntu precise universe \
		>> /etc/apt/sources.list
	echo deb http://ceph.com/debian/ "${IMAGE_RELEASE}" main \
		> /etc/apt/sources.list.d/ceph.list
	apt-get update

	# packages
	apt-get -y --force-yes install ssh libcrypto\+\+ bind9-host
	apt-get -y --force-yes install ceph

	# fix up consoles
	cat >> /etc/securetty <<-NESTED_EOF

		# uml
		tty0
		vc/0
	NESTED_EOF

	# no root password
	passwd -d root
EOF

# kcon_ helpers
cp "${CEPH_SCRIPT}/kcon_most.sh" "${IMAGE_ROOT}/root/most.sh"
cp "${CEPH_SCRIPT}/kcon_all.sh" "${IMAGE_ROOT}/root/all.sh"


# copy creating user's authorized_keys
mkdir "${IMAGE_ROOT}/root/.ssh"
chmod 700 "${IMAGE_ROOT}/root/.ssh"
cp ~/.ssh/authorized_keys "${IMAGE_ROOT}/root/.ssh/authorized_keys"
chmod 600 "${IMAGE_ROOT}/root/.ssh/authorized_keys"

# Cleanup
sync
!

# Cleanup
rmdir "${IMAGE_ROOT}"

exit 0
