#!/bin/sh
#
# usb-storage - program to automount multiple usb disks 
# version 0.1.5
#
# Copyright (C) 2005 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.
#
######################################################################
. /etc/hotplug/hotplug.functions 2>/dev/null

FSTABOPT="auto noauto,umask=000,noatime,iocharset=utf8 0 0"

make_notifier ()
{
cat > /opt/QtPalmtop/bin/usbnotify << EOF
#!/bin/sh
# This file is generated by usb-storage
MSG=\$1
if [ "\`id -u\`" = "500" ]; then
  /opt/QtPalmtop/bin/qcop QPE/TaskBar "message(QString)" "\$MSG" 2>/dev/null
else
  su zaurus -c "/opt/QtPalmtop/bin/qcop QPE/TaskBar \"message(QString)\" \"\$MSG\" 2>/dev/null"
fi

EOF

chmod 755 /opt/QtPalmtop/bin/usbnotify
}

make_remover ()
{
cat > /etc/hotplug/usb/usb-storage.off << EOF
#!/bin/sh
# This file is generated by usb-storage
. /etc/hotplug/hotplug.functions 2>/dev/null
unmount ()
{
  DSK=\$1
  PRT=\$2
  MNT=\`mount|grep /dev/\$DSK|awk '{print $3}'\`
  if [ "\$MNT" != "" ]; then
    umount \$MNT
    /opt/QtPalmtop/bin/usbnotify "unmounting \$MNT"
  fi
  LN=\`ls /home/samba|grep USB|grep \$PRT\`
  if [ "\$LN" != "" ]; then
    rm /home/samba/\$LN
  fi
}
mesg Removing usb disks 
unmount sda 1
unmount sdb 2
unmount sdc 3
unmount sdd 4

EOF

chmod 755 /etc/hotplug/usb/usb-storage.off
}

add_remover ()
{
    if [ ! -f $REMOVER ]; then
      ln -s /etc/hotplug/usb/usb-storage.off $REMOVER
      mesg make REMOVER in $REMOVER
    fi
}

mount_part ()
{
    DEVICE=$1
    ID=$2
    DISKINFO=$3
    PART=$4
    MNT=$5
    M=`mount|grep ${DEVICE}${PART}`
    D=`echo $DISKINFO|grep ${DEVICE}${PART}`
 
    if [ "$D" != "" ] && [ "$M" = "" ]; then
        if [ ! -d /mnt/${MNT}${ID} ]; then
            mkdir -p /mnt/${MNT}${ID}
        fi
        #if [ "`grep ${DEVICE}${PART} /etc/fstab`" = "" ]; then
        #    echo "/dev/${DEVICE}${PART} /mnt/${MNT}${ID} ${FSTABOPT}" >> /etc/fstab
        #fi
        #mount /mnt/${MNT}${ID}
        mount /dev/${DEVICE}${PART} /mnt/${MNT}${ID} ${FSTABOPT}
        /opt/QtPalmtop/bin/usbnotify "mounting /mnt/${MNT}${ID}"
        if [ $? = 0 ]; then
            add_remover
            if [ ! -d /home/samba/USB_${MNT}${ID} ]; then
                ln -s /mnt/${MNT}${ID} /home/samba/USB_${MNT}${ID}
            fi 
        fi
    fi
}

mount_disk ()
{
    DEVICE=$1
    ID=$2
    DISKINFO=`fdisk -l /dev/$DEVICE`
    if [ "$DISKINFO" != "" ]; then
        if [ "`echo $DISKINFO|grep ${DEVICE}5`" != "" ]; then
           mount_part $DEVICE $ID "$DISKINFO" 5 usbdisk 
        else
           mount_part $DEVICE $ID "$DISKINFO" 1 usbstorage 
        fi
    fi 
}

# start execution here
if [ ! -f /etc/hotplug/usb/usb-storage.off ]; then
    make_remover
fi
if [ ! -f /opt/QtPalmtop/bin/usbnotify ]; then
    make_notifier
fi
if [ ! -L /var/run/usb/%proc%bus%usb%* ]; then
    mesg Try to Mount
    mount_disk sda 1
    mount_disk sdb 2
    mount_disk sdc 3
    mount_disk sdd 4
    if [ "`fdisk -l /dev/sda4`" != "" ]; then
        mkdir -p /mnt/usbzip 2>/dev/null
        mount /dev/sda4 /mnt/usbzip
        /opt/QtPalmtop/bin/usbnotify "mounting /mnt/usbzip" 
    fi
    if [ "`fdisk -l /dev/scd0`" != "" ]; then
        mkdir -p /mnt/cdrom 2>/dev/null
        mount -o ro /dev/scd0 /mnt/cdrom
        /opt/QtPalmtop/bin/usbnotify "mounting /mnt/cdrom" 
    fi
fi
