#!/bin/bash

# Mount filesystems then chroot

MP=${1}

if [ -z "${MP}" ]; then
	echo "No mount point given"
	exit 1
fi

if [ ! -d ${MP} ]; then
	echo "${MP} is not a directory"
	exit 1
fi

mount -o bind /proc ${MP}/proc
mount -o bind /dev ${MP}/dev
mount -o bind /dev/pts ${MP}/dev/pts
mount -o bind /dev/shm ${MP}/dev/shm
mount -o bind /sys ${MP}/sys 

shift

test -d ${MP}/etc && cp /etc/resolv.conf ${MP}/etc/resolv.conf

if [ $# -ne 0 ]; then
chroot ${MP} $@
else
chroot ${MP} /bin/bash -l
fi

umount ${MP}/sys
umount ${MP}/dev/shm
umount ${MP}/dev/pts
umount ${MP}/dev
umount ${MP}/proc
#umount ${MP}
