#!/bin/sh
# chroot.sh - Chroot to a different environment while being able
# to access the internet and run X applications
# Written by: Ahmed S. Darwish under the license of your choice
#
export CHROOT_ENV=$CHROOT_DIR

# We need files under /dev for various block and character devices operations
sudo mount --bind /dev/ $CHROOT_ENV/dev/
# We need files /proc and /sys files for chrooted environment internet access
sudo mount --bind /proc/ $CHROOT_ENV/proc/
sudo mount --bind /sys/ $CHROOT_ENV/sys/

# Let the chrooted environment X apps see the host X windows session files
sudo mount --bind /tmp/ $CHROOT_ENV/tmp/

# Let our host X accept connections from all local clients (so from the chrooted env. too)
#xhost +
#sudo cp ~/.Xauthority $CHROOT_ENV/root/
#sudo cp ~/.ICEauthority $CHROOT_ENV/root/

# Do the chroot and global variable modification in a subshell not to affect the host session
(
export TERM="xterm"
export SHELL="/bin/bash"
export USER="root"
export USERNAME="root"
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin"
export PWD="/"
# Have the illusion of a first shell level
export SHLVL="1"
export HOME="/root"
export LOGNAME="root"
export DISPLAY=":0"
export XAUTHORITY="$HOME/.Xauthority"
export COLORTERM="$TERM"

sudo chroot "$CHROOT_ENV" /bin/bash -i
)
