#!/bin/bash
#
# Check the status of our mounted NFS shared directory.
# If the shared directory is offline, change our link to point at the backup directory.
# If the shared directory is online, change our link to point at the shared directory.
#

SHARED=/mnt/files
BACKUP=/srv/file_read_only
LINK=/srv/files
TO="webmaster@example.edu"

LINK_TARGET=$(readlink $LINK)

if [ -f ${SHARED}/online ]
then
        # Verify that our link is pointing at the NFS path, updating it if not.
        if [ $LINK_TARGET != $SHARED ]
        then
                rm $LINK
                ln -s $SHARED $LINK
                SUBJECT="Drupal NFS Status"
                mail -s "$SUBJECT" $TO <<EOF
Status change from `hostname`:
The Drupal image directory at $SHARED is now available.

The sym-link at $LINK now points to the NFS directory at ${SHARED}.

No further action is needed at this time.

Time: `date`
EOF

        fi
else
        # Verify that our link is pointing at the backup path, updating it if not.
        if [ $LINK_TARGET != $BACKUP ]
        then
                rm $LINK
                ln -s $BACKUP $LINK
                SUBJECT="Drupal NFS Status"
                mail -s "$SUBJECT" $TO <<EOF
Status change from `hostname`:
The Drupal image directory at $SHARED is NOT available.

The sym-link at $LINK now points to the backup directory at ${BACKUP}

When the NFS mount at $SHARED is reestablished, the sym-link will be updated automatically.

Time: `date`
EOF

        fi
fi
