#!/bin/sh
#
# mntloop - script to mount and unmount loop devices and binds
#           additionally swapfile and /hdd4 is also handled
# version 0.4.9
#
# Copyright (C) 2006 hdluc@yahoo.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
######################################################################

usage()
{
    echo "Usage: $0 {start|stop|status|restart|reload}"
    exit 0
}

mountfs()
{
   if [ -f $1 ]; then
       MP=`grep $1 /etc/fstab|grep -v \#|awk '{print $2}'`
       if [ "`mount|grep ${MP}`" = "" ]; then
           mount $1 
       fi
       if [ ! -f /home/samba/Extra_`basename $MP` ]; then
         ln -sf $MP /home/samba/Extra_`basename $MP`
       fi
   fi
}

if [ $# -lt 1 ] ; then usage ; break ; fi
if [ "`whoami`" != "root" ]; then
  echo "`basename $0` needs to be run as root"
fi
action=$1
option="$2"
if [ "$option" = "" ]; then
  option="/"
fi

case "$action" in

start)
    # mount /hdd4 if available
    if [ -d /hdd4 ] && [ "`mount|grep hdd4`" = "" ]; then
      if [ "`fdisk -l /dev/hda|grep hda4`" != "" ]; then
        mount -o rw,noatime /dev/hda4 /hdd4
        ln -s /hdd4 /home/samba/Extra_Disk
      else
        if [ "`fdisk -l /dev/hdc|grep hdc4`" != "" ]; then
          mount -o rw,noatime /dev/hdc4 /hdd4
          ln -s /hdd4 /home/samba/Extra_Disk
        fi
      fi
    fi

    # make more loop devices
    for i in 2 3 4 5 6
    do
      if [ ! -e /dev/loop$i ]; then
        mknod /dev/loop$i b 7 $i 2>/dev/null
      fi
    done

    # make cdrom devices
    mknod /dev/pg0 b 11 0 2>/dev/null
    mknod /dev/sg0 b 11 0 2>/dev/null
    ln /dev/scd0 /dev/cdrom 2>/dev/null
    ln /dev/scd0 /dev/vcd 2>/dev/null
    ln /dev/scd0 /dev/dvd 2>/dev/null

    # mount the cram images if defined in /etc/fstab
    for i in `grep cramfs /etc/fstab|grep $option|grep -v \#|awk '{print $1}'`
    do
      mountfs $i
    done

    # mount the squash images if defined in /etc/fstab
    for i in `grep squashfs /etc/fstab|grep $option|grep -v \#|awk '{print $1}'`
    do
      mountfs $i
    done

    # mount the ext2 images if defined in /etc/fstab
    for i in `grep ext2 /etc/fstab|grep $option|grep -v \#|awk '{print $1}'`
    do
      mountfs $i
    done

    # mount the ext3 images if defined in /etc/fstab
    for i in `grep ext3 /etc/fstab|grep $option|grep -v \#|awk '{print $1}'`
    do
      mountfs $i
    done

    # start sd card
    if [ -f /etc/rc.d/init.d/card ] && [ "`basename $0`" = "automounter" ] && [ "`mount|grep card`" = "" ]; then
        /etc/rc.d/init.d/card start >/dev/null 2>/dev/null
    fi

    # turn on swap
    SWAPFILE=swapfile
    if [ "$option" = "" ]; then
      if [ -f /mnt/card/$SWAPFILE ]; then
        SWAP=/mnt/card/$SWAPFILE
      fi 
      if [ -f /mnt/cf/$SWAPFILE ]; then
        SWAP=/mnt/cf/$SWAPFILE
      fi 
      if [ -f /hdd3/$SWAPFILE ]; then
        SWAP=/hdd3/$SWAPFILE
      fi 
      if [ -f /hdd4/$SWAPFILE ]; then
        SWAP=/hdd4/$SWAPFILE
      fi 
    else
      SWAP=/mnt/$option/$SWAPFILE
    fi
    if [ -f $SWAP ] && [ "`grep $SWAPFILE /proc/swaps`" = "" ]; then
      swapon $SWAP 
    fi

    # bind cards
    if [ -f /etc/minideb ]; then
      MINIDEB=`cat /etc/minideb`
    fi
    if [ "$option" != "/" ] && [ "$MINIDEB" != "" ] && [ -d $MINIDEB ]; then
      DB=`basename $MINIDEB`
      if [ "`mount|grep $DB/proc`" != "" ]; then
        mount -o bind /mnt/$option $MINIDEB/mnt/$option
      fi
    fi
    if [ -f /etc/debroot ]; then
      DEBROOT=`cat /etc/debroot`
    fi
    if [ "$option" != "/" ] && [ "$DEBROOT" != "" ] && [ -d $DEBROOT ]; then
      DB=`basename $DEBROOT`
      if [ "`mount|grep $DB/proc`" != "" ]; then
        mount -o bind /mnt/$option $DEBROOT/mnt/$option
      fi
    fi

    if [ "`mount|grep dictionaries|wc -l|awk '{print $1}'`" = "1" ]; then
      if [ -d /hdd3/Documents ] && [ -d /mnt/dictionaries/qbedic ]; then
        if [ ! -d /hdd3/Documents/dictionaries ]; then
          mkdir -p /hdd3/Documents/dictionaries
        fi
        mount -o bind /mnt/dictionaries/qbedic /hdd3/Documents/dictionaries
      fi
    fi
    ;;

stop)
    # turn off swap
    for i in `cat /proc/swaps|grep swapfile|grep $option|awk '{print $1}'`
    do
      swapoff $i 2>/dev/null
    done
   
    # unmount binds
    if [ -f /etc/debroot ]; then
      DEBROOT=`cat /etc/debroot`
    fi
    if [ "$DEBROOT" != "" ] && [ -d $DEBROOT ]; then
      DB=`basename $DEBROOT`
      if [ "`mount|grep $DB`" != "" ]; then
        for i in `mount|grep $DB|grep $option|awk '{print $3}'`
        do
          if [ "`basename $i`" != "$DB" ]; then
            umount $i 2>/dev/null
          fi
        done
      fi
    fi
    if [ -f /etc/minideb ]; then
      MINIDEB=`cat /etc/minideb`
    fi
    if [ "$MINIDEB" != "" ] && [ -d $MINIDEB ]; then
      DB=`basename $MINIDEB`
      if [ "`mount|grep $DB`" != "" ]; then
        for i in `mount|grep $DB|grep $option|awk '{print $3}'`
        do
          if [ "`basename $i`" != "$DB" ]; then
            umount $i 2>/dev/null
          fi
        done
      fi
    fi

    # unmount the cram images and loopbacks 
    for i in `mount|grep loop|awk '{print $3}'`
    do
      if [ "$i" != "/usr" ]; then
        if [ "$option" = "/" ]; then
          umount $i 2>/dev/null
        else
          umount `grep $i /etc/fstab|grep $option|awk '{print $2}'` 2>/dev/null
        fi
        rm /home/samba/Extra_`basename $i` 2>/dev/null
      fi
    done

    # unmount /hdd4 if mounted
    if [ "$option" = "/" ] && [ -d /hdd4 ] && [ "`mount|grep hdd4`" != "" ]; then
      umount /hdd4
      rm /home/samba/Extra_Disk 2>/dev/null
    fi 

    # stop  sd card
    if [ "$option" = "/" ] && [ -f /etc/rc.d/init.d/card ] && [ "`basename $0`" != "automounter" ]; then
        /etc/rc.d/init.d/card stop >/dev/null 2>/dev/null
    fi

    # unmount swap partition
    if [ "$option" = "/" ] && [ "`cat /proc/swaps|grep partition`" != "" ]; then
      swapoff `cat /proc/swaps|grep partition|awk '{print $1}'`
    fi 
    ;;

status)
    mount |grep loop 
    ;;

restart|reload)
    $0 stop
    $0 start
    ;;

*)
    usage
    ;;

esac
exit 0
