#!/bin/sh
# Sets up (if necessary) and chroots into a Debian environment.
# Expects root privileges, does not drop them. Look to /usr/bin/debbie for a
# friendly wrapper.

#Pull in the config, if possible...
[ -f /home/user/.chroot ] && . /home/user/.chroot

#This comes from the config file... If not, fall back on '/debian'
[ "x$CHROOT" != x ] || CHROOT=/debian

#Check for complete setup
if [ -f "$CHROOT/tmp/chroot-complete" ] ; then
  echo "chroot set up already!"
else
  echo "Setting up the chroot..."
  # if the file's already defined
  if [ "x$IMGFILE" != x ] ; then
    if [ -f "$IMGFILE" ] ; then
      LOOP=loop,
    else
      LOOP=
    fi
    if [ ! -f "$IMGFILE" ] && [ ! -b "$IMGFILE" ] ; then
      MSG1=`printf "ERROR!\n\nThe image specified ($IMGFILE) does not exist or is neither\na regular nor a block special file.\n\nPlease correct your /home/user/.chroot, or remove it for\nautodetection of Debian images on your memory cards."`
      if [ ! -f "/usr/bin/gxmessage" ] ; then
        echo $MSG1
      else
        gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "INVALID DISK IMAGE" "$MSG1"
      fi
      exit 9
    fi
  else
    #Definitely loopback mount, in absence of a search strategy for partitions
    LOOP=loop,
    IMGMMC="`ls -1 /media/mmc?/debian*.img* | head -1`"
    if [ "x$IMGMMC" != x ] ; then
      IMGFILE="$IMGMMC"
    else
      MSG1=`printf "ERROR!\n\nYou have no debian.img.ext2 file on your memory cards.\n\nPlease use the Debian Image Installer in Extras."`
      if [ ! -f "/usr/bin/gxmessage" ] ; then
        echo $MSG1
      else
        gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "MISSING DISK IMAGE" "$MSG1"
      fi
      exit 9
    fi
  fi
  if [ "x$LOOP" = x ] ; then
    echo "using image file: $IMGFILE"
  else
    echo "using device: $IMGFILE"
  fi
  if [ -d "/mnt/initfs/lib/modules/`uname -r`" ] ; then
    MODULEPATH="/mnt/initfs/lib/modules/`uname -r`"
  else
    MODULEPATH=/mnt/initfs/lib/modules/2.6.21-omap1
  fi
  insmod "$MODULEPATH/mbcache.ko"
  #We may have this from the config file; if not, use extension...
  #Note this fails on a partition, so always define it for partitions.
  if [ "x$IMGFS" = x ] ; then
    IMGFS=`echo $IMGFILE | awk -F '.' '{print $NF}'`
  fi
  #FIXME: not clean; what about other fs. Check /proc/filesystems, maybe...
  if [ "$IMGFS" != "ext3" ] && [ "$IMGFS" != "ext2" ] ; then
    echo "Don't know $IMGFS: Using ext2 file system"
    IMGFS=ext2
  fi
  echo "Using $IMGFS file system"
  if [ "$IMGFS" = ext3 ] ; then
    insmod "$MODULEPATH/jbd.ko"
  fi
  insmod "$MODULEPATH/$IMGFS.ko"

  if [ "$LOOP" = "loop," ] ; then
    echo "using $IMGFILE on the turbo-loop ;)"
    insmod /lib/modules/2.6.21-omap1/dm-mod.ko
    insmod /lib/modules/2.6.21-omap1/dm-loop.ko
    dmlosetup loop0 "$IMGFILE"
    mount -t "$IMGFS" /dev/dm-0 "$CHROOT" -o noatime
  else
    echo "using device: $IMGFILE"
    mount -t "$IMGFS" "$IMGFILE" "$CHROOT" -o ${LOOP}noatime
  fi

  #
  #Make the temp dir bigger for OpenOffice etc.
  #
  #This comes from the config file... 
  #If not, fall back to a 6MB /tmp dir
  #
  [ "x$TMPSIZE" != x ] || TMPSIZE=6M
  mount -o remount,size=$TMPSIZE /tmp

  #Make the tablet's devices available to Debian
  echo .
  mount -o bind /dev "$CHROOT/dev"
  mount -o bind /proc "$CHROOT/proc"

  #Gentoo wiki says this will make X work
  echo ..
  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 ...
  MNTD=`cat /proc/mounts | grep ' /media/' | awk '{print $2}'`
  for MDRV in $MNTD ; do
    if [ ! -d "$CHROOT$MDRV" ] ; then
      mkdir "$CHROOT$MDRV"
    fi
    mount -o bind "$MDRV" "$CHROOT$MDRV"
  done

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

  #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`

  #Use scripts to alternate boot/chroot files, if available
#  if [ -x "$CHROOT/root/bin/boottochroot.sh" ] ; then
#    "$CHROOT/root/bin/boottochroot.sh"
#  else
  # Sync the chroot if requested...
  if [ -f /home/user/.synchroot ] ; then 
    /sbin/synchroot
    rm /home/user/.synchroot
  fi
#  fi
fi

#All set up. Set flag for next time...
touch "$CHROOT/tmp/chroot-complete"

#Custom prompt and xterm title. Reduces confusion.
echo -ne "\033]0;Debian\007"
export PS1="[\u@Debian: \w]"

#Messy hack to make tap-and-hold work with GTK apps.
export GTK_MODULES=libgtkstylus.so

#Some OpenOffice environment variables
export SAL_USE_VCLPLUGIN="gtk"
export OOO_FORCE_DESKTOP="gnome"
export SAL_NOOPENGL="true"
export OOO_DISABLE_RECOVERY="true"
export SAL_DISABLE_SYNCHRONOUS_PRINTER_DETECTION="true"
#export SAL_DISABLE_CUPS="true"

#Actually chroot
echo "Everything set up, running chroot..."
chroot $CHROOT "$@"

#All done, reset.
echo -ne "\033]0;osso_xterm\007"
