#!/bin/bash
#
###########################################
#
# Checks if the object is available
# If the command got an arg, it will check this
# otherwise it will check /var/backup/ismounted.txt 
#
# ATTENTION: use only for checking a specific file, otherwise you got a crit
#
# Author: Gerrit Lazer - TimmeHosting
# Date: 2017-07-27
# Version: 0.1
###########################################

# Check if we got an up to date timeout (coreutils) version
if [[ $(timeout --help | grep 'preserve-status' | wc -l) -eq 0 ]];
then
        echo "Timeout (coreutils) are too old"
        exit 1
fi

# Check if we got an argument, otherwise use the default Timme-Path(finder)
if [[ -n $1 ]];
then
        TESTINGOB=$1
else
        TESTINGOB="/var/backup/ismounted.txt"
fi

# Killed two birds with one stone:
# The check recognizes if the network connection is stalled and if the ismounted.txt is available alias the backupdir is mounted
/usr/bin/timeout --preserve-status --signal=15 60 /bin/cat $TESTINGOB &> /dev/null

# If the ls stalled and got killed, the exit code is greater 0
# If the file ismounted.txt is not available, the exit code is greater 0
TESTING=$( echo $? )

if [[ $TESTING -eq 0 ]]; 
then
        echo "$TESTINGOB available"
        exit 0
else
        echo "$TESTINGOB not available"
        exit 2
fi

