#!/bin/sh
# uninstall captive-ntfs filesystem driver, first unmounting
# any captive-ntfs mounted paritions
# this is a wrapper for the "stock" captive-ntfs uninstall script

# where the stock scripts live
CAPDIR=/opt/beezix/captive-ntfs

# the name of the file system in "mount"
CAPMOUNT=fuse

# wait for enter key then exit w/good status
goodexit ()
{
    echo
    echo "Smite the enter key to continue."
    read
    echo
    exit 0
}

# wait for enter key then exit w/bad status
badexit ()
{
    echo
    echo "Whack the enter key to continue."
    read
    echo
    exit 1
}

cat <<DOC_HERE

This is the captive-ntfs-uninstall wrapper script for the "stock"
captive-ntfs uninstall script.  It will first unmount any
partitions mounted captive-ntfs.  Unmounting is NECESSARY to
ensure data integrity if any partitions have been written.  It
is possible that the stock unmount takes care of this but since
we can't see inside the binaries we will go for the sure thing.

Lean on the enter key to continue.
DOC_HERE

read

# get a list of captive-mounted partitions

mountlist=$(mount -t $CAPMOUNT | awk '{ print $3}')

if [ -z "$mountlist" ]
then
    echo
    echo "No captive-ntfs mounted partitions."
    echo
else
    for mountname in $mountlist
    do
        echo "Unmounting $mountname."
        sudo umount "$mountname"
        if [ 0 != $? ]
        then
            echo
            echo "*** umount $mountname failure!"
            echo "Turning tail and exiting!"
            badexit
        fi
    done
    echo
    echo "Unmount(s) done."
fi

# uninstall the filesystem driver

cat <<DOC_HERE

Uninstalling captive-ntfs filesystem driver.  There will
be some (harmless, I think) warnings that can be ignored.
As long as nobody yells "failure" it probably went OK.

DOC_HERE

cd "$CAPDIR"
sudo ./uninstall
if [ 0 != $? ]
then
    echo "*** Uninstall failure!  There's not much to do but exit."
    badexit
fi

echo
echo "Done."
goodexit