#!/bin/bash

# SMBMBT: Check Samba Mount at Boot
# By Chris Blake
# http://servernetworktech.com
# Version 1.1

# Define the location that needs to be checked for mount
DRIVE="/mnt/NFS-SMB"

# Define the name of the interface that is used for the samba mount (ex eth0)
INTERFACE="vmbr1"

#Code starts here, no need to edit after this

if [ "$IFACE" != $INTERFACE ]; then
        exit 0
fi

logger "SMBMNT: Starting SAMBA Mount Check Script..."
echo "SMBMNT: Starting SAMBA Mount Check Script..."
LOOPCHECK=0
COUNT=0
while [ $LOOPCHECK != 1 ]; do
        if grep -swq $DRIVE /proc/mounts;
        then
            logger "SMBMNT: $DRIVE is already mounted. Finishing..."
            echo "SMBMNT: $DRIVE is already mounted. Finishing..."
            let LOOPCHECK=1
        else
        if [ $COUNT == 10 ];
        then
            logger "SMBMNT: Failed to mount 10 times... System will now reboot!"
            echo "SMBMNT: Failed to mount 10 times... System will now reboot!"
            reboot
            exit 0
        fi
        logger "SMBMNT: $DRIVE is not mounted, attempting to mount..."
        echo "SMBMNT: $DRIVE is not mounted, attempting to mount..."
        mount $DRIVE &>/dev/null
        logger "SMBMNT: $DRIVE should be mounted, pausing 5 seconds to try again..."
        echo "SMBMNT: $DRIVE should be mounted, pausing 5 seconds to try again..."
        sleep 5
        let COUNT=COUNT+1
        fi
done

logger "SMBMNT: Script Finished"
echo "SMBMNT: Script Finished"
exit 0
