#!/bin/sh

set -e

# Modify the sudoers file
if grep xenusers /etc/sudoers 2>&1 >/dev/null ; then
	echo "Sudoers already modified"
else
	echo "Adding avaibility of dtc-xen_userconsole to xenusers in /etc/sudoers"
	echo "%xenusers       ALL= NOPASSWD: /usr/sbin/xm console xen*" >>/etc/sudoers
fi

chown root:xenusers /usr/bin/dtc-xen_userconsole
chmod -s /usr/bin/dtc-xen_userconsole

if ! [ -e /xen ] ; then
	ln -s /var/lib/dtc-xen/mnt /xen
fi

if grep "AllowTcpForwarding no" /etc/ssh/sshd_config ; then
	echo "Port forwarding seems to be disabled already!"
else
	if grep "AllowTcpForwarding" /etc/ssh/sshd_config ; then
		echo "There is a AllowTcpForwarding but not to no: please disable port forwarding NOW!"
	else
		echo "AllowTcpForwarding no" >>/etc/ssh/sshd_config
		echo "Disabling ssh port forwarding for security reasons"
		echo "Please type: \"killall -HUP sshd\" right after this package is setup!!!"
	fi
fi

if grep -q "/usr/bin/dtc-xen_userconsole" /etc/shells ; then
	echo "/etc/shells already knows /usr/bin/dtc-xen_userconsole"
else
	echo "Adding /bin/dtc-xen_userconsole to /etc/shells"
	echo "/usr/bin/dtc-xen_userconsole" >>/etc/shells
fi

FOUNDED_ARCH=`uname -m`
case "$FOUNDED_ARCH" in
	i386)
		BINARCH=i386
		;;
	i436)
		BINARCH=i386
		;;
	i586)
		BINARCH=i386
		;;
	i686)
		BINARCH=i386
		;;
	x86_64)
		BINARCH=amd64
		;;
	*)
		echo "Unrecognized arch: exiting!"
		exit 1
		;;
esac

APT_GET_PKGS=""
if [ ${BINARCH} = "amd64" ] ; then
	if [ ! -e /usr/share/dtc-xen-os/fedora-amd64 ]; then
		APT_GET_PKGS="${APT_GET_PKGS} dtc-xen-os-fedora-amd64"
	fi
	if [ ! -e /usr/share/dtc-xen-os/gentoo-amd64 ]; then
		APT_GET_PKGS="${APT_GET_PKGS} dtc-xen-os-gentoo-amd64"
	fi
	if [ ! -e /usr/share/dtc-xen-os/ubuntu-amd64 ]; then
		APT_GET_PKGS="${APT_GET_PKGS} dtc-xen-os-ubuntu-amd64"
	fi
	if [ ! -e /usr/share/dtc-xen-os/netbsd5-amd64 ]; then
		APT_GET_PKGS="${APT_GET_PKGS} dtc-xen-os-netbsd5-amd64"
	fi
else
	if [ ! -e /usr/share/dtc-xen-os/fedora-x86 ]; then
		APT_GET_PKGS="${APT_GET_PKGS} dtc-xen-os-fedora-x86"
	fi
	if [ ! -e /usr/share/dtc-xen-os/gentoo-i686 ]; then
		APT_GET_PKGS="${APT_GET_PKGS} dtc-xen-os-gentoo-i686"
	fi
	if [ ! -e /usr/share/dtc-xen-os/ubuntu-x86 ]; then
		APT_GET_PKGS="${APT_GET_PKGS} dtc-xen-os-ubuntu-x86"
	fi
	if [ ! -e /usr/share/dtc-xen-os/netbsd5-i386 ]; then
		APT_GET_PKGS="${APT_GET_PKGS} dtc-xen-os-netbsd5-i386"
	fi
fi

echo "You should now add one of the following Debian repository:
Main mirror:   deb ftp://ftp.gplhost.com/debian stable main
Europe mirror: deb ftp://ftp.gplhost.fr/debian stable main
Asia mirror:   deb ftp://ftp.gplhost.sg/debian stable main
and do:
apt-get install ${APT_GET_PKGS}"

exit 0
