#!/bin/sh
#
# mntloop - script to mount and unmount loop devices
# version 0.5.0
#
# 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 [ ! -d $MP ]; then
           mkdir -p $MP
       fi
       if [ "`mount|grep ${MP}`" = "" ]; then
           mount $1 
           if [ ! -f /home/samba/Extra_`basename $MP` ]; then
             ln -sf $MP /home/samba/Extra_`basename $MP` 2>/dev/null
           fi
       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)
    # 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

    if [ "`ls /data/*3.4.5* 2>/dev/null`" != "" ] || [ "`ls /home/root/*3.4.5* 2>/dev/null`" != "" ]; then
      if [ ! -d /opt/native/arm/3.4.5-xscale-softvfp ]; then
        mkdir -p /opt/native/arm/3.4.5-xscale-softvfp
      fi
    fi
    if [ -f /data/zgcc-3.4.5-4.squashfs ]; then
        mount -o loop -t squashfs /data/zgcc-3.4.5-4.squashfs /opt/native/arm/3.4.5-xscale-softvfp
    elif [ -f /home/root/zgcc-3.4.5-4.squashfs ]; then
        mount -o loop -t squashfs /home/root/zgcc-3.4.5-4.squashfs /opt/native/arm/3.4.5-xscale-softvfp
    elif [ -f /home/root/zgcc-3.4.5.img ]; then
        mount -o loop /home/root/zgcc-3.4.5.img /opt/native/arm/3.4.5-xscale-softvfp
    fi
    if [ -f /opt/native/arm/3.4.5-xscale-softvfp/specs/default/qmake.conf ]; then
      if [ ! -f /etc/sysconfig/zgcc ] || [ "`grep '3.4.5' /etc/sysconfig/zgcc`" = "" ]; then 
        echo "ZGCC_VERSION=3.4.5" > /etc/sysconfig/zgcc
      fi
    fi

    if [ "`ls /data/*3.4.6* 2>/dev/null`" != "" ] || [ "`ls /home/root/*3.4.6* 2>/dev/null`" != "" ]; then
      if [ ! -d /opt/native/arm/3.4.6-xscale-softvfp ]; then
        mkdir -p /opt/native/arm/3.4.6-xscale-softvfp
      fi
    fi
    if [ -f /data/zgcc-3.4.6.squashfs ]; then
        mount -o loop -t squashfs /data/zgcc-3.4.6.squashfs /opt/native/arm/3.4.6-xscale-softvfp
    elif [ -f /home/root/zgcc-3.4.6.squashfs ]; then
        mount -o loop -t squashfs /home/root/zgcc-3.4.6.squashfs /opt/native/arm/3.4.6-xscale-softvfp
elif [ -f /home/root/zgcc-3.4.6.img ]; then
        mount -o loop /home/root/zgcc-3.4.6.img /opt/native/arm/3.4.6-xscale-softvfp
    fi
    if [ -f /opt/native/arm/3.4.6-xscale-softvfp/specs/default/qmake.conf ]; then
      if [ ! -f /etc/sysconfig/zgcc ] || [ "`grep '3.4.6' /etc/sysconfig/zgcc`" = "" ]; then 
        echo "ZGCC_VERSION=3.4.6" > /etc/sysconfig/zgcc
      fi
    fi

#    if [ -f /data/dictionaries.squashfs ]; then
#        if [ ! -d /mnt/dictionaries ]; then
#	    mkdir -p /mnt/dictionaries
#        fi
#        mount -o loop -t squashfs /data/dictionaries.squashfs /mnt/dictionaries
#    fi

#    if [ -f /data/openoffice.squashfs ]; then
#        if [ ! -d /mnt/openoffice ]; then
#	    mkdir -p /mnt/openoffice
#        fi
#        mount -o loop -t squashfs /data/openoffice.squashfs /mnt/openoffice
#    fi

    # 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

    if [ -d /mnt/dictionaries/qbedic ]; then
      mount --bind /mnt/dictionaries/qbedic /usr/share/bedic
    fi
    if [ -d /mnt/dictionaries/stardict ]; then
      mount --bind /mnt/dictionaries/stardict /usr/local/share/stardict/dic
    fi

    ;;

stop)
   
    # unmount the cram images and loopbacks 
    for i in `mount|grep loop|awk '{print $3}'`
    do
      LD=`mount|grep $i|cut -d' ' -f1`
      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
        losetup -d $LD 2>/dev/null
        rm /home/samba/Extra_`basename $i` 2>/dev/null
      fi
    done

    ;;

status)
    mount |grep loop 
    ;;

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

*)
    usage
    ;;

esac
exit 0
