#!/bin/sh
# install captive-ntfs and offer to mount paritions
# this is a wrapper for the "stock" captive-ntfs install script

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

# the name of the file system in /etc/fstab
CAPFSTAB=captive-ntfs

# 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-install wrapper script for the "stock"
captive-ntfs install script.  It will run the stock install and
offer to mount any ntfs partitions found.  The stock install will
scan for usable Windows ntfs driver files.  On successful completion
there will be mount points in /mnt and entries in /etc/fstab for any
ntfs paritions found - but they will not be mounted.  That can be
done by this wrapper or manually.

When the stock install completes it will indicate that it can be
unistalled by running /etc/<something>.  It may also be uninstalled by
executing the captive-ntfs-uninstall wrapper script.  The wrapper will
first unmount any captive-mounted paritions (VERY important for data
integrity if data has been WRITTEN) and then run the "stock" uninstall.

Hit the enter key to continue (this could take a while...)
DOC_HERE

read

# go run the ~stock install script - in its own dir since
# it needs its root.tar.gz file to be local

cat <<DOC_HERE

Installing captive-ntfs filesystem driver.

(The stock install will complain about /sbin/chkconfig and
/var/lock/subsys/captive but that seems to be harmless with
this KNOPPIX version.)

DOC_HERE

cd "$CAPDIR"
sudo ./install
if [ 0 != $? ]
then
    cat <<DOC_HERE

*** Install failure!
If the error was "already installed" you can run the
suggested script or my captive-ntfs-uninstall.
DOC_HERE

    badexit
fi

echo
echo "install success!"
echo

# see if there are any mountpoints created w/captive-ntfs fs

if ! grep -q "$CAPFSTAB" /etc/fstab
then
    cat <<DOC_HERE
   
*** No mountable ntfs partitions found!
DOC_HERE

    badexit
fi

# display available captive-ntfs mountable paritions

echo "Found the following mountable ntfs partitons:"
echo

mountpointlist=$(grep "$CAPFSTAB" /etc/fstab | sort | awk '{ print "    " $1 "   " $2}')
echo "$mountpointlist"

cat <<DOC_HERE

Shall I mount one or more of these partitions for you?
Enter the name of the partition you wish to mount (the part
after the "/mnt/") or "list" to list available mount points,
or whack the enter key to exit without mounting.  You may
also mount available paritions manually.

DOC_HERE

# get the user's requested mount point

mountmsg="Enter mount point name to mount, \"list\" to list
available mount points, or smack the enter key to exit.

/mnt/"

while read -p "$mountmsg" partname
do
    if [ -z $partname ]
    then
        echo
        echo "OK fine!  Exiting..."
        goodexit
    fi

    if [ "list" = $partname ]
    then
    echo
    echo "$mountpointlist"
    echo
    continue
    fi

# this is the name of the desired mount point

    partname="/mnt/$partname"

# this is the device that corresponds to the desired mount point

    devname=$(grep -w "$CAPFSTAB" /etc/fstab | grep -w "$partname" | awk '{ print $1}')
    if [ -z "$devname" ]
    then
        echo
        echo "*** One of us is confused!  That isn't a valid name!"
        echo
        continue
    fi

# if this device is already mounted get its mount point
# and deal with it

    mountname=$(mount | grep "$devname" | awk '{ print $3}')
    if [ -n "$mountname" ]
    then
        echo
        echo "**** Oops!  $mountname is already mounted - probably"
        echo "read-only.  Shall I unmount it and remount with captive-ntfs?"
        echo -n "[y|n]: "
        read reply
       
        case "$reply" in
        [Nn]*)
            echo
            echo "All right fumble-fingers, let's try that again..."
            echo
            continue
        ;;
        [Yy]*)
            echo
            echo "Unmounting $mountname."
            if ! sudo umount "$mountname"        
            then
                echo
                echo "*** umount $mountname failure! Exiting in total confusion!"
                badexit
            fi
            echo
            echo "Done."
        ;;
        *)
            echo
            echo "Cripes! This is just getting worse.  Take a deep"
            echo "breath and try again..."
             echo
            continue
        ;;
        esac
    fi

# go ahead and mount the requested partition

    echo
    echo "Mounting $partname."
    echo "(The \"unknown option\" message is harmless.)"
    echo
   
    if ! sudo mount "$partname"
    then
        echo "**** mount $partname failure! I have no clue"
        echo "what to do now.  Exiting..."
        badexit
    fi
    echo
    echo "Done.  Mount additional partitions?  Enter the name"
    echo "of the partition, or tickle the enter key to exit."
    echo
           
done

echo
echo "Done.  Really."
goodexit