#!/bin/sh
set -e
USBDIR="/media/usb"
DIR="$USBDIR/ext2"

sync

umount_other_dirs () {
    WCDIRS="disk disk-1 usb"
    for dir in $WCDIRS ; do
        if [ -d /media/${dir} ] ; then
    	    umount /media/${dir}
	fi
    done
}

if mount | grep -q ${DIR} ; then
   # umount ${DIR}
   cryptmount -u usb
   umount_other_dirs
   exit
fi

MODULES="ehci_hcd
         uhci_hcd
         usb-storage
         vfat"

#         usbcore
#         usb-uhci
                                                                                          
for mod in $MODULES ; do
    if ! /sbin/lsmod | grep -q -w "^$mod" ; then
         sudo modprobe -v $mod
    fi
done

if ! mount | grep -q ${USBDIR} ; then
    mount ${USBDIR}
fi

if [ -d ${DIR} ] ; then
    # mount ${DIR}
    cryptmount usb
fi
