#!/bin/bash
#
# Mount USB Drive in preparation for backup
# Ron Olsen <ronolsen@comcast.net>
# 07 Mar 2014
#
# Assumptions:
# USB device name is in /etc/vortexbox/CurrentUsbDrive.txt
# Backup mount point is /backup
# Drive is formatted as a VortexBox backup drive with a VortexBox.txt file
#
# Returns 0 on successful mount, nonzero otherwise
#
# Make sure backup drive is present
#
if [ ! -f /etc/x-stream/CurrentUsbDrive.txt ]
then
    echo "Error: No backup drive. Please plug your xo|one backup drive into a USB port and try again."
    exit 1
fi
BackupDir=/backup
UsbDrive=$(cat /etc/x-stream/CurrentUsbDrive.txt)1 
if  grep -qs "$BackupDir" /proc/mounts 
then
    umount $UsbDrive
fi
echo "Mounting $UsbDrive at $BackupDir..."
mount $UsbDrive $BackupDir
Status=$?
if test $Status -eq 0
then
    echo "$UsbDrive mounted on $BackupDir"

    if [ -f "/backup/xo-one.txt" ]
    then
        echo "$UsbDrive is valid xo|one backup drive"
        exit 0
    else
        echo "$UsbDrive is not a xo-one backup drive. Please format."
        umount $BackupDir
        exit 1
    fi
else
    echo "Error $Status occurred while mounting USB drive. Mount failed."
    exit $Status
fi