#!/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!

# Hacked roughly to work with Fremantle. Help always appreciated. I can be found on talk.maemo.org.

# This script should have a wrapper to set up extra variables,
# OR, it can be run as a command:
# qmount <partition/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 >/dev/stderr
  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 >/dev/stderr
  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!" >/dev/stderr
  MTDIMGFILE=`cat $MNTPT/var/lock/qmount-complete`
  if [ "$IMGFILE" != "$MTDIMGFILE" ] ; then
    echo $MTDIMGFILE already mounted here! >/dev/stderr
    MSG1=`printf "Mount problem!\n\n$MTDIMGFILE already mounted on $MNTPT"`
       if [ ! -f "/usr/bin/gxmessage" ] ; then
         echo $MSG1 >/dev/stderr
       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... >/dev/stderr
    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" >/dev/stderr
     if [ "x$IMGFS" = x ] ; then
       IMGFS=`echo $IMGFILE | awk -F '.' '{print $NF}'`
       echo "fs type is $IMGFS" >/dev/stderr
     fi
   else
     LOOP=
     echo "using device: $IMGFILE" >/dev/stderr
     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

   # missing in Fremantle...
   # modprobe mbcache

   if [ "$IMGFS" != "ext3" ] && [ "$IMGFS" != "ext2" ] ; then
     echo "Don't know $IMGFS: Using ext2 file system" >/dev/stderr
     IMGFS=ext2
   fi

   echo "Using $IMGFS file system"
   if [ "$IMGFS" = ext3 ] ; then
     #insmod "$MODULEPATH/jbd.ko" 2>/dev/null
     modprobe jbd
   fi

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

   if [ -d "/lib/modules/`uname -r`" ] ; then
     MODULEPATH="/lib/modules/`uname -r`"
   else
     MODULEPATH=/lib/modules/2.6.28-omap1
   fi

   if [ "$LOOP" = "loop," ] ; then

   # Check for dm-loop kernel module and dmlosetup command. 
   # If found, then use dm-loop instead of regular loop.

     if [ -f "$MODULEPATH/dm-loop.ko" ] && [ -f "/sbin/dmlosetup" ]; then

       # use dm-loop

       echo "mounting $IMGFILE on the turbo-loop ;)"
       #insmod $MODULEPATH/dm-mod.ko 2>/dev/null
       modprobe dm-mod
       #insmod $MODULEPATH/dm-loop.ko 2>/dev/null
       modprobe dm-loop

       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,nobh,nodiratime 2>&1` 

     else

       # use "regular" loop

       echo "mounting $IMGFILE on loop" >/dev/stderr
       NEXTLOOP=""
       if [ "x$NEXTLOOP" = "x"  ] ; then
         NEXTLOOP=0
       fi

       DMLOMSG=""
       MNTMSG=`mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o loop,noatime,nobh,nodiratime 2>&1` 

     fi

     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 >/dev/stderr
       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 >/dev/stderr
   else
     echo "mounting device: $IMGFILE" >/dev/stderr
     if ! mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o ${LOOP}noatime,nobh,nodiratime ; then
       MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on $MNTPT"`
       if [ ! -f "/usr/bin/gxmessage" ] ; then
         echo $MSG1 >/dev/stderr
       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" >/dev/stderr
  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
