#!/bin/sh
# Sets up (if necessary) and chroots into a different environment.
# Expects root privileges, does not drop them. 

# By Alan M Bruce (qole) with help from Benson Mitchell and Thomas Perl
#
# GPL licensed; keep code free!

# This script should have a wrapper to set up extra variables,
# OR, it can be run as a command:
# ezchroot <part/file/'none'> <chroot dir> <command> <params...>

if [ "`whoami`" != "root" ] ; then
  echo "please run me as root!"
  exit 9
fi

IMGFILE=$1
shift 1

CHROOT=$1
shift 1

# echo ezchroot $IMGFILE $CHROOT $*

qmount $IMGFILE $CHROOT
MOUNTSUCCESS=$?

if [ "$MOUNTSUCCESS" != "1" ] && [ "$MOUNTSUCCESS" != "0" ] ; then
  echo Cancelling chroot...
  exit $MOUNTSUCCESS
fi

if [ "$MOUNTSUCCESS" = "0" ] ; then
  #Make the tablet's devices available to the chroot
  echo . >/dev/stderr
  mount -o bind /dev "$CHROOT/dev"
  mount -o bind /proc "$CHROOT/proc"

  #Gentoo wiki says this will make X work
  echo .. >/dev/stderr
  mount -t devpts none "$CHROOT/dev/pts"
  mount -o bind /tmp "$CHROOT/tmp"

  #Open e-mail attachments, etc
  mount -o bind /var/tmp "$CHROOT/var/tmp"

  #Any external devices
  echo ... >/dev/stderr
  MNTD=`cat /proc/mounts | grep ' /media/' | awk '{print $2}'`

  for MDRV in $MNTD ; do
    if [ ! -d "$CHROOT$MDRV" ] ; then
      mkdir -p "$CHROOT$MDRV"
    fi
    mount -o bind "$MDRV" "$CHROOT$MDRV"
  done

  #Mount the user's home dir
  echo .... >/dev/stderr
  #mount -o bind /home/user "$CHROOT/home/user"

  # Do it the Fremantle way.
  mount /dev/mmcblk0p2 "$CHROOT/home"
  mount /dev/mmcblk0p1 "$CHROOT/home/user/MyDocs"
  
  #Make DBus work
  mount -o bind /var/run/dbus "$CHROOT/var/run/dbus"

  #Speed hacks: lower the priority of processes
  #renice 0 `pidof mmcqd`
  #renice 20 `pidof metalayer-crawler`

  # Sync the chroot if requested...
  if [ -f /home/user/.synchroot ] ; then 
    /sbin/synchroot $CHROOT
    rm /home/user/.synchroot
  fi

  # Place any commands you wish to run the first time you chroot
  # into the /var/run/onfirstchroot-ext.rc file (inside your rootfs)

  if [ -f "$CHROOT/var/run/onfirstchroot-ext.rc" ] ; then
    . "$CHROOT/var/run/onfirstchroot-ext.rc"
  fi

  # Place any commands you wish to run from inside the chroot 
  # the first time you chroot into the /var/run/onfirstchroot.rc
  # file (inside your rootfs)

  if [ -f "$CHROOT/var/run/onfirstchroot.rc" ] ; then
    chroot $CHROOT "/var/run/onfirstchroot.rc"
  fi

fi

# Place any commands you wish to run every time you chroot
# into the /var/run/onchroot-ext.rc file (inside your rootfs)

if [ -f "$CHROOT/var/run/onchroot-ext.rc" ] ; then
  . "$CHROOT/var/run/onchroot-ext.rc"
fi

# Place any commands you wish to run from inside the chroot
# every time you chroot into the /var/run/onchroot.rc
# file (inside your rootfs)

if [ -f "$CHROOT/var/run/onchroot.rc" ] ; then
  chroot $CHROOT "/var/run/onchroot.rc"
fi

#All set up. Set flag for next time...

if [ ! -d "$CHROOT/var/lock" ] ; then
  mkdir -p "$CHROOT/var/lock"
fi

trap "rm -f $CHROOT/var/lock/chroot-complete ; echo -ne '\033]0;osso_xterm\007' ; exit" INT TERM EXIT
echo $IMGFILE $@ > "$CHROOT/var/lock/chroot-complete"

#Custom prompt and xterm title. Reduces confusion.
CHRLABEL=`blkid -s LABEL $IMGFILE | cut -d' ' -f2 | cut -d'=' -f2 | sed 's/"//g'`
if [ "x$CHRLABEL" = "x" ] ; then
  CHRLABEL=chroot
fi
echo -ne "\033]0;$CHRLABEL\007" >/dev/stderr
export PS1="[\u@$CHRLABEL: \w]"

#Actually chroot
echo "Everything set up, running chroot..." >/dev/stderr
chroot $CHROOT "$@"

#All done, reset.
exit 0

