#!/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 "dtc-xen_domUconf_standard: Fatal Error: Cannot read file /usr/share/dtc-xen/dtc-xen-parse-param. Exiting ..." && exit 1
fi

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

ETC=${VPS_PATH}/etc

if [ "${XEN_DOMU_HDD_DEV_TYPE}" = "xvd" ] ; then
	part_dev=xvda
	swap_dev=xvdb
else
	part_dev=sda1
	swap_dev=sda2
fi

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

# Tweaks the /etc/inittab to use the console device instead of tty1
sed -i "s/tty1/console/" ${ETC}/inittab

# 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

if [ "${DISTRO}" = "debian" ] ; then
	if [ -f ${VPS_PATH}/etc/locale.gen ] ; then
		echo "Setting up Debian locale to en_US.UTF-8"
		TMP_FILE=`mktemp -t DTC_SET_LOCALE.XXXXXX` || exit 1
		grep -v "en_US.UTF-8" ${VPS_PATH}/etc/locale.gen >${TMP_FILE}
		cat <${TMP_FILE} >${VPS_PATH}/etc/locale.gen
		rm ${TMP_FILE}
		echo "en_US.UTF-8 UTF-8" >>${VPS_PATH}/etc/locale.gen
		chroot ${VPS_PATH} localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
		chroot ${VPS_PATH} locale-gen
	fi
fi

exit 0
