#!/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
#
# GPL licensed; keep code free!

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

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

IMGFILE=$1
MNTPT=$2

echo qmount $IMGFILE $MNTPT

#Ensure that we have an image or partition to mount

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\nFirst parameter must be an image file or partition"`
  if [ ! -f "/usr/bin/gxmessage" ] ; then
    echo $MSG1
  else
    gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
  fi
  exit 9
fi

#Ensure that we have a chroot directory to mount the image or partition on

if [ "x$MNTPT" = "x" ] || [ "x`echo $MNTPT | grep '/'`" = "x"  ] ; then
  MSG1=`printf "ERROR!\n\nNo chroot directory specified!\n\nSecond parameter must be chroot dir (eg. /debian)"`
  if [ ! -f "/usr/bin/gxmessage" ] ; then
    echo $MSG1
  else
    gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
  fi
  exit 9
fi

#Check to see if already mounted
if [ -f "$MNTPT/var/lock/qmount-complete" ] ; then
  echo "$MNTPT has a qmount already!"
  MTDIMGFILE=`cat $MNTPT/var/lock/qmount-complete`
  if [ "$IMGFILE" != "$MTDIMGFILE" ] ; then
    echo $MTDIMGFILE already mounted here! 
    MSG1=`printf "Mount problem!\n\n$MTDIMGFILE already mounted on $MNTPT"`
       if [ ! -f "/usr/bin/gxmessage" ] ; then
         echo $MSG1
       else
         gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
       fi
    exit 9
    # Instead of failing, we could unmount instead...
    # echo Unmounting...
    # closechroot $MNTPT
  else
    echo $MTDIMGFILE already mounted on $MNTPT...
    exit 1
  fi
fi 

if [ ! -f "$MNTPT/var/lock/qmount-complete" ] ; then
 echo "Mounting..."
 if [ "$IMGFILE" != "none" ] ; then

   if [ -f "$IMGFILE" ] ; then
     LOOP=loop,
     echo "using image file: $IMGFILE"
     if [ "x$IMGFS" = x ] ; then
       IMGFS=`echo $IMGFILE | awk -F '.' '{print $NF}'`
       echo "fs type is $IMGFS"
     fi
   else
     LOOP=
     echo "using device: $IMGFILE"
     PARTINFO="`blkid -s TYPE $IMGFILE`"
     if [ "x$IMGFS" = x ] ; then
       IMGFS=`echo $PARTINFO | awk '{print $NF}' | awk -F '=' '{print $NF}' | sed s/\"//g`
     fi
   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" 2>/dev/null

   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" 2>/dev/null
   fi

   insmod "$MODULEPATH/$IMGFS.ko" 2>/dev/null

   if [ "$LOOP" = "loop," ] ; then
    echo "mounting $IMGFILE on the turbo-loop ;)"
     insmod /lib/modules/2.6.21-omap1/dm-mod.ko 2>/dev/null
     insmod /lib/modules/2.6.21-omap1/dm-loop.ko 2>/dev/null
     NEXTLOOP=`ls -l /dev/dm-* 2>/dev/null | tail -1 | awk '{print $NF}' | awk -F '-' '{print $NF+1}'`
     if [ "x$NEXTLOOP" = "x"  ] ; then
       NEXTLOOP=0
     fi
     DMLOMSG=`dmlosetup loop$NEXTLOOP "$IMGFILE" 2>&1`
     MNTMSG=`mount -t "$IMGFS" /dev/dm-$NEXTLOOP "$MNTPT" -o noatime 2>&1` 
     if [ "$?" != 0 ] ; then
       MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on loop$NEXTLOOP\n\n$DMLOMSG\n$MNTMSG"`
       if [ ! -f "/usr/bin/gxmessage" ] ; then
         echo $MSG1
       else
         gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
       fi
       exit 2
     fi
     echo ...$IMGFILE mounted on loop$NEXTLOOP
   else
     echo "mounting device: $IMGFILE"
     if ! mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o ${LOOP}noatime ; then
       MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on $MNTPT"`
       if [ ! -f "/usr/bin/gxmessage" ] ; then
         echo $MSG1
       else
         gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
       fi
       exit 3
     fi

   fi

  else
    echo "Not mounting any filesystem, chroot is $MNTPT"
  fi 

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

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

 # Place any commands you wish to run the first time you mount
 # into the $MNTPT/var/run/onmount.rc file

  if [ -f "$MNTPT/var/run/onmount.rc" ] ; then
    . "$MNTPT/var/run/onmount.rc"
  fi

 echo $IMGFILE > "$MNTPT/var/lock/qmount-complete"
 exit 0

fi

exit 1