#! /bin/bash
# Usage ./umountdcss <dcssname>

DEBUGON="FALSE"
ERR_DCSS_UMNT=10
ERR_DCSS_REMOVE=11

# Echo the parameters if DEBUGON is true
debugecho() {
        if [ "$DEBUGON" = "TRUE" ]; then
                echo $1
        fi
}

# Exit with an Error
#    Use only after DCSS has been loaded
exitwitherror() {
        if [ $# = 3 ]
        then
                debugecho "Must first umount"
                umount /mnt
        fi

        echo $1 > /sys/devices/dcssblk/remove
        debugecho "Removed the DCSS"
        exit $2
}

# Check Script Usage
if [ $# = 0 ]
then
        debugecho "Usage: ./umountdcss <dcssname>"
        exit 1
fi

DCSSNAME=$1

# Unmount the file system
umount /mnt

# Check for errors
if [ $? -ne 0 ]; then
       debugecho "Error unmounting $DCSSNAME"
       exitwitherror $DCSSNAME $ERR_DCSS_UMNT
else
       debugecho "$DCSSNAME unmounted successfully"
fi

# Remove the device
echo $DCSSNAME > /sys/devices/dcssblk/remove

# Check for errors
if [ $? -ne 0 ]; then
       debugecho "Error removing $DCSSNAME"
       exitwitherror $DCSSNAME $ERR_DCSS_REMOVE
else
       debugecho "$DCSSNAME removed successfully"
fi

# If we got here everything worked
rc=0

exit $rc
