#!/bin/sh

# This script is shared by almost all Unix distributions, it's a good idea to call it.

if [ -r /usr/share/dtc-xen/dtc-xen-parse-param ]; then
       . /usr/share/dtc-xen/dtc-xen-parse-param
else
       echo "Fatal Error: Cannot read file /usr/share/dtc-xen/dtc-xen-parse-param. Exiting ..." && exit 1
fi

if [ "x$VPS_PATH" = "x" ]; then
       echo "Fatal Error: $VPS_PATH is not defined or empty. Exiting ..." && exit 1
fi

ETC=${VPS_PATH}/etc

# Setup the fstab
echo "/dev/sda1	/	ext3	errors=remount-ro	0 0
proc		/proc	proc	defaults		0 0
/dev/sda2	none	swap	sw			0 0
none		/dev/pts devpts	defaults		0 0
" >${ETC}/fstab

# Setup hostname and hosts
echo "mx.xen${VPSNUM}.${NODE_FQDN}" >${ETC}/hostname
echo "127.0.0.1	localhost.localdomain localhost
${FIRST_IP} mx.xen${VPSNUM}.${NODE_FQDN} dtc.xen${VPSNUM}.${NODE_FQDN} xen${VPSNUM}.${NODE_FQDN}

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
" >${ETC}/hosts

# Setup the devices
mkdir -p ${VPS_PATH}/dev/
echo "Making VPS devices with MAKEDEV generic, this WILL take a while..."
OLDPWDDIR=`pwd`
cd ${VPS_PATH}/dev
/sbin/MAKEDEV generic
cd ${OLDPWDDIR}

# Fix the /dev/ptmx and /dev/pts device and folder
rm -rf ${VPS_PATH}/dev/ptmx ${VPS_PATH}/dev/pts
mknod ${VPS_PATH}/dev/ptmx c 5 2
chmod 666 ${VPS_PATH}/dev/ptmx
mkdir ${VPS_PATH}/dev/pts

# If we run on a non-debian non-64 bits system, disable the tls folder
FOUNDED_ARCH=`uname -m`
if [ $FOUNDED_ARCH = "i386" -o $FOUNDED_ARCH = "i486" -o $FOUNDED_ARCH = "i586" -o $FOUNDED_ARCH = "i686" ] ; then
	if ! [ -f ${VPS_PATH}/etc/debian_version ] ; then
		if [ -d "${VPS_PATH}/lib/tls" ] ; then
			echo "Disabling lib/tls"
			mv ${VPS_PATH}/lib/tls ${VPS_PATH}/lib/tls.disabled
		fi
	fi
fi

# Setup the kernel
echo "Installing kernel and modules..."
if [ ! -e ${VPS_PATH}/lib/modules ]; then
	$MKDIR -p ${VPS_PATH}/lib/modules
fi
echo "cp -auxf ${KMOD_PATH} ${VPS_PATH}/lib/modules"
cp -auxf ${KMOD_PATH} ${VPS_PATH}/lib/modules
cp -L ${KERNELPATH} ${VPS_PATH}/boot
if [ ! -e ${VPS_PATH}/boot/vmlinuz ] ; then
	ln -s ${KERNELPATH} ${VPS_PATH}/boot/vmlinuz
fi
echo "chroot ${VPS_PATH} /sbin/depmod -a ${KERNEL_RELEASE}"
chroot ${VPS_PATH} /sbin/depmod -a ${KERNEL_RELEASE}

# Copy an eventual /etc/dtc-xen/authorized_keys2 file
if [ -f /etc/dtc-xen/authorized_keys2 ] ; then
	if [ ! -d "${VPS_PATH}/root/.ssh" ] ; then
		mkdir -p "${VPS_PATH}/root/.ssh"
		chmod 700 "${VPS_PATH}/root/.ssh"
	fi
	if [ -d "${VPS_PATH}/root/.ssh" -a ! -e "${VPS_PATH}/root/.ssh/authorized_keys2" ] ; then
		cp /etc/dtc-xen/authorized_keys2 "${VPS_PATH}/root/.ssh/authorized_keys2"
		chmod 600 "${VPS_PATH}/root/.ssh/authorized_keys2"
	fi
	if [ -d "${VPS_PATH}/root/.ssh" -a ! -e "${VPS_PATH}/root/.ssh/authorized_keys" ] ; then
		cp /etc/dtc-xen/authorized_keys2 "${VPS_PATH}/root/.ssh/authorized_keys"
		chmod 600 "${VPS_PATH}/root/.ssh/authorized_keys"
	fi
fi

# Customize the /root/.bashrc script
sed "s/VPS_HOSTNAME/xen${VPSNUM}.${NODE_FQDN}/" /etc/dtc-xen/bashrc >${VPS_PATH}/root/.bashrc

exit 0
