#!/bin/sh
# -*- mode: sh; tab-width: 8; encoding: utf-8-unix -*-

ROLE=testforge
# export PATH=$PATH:/usr/local/bin
MOUNTS="mnt/i mnt/j mnt/o"

. /usr/local/bin/usr_local_tput.bash
error () { retval=$1 ; shift; ERROR "$0" $* ; exit $retval ; }
info () { INFO " $0 " $* ; }

# must be run as root
[ "`id -u`" -ne "0" ] && error 1 "must be run as root"

if [ "$#" -eq "0" ] ; then
    error 2 "give an absolute directory name as argument"
fi
LARGS=""
CMD=""
while true; do
        case "$1" in
            '-'*)
		LARGS="$LARGS $1"
                shift
                ;;
            *)
                break
                ;;
        esac
done

root=$1
shift
if [ ! -d "$root" ] ; then
    error 3 "absolute directory name for chroot not found - $root"
  fi

# unix partition
[ -d $root/lost+found ] || exit 4
# linux partition
[ -e $root/usr/src/ ] || exit 5
cd $root || exit 6

if [ ! -d boot ] ; then
    error 7 "missing boot/"
  fi
for file in tmp usr/tmp var/tmp ; do
    [ -d $file ] && continue
    mkdir $file || error 8 " missing directory $file"
    chmod 1777 $file
  done
# df /var/tmp | grep -q sd.12 || mount /var/tmp

for file in proc sys dev dev/pts dev/shm usr ; do
    [ -d $file ] && continue
    mkdir $file || exit 9
    chmod 755 $file
  done

# Think -R is causing problems
[ -e proc/self ] || mount -t proc none proc || error 10
# this was rbind
[ -e sys/kernel ] || mount -o bind /sys sys || error 12
[ -e dev/null ] || mount -o bind /dev dev || error 11
[ -e /dev/pts/0 ] || \
    mount -t devpts  -o rw,relatime,mode=600 devpts dev/pts \
    || error 12
#? try mount| while read a on what type [ tmpfs cgroup binfmt_misc? ] without rbind
mount| while read what foo on bar type rest ; do
    # cgroup devpts devtmpfs ecryptfs ext2 fuseblk proc rpc_pipefs sysfs tmpfs vfat
    # leave real disks for $MOUNTS
    [ $type = 'fuse' -o  $type = 'ext2' -o  $type = 'ext4' -o  $type = 'vfat' -o ] && continue
    # have done these
    [ $type = 'proc' -o  $type = 'sys' -o  $type = 'dev' -o  $type = 'devpts' -o ] && continue
    DBUG "Dunno $what $on $type"
  done
# check for /dev/loop devices - up to 255 on android
[ -e /dev/loop1 ] || \
    ( cd /dev  && \
	    for i in 0 1 2 3 4 5 6 7 ; do
		[ -e loop$i ] && continue
		mknod loop$i b 7 $i
		chmod 660 loop$i
		chgrp disk loop$i
	    done )

for elt in $MOUNTS ; do
    [ -d $elt ] || { mkdir $elt ; chmod 755 $elt ; }
    grep -q /$elt /proc/mounts || continue
    [ -d $elt/tmp ] && continue
    grep -q $root/$elt /proc/mounts && continue
    mount --bind /$elt $root/$elt
  done

#   You'll also want to copy over resolv.conf in order to have proper DNS name
#      resolution from inside the chroot:
# but in chroot, you'll need to change this to your connected IP address.
cp -L /etc/resolv.conf etc || exit 16

for file in .bashrc .profile .jedrc ; do
    [ -f root/$file ] || \
      cp -p /root/$file root/$file
  done


EARGS="CHROOT=1 LANG=en_US.UTF-8 LC_COLLATE=C"
EELTS="$EELTS TERM DISPLAY HOME USER LOGNAME USERNAME PATH"
EELTS="$EELTS http_proxy https_proxy socks_proxy no_proxy"
for elt in $EELTS ; do
    EARGS="$EARGS `env|grep ^${elt}=`"
  done

# was /bin/bash -l
[ "$#" -eq 0 ] && set -- /bin/sh -i

# Now you can chroot into your new system. Use env before chroot to ensure that no
# environment variables from the installation media are used by your new system:
#?  PATH=$PATH
echo chroot $LARGS $root /usr/bin/env -i $EARGS "$@"
chroot $LARGS $root /usr/bin/env -i $EARGS "$@"
