#!/bin/bash

# shellcheck disable=SC1090,SC1091,SC2086,SC2129,SC2162,SC2181,SC2219

#######################################################################
# this is a helper script that keeps snapraid parity info in sync with
# your data. Here's how it works:
#   1) it first calls diff to figure out if the parity info is out of sync
#   2) if there are changed files (i.e. new, changed, moved or removed),
#         it then checks how many files were removed.
#   3) if the deleted files exceed X (configurable), or updated files exceed
#	   Y (configurable), it triggers an
#         alert email and stops. (in case of accidental deletions)
#   4) otherwise, it will call sync.
#   5) when sync finishes, it sends an email with the output to user.
#
# code from:
# Original-Author: sidney
# Author Zack Reed
# http://zackreed.me/articles/83-updated-snapraid-sync-script
# HeadURL: file:///svnrepo/linuxScripts/snapraid_diff_n_sync.sh $
# Modified Wouter Dullaert http://soundhacker.be
# changed for OMV:
# Solo0815: http://forums.openmediavault.org/index.php/User/156-Solo0815/
#
# ToDo:
# - write sed file from config
# - check for mail and PW in config.xml
# - check for drive name in OMV -> snapraid.conf plugin!
#
################################################################

# Changes from v 1.1.0: Fix deleted and updated files logic
# Changes from v 1.2.0: Add support for multiple arrays
SCRIPTVERSION="1.3.5"

# get basic info
. /etc/default/openmediavault

# needed for omv_config_get command
. /usr/share/openmediavault/scripts/helper-functions

# Set error count variable
# 0 for no thresholds exceeded; 1 for update threshold exceeded; 2 for delete threshold exceeded; 3 for both exceeded
declare -i UPD_DEL_CHECK=0

declare -i DEL_COUNT=0
declare -i ADD_COUNT=0
declare -i MOVE_COUNT=0
declare -i COPY_COUNT=0
declare -i UPDATE_COUNT=0

declare -i PREHASH_VALUE=0


ARRAY_CONF="${1}"

# Set path to snapraid.conf
if [ -f "${ARRAY_CONF}" ]; then
    SNAPRAID_CONF="${ARRAY_CONF}"
else
    SNAPRAID_CONF="/etc/snapraid.conf"
fi

# the following file is created by the OMV-GUI
SR_DIFF_CONF="/etc/snapraid-diff.conf"

EMAIL_SUBJECT_PREFIX="SnapRAID"

SNAPRAID_LOG="/var/log/snapraid.log"

################################################################
#####     Functions
################################################################

################################################################
#
#   name:	_send_email
#   parameter : 	# $1 = Subject
					# $2 = file with mailtext
					# Optional:
					# $3 = Who sent this message (full name), e.g. "Server Status-Report"
					# $4 = Answer to this adress
#   return: 	0 = All OK
#				1 = Error happend
#
_send_email(){
## the below does not work with the internal OMV-Settings
#	mail -s "$1" -a 'Content-Type: text/plain; charset=utf-8' $MAIL -- -F "$3" -f "$4" < $2

if [ $SENDMAIL = "false" ]; then
	_log "INFO: Mail should be sent here - SENDMAIL set to false"
	echo "Mail should be sent here - SENDMAIL set to false"
	return 0
fi

# add mail footer
cat >> $TMP_OUTPUT <<EOF

Yours, SnapRAID-diff script
EOF

#mail [-a header] [-b bcc-addr] [-c cc-addr] [-s subject] to-addr
mail -c "$OMV_MAIL_secondarymail" -s "$1" "$OMV_MAIL_primarymail" < $2

	if [ $? -eq 0 ]; then
		_log "INFO: Full statusreport sent to '$MAIL'"
		return 0
	else
		_log "WARN: Mail with full statusreport could not be sent. There was an error. Please have a look at /var/log/mail.log"
		return 1
	fi
}

################################################################
#
#   name      : _log
#   parameter   : $LOGMESSAGE : logmessage in format "PRIORITY: MESSAGE"
#   return      : none
#
_log() {
	   [[ "$*" =~ ^([A-Za-z]*):\ (.*) ]] &&
		 {
			PRIORITY=${BASH_REMATCH[1]} # INFO, DEBUG, WARN
			LOGMESSAGE=${BASH_REMATCH[2]} # the Log-Message
		 }

	  if $DEBUG ; then
		echo "$(date '+[%Y-%m-%d %H:%M:%S]') $(basename "$0"): $PRIORITY: '$LOGMESSAGE'"
		echo "$(date '+[%Y-%m-%d %H:%M:%S]') $(basename "$0"): $PRIORITY: '$LOGMESSAGE'" >> $SNAPRAID_LOG
	else
		echo "$(date '+[%Y-%m-%d %H:%M:%S]') $(basename "$0"): $PRIORITY: '$LOGMESSAGE'" >> $SNAPRAID_LOG
	fi
}

################################################################
#
#   name      : _rmtmp
#   parameter   : none
#   return      : none
#
# removes $TMP_OUTPUT
_rmtmp() {
	rm $TMP_OUTPUT >/dev/null 2>&1
	rm $TMP_DIR >/dev/null 2>&1
}

################################################################
#
#   name      : _check_success
#   parameter   : none
#   return      : 0 = last command was successful
#				: 1 = last command ended with an error -> [ $? -gt 1 ]
#
_check_success() {
	if [ $? = 0 ]; then
		return 0
	else
		return 1
	fi
}

################################################################
#
#   name      : _add_mail_header
#   parameter   : none
#
# adds mail text
#
_add_mail_header() {
	cat > $TMP_OUTPUT <<EOF
SnapRAID Script - Version: $SCRIPTVERSION
running SnapRAID $SNAPRAIDVERSION
---------------------------------------------
SnapRAID Job started - $(date)
Warnings about 'Ignoring mount point' are possibly due to AUFS or mhddfs-Pooling in OMV

EOF
}

################################################################
#####     MAIN BODY of the script
################################################################

# read config
if [ -f $SR_DIFF_CONF ]; then
	. $SR_DIFF_CONF
else
	_log "WARN: $SR_DIFF_CONF not found! Exiting ..."
	echo "$SR_DIFF_CONF not found! Exiting ..."
fi

############################
# Mail settings:
#primary and secondary recipients
OMV_MAIL_primarymail=$(omv_config_get "//system/email/primaryemail")
OMV_MAIL_secondarymail=$(omv_config_get "//system/email/secondaryemail")

if $DEBUG; then
	_log "DEBUG: OMV_MAIL_primarymail $OMV_MAIL_primarymail"
	_log "DEBUG: OMV_MAIL_secondarymail $OMV_MAIL_secondarymail"
fi

# structure in /etc/openmediavault/config.xml

#    <email>
#      <enable>1</enable>
#      <server>mail.blablabla.com</server>
#      <port>25</port>
#      <tls>0</tls>
#      <sender>server@blablabla.com</sender>
#      <authentication>
#        <enable>0</enable>
#        <username></username>
#        <password></password>
#      </authentication>
#      <primaryemail>someuser@blablabla.net</primaryemail>
#      <secondaryemail></secondaryemail>
#    </email>

# read CONTENT and PARITY from $SNAPRAID_CONF
OMV_CONTENT_FILES="$(awk '$1 == "content" { print $2 }' ${SNAPRAID_CONF})"
PARITY_FILE="$(awk '$1 == "parity" { print $2 }' ${SNAPRAID_CONF})"
OMV_SNAPRAID_DISKS="$(awk '$1 == "data" { print $3 }' ${SNAPRAID_CONF})"

## INTERNAL TEMP VARS ##
TMP_DIR="/tmp/snapraid"
TMP_OUTPUT="$TMP_DIR/snapraid.out"
TMP_SCRUB_DIR="/root/.snapraid"

if $DEBUG; then
	_log "DEBUG: OMV_CONTENT_FILES: $OMV_CONTENT_FILES"
	_log "DEBUG: PARITY_FILE: $PARITY_FILE"
	_log "DEBUG: OMV_SNAPRAID_DISKS: $OMV_SNAPRAID_DISKS"
	_log "DEBUG: TMP_OUTPUT: $TMP_OUTPUT"
	_log "DEBUG: RUN_SCRUB: $RUN_SCRUB"
	_log "DEBUG: SCRUB_FREQUENCY_IN_DAYS: $SCRUB_FREQUENCY_IN_DAYS"
	_log "DEBUG: SCRUB_PERCENT: $SCRUB_PERCENT"
	_log "DEBUG: SCRUB_OLDER_THAN_DAYS: $SCRUB_OLDER_THAN_DAYS"
	_log "DEBUG: DEL_THRESHOLD: $DEL_THRESHOLD"
	_log "DEBUG: UPD_THRESHOLD: $UPD_THRESHOLD"
fi

SNAPRAID_BIN="/usr/bin/snapraid"
if [ -z $SNAPRAID_BIN ]; then
	_log "WARN: snapraid executable not found!"
fi
if [ ! -x $SNAPRAID_BIN ]; then
	_log "WARN: snapraid '$SNAPRAID_BIN' not executable! Please do a 'chmod +x $SNAPRAID_BIN'"
fi

SNAPRAIDVERSION="$(dpkg-query -W -f='${Version}' snapraid)"

# check tmp-dirs
if [ ! -d $TMP_DIR ]; then
		mkdir $TMP_DIR
fi

if [ -f $TMP_OUTPUT ]; then
	rm $TMP_OUTPUT && _log "INFO: '$TMP_OUTPUT' deleted - creating a new one"
fi

#sanity check first to make sure we can access the content and parity files
CONTENT_FILES_COUNTER=0

# check, if at least one content file is found
for SINGLE_CONTENT_FILE in $OMV_CONTENT_FILES; do

	if $DEBUG; then _log "DEBUG: found: SINGLE_CONTENT_FILE: $SINGLE_CONTENT_FILE"; fi

	if [ -e $SINGLE_CONTENT_FILE ]; then
		let CONTENT_FILES_COUNTER++
		continue
	else
		_log "WARN: Content file ($SINGLE_CONTENT_FILE) not found!"
	fi
done

if [ $CONTENT_FILES_COUNTER -eq 0 ]; then
	_log "WARN: No Content files found! Exiting ..."
	exit 1
fi

IFS=','; for parity in ${PARITY_FILE}; do
    if [ -e ${parity} ]; then
	    if $DEBUG; then _log "DEBUG: found: PARITY_FILE: ${parity}"; fi
    else
        _log "WARN: Parity file (${parity}) not found!"
        exit 1
    fi
done

### Section to support SnapRAID Scrub ###
mkdir -p $TMP_SCRUB_DIR

if [ -f $TMP_SCRUB_DIR/snapraid_scrub_counter ]; then
	COUNTER=$(cat $TMP_SCRUB_DIR/snapraid_scrub_counter)
else
	echo "1" > $TMP_SCRUB_DIR/snapraid_scrub_counter
	COUNTER=1
fi
# enable only one of the two below!!!
let COUNTER++
# COUNTER=$(($COUNTER+0))

# define Variables, if not set in snapraid-diff
if [ -z ${SCRUB_PERCENT} ]; then
	SCRUB_PERCENT=100
	_log "INFO: SCRUB_PERCENT not set in '$SR_DIFF_CONF'"
	_log "INFO: set it to $SCRUB_PERCENT"
fi
if [ -z ${SCRUB_OLDER_THAN_DAYS} ]; then
	SCRUB_OLDER_THAN_DAYS=0
	_log "INFO: SCRUB_OLDER_THAN_DAYS not set in '$SR_DIFF_CONF'"
	_log "INFO: set it to $SCRUB_OLDER_THAN_DAYS"
fi
### End Scrub Section ###

################################################################
## Start of the script
_log "INFO: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_log "INFO: SnapRAID Job started - Script-version: $SCRIPTVERSION"
_log "INFO: SnapRAID-Version: $SNAPRAIDVERSION"
_add_mail_header # all info above is in that function

_log "INFO: Checking 'disks' in '$SNAPRAID_CONF'"
### check if all hdds in the config are there
for OMV_DISK in $OMV_SNAPRAID_DISKS; do

	if $DEBUG; then _log "DEBUG: found: OMV_DISK: $OMV_DISK"; fi

	if ! grep $OMV_DISK /etc/mtab > /dev/null 2>&1; then

	#if [ ! -b $OMV_DISK ]; then # doesn't work - no block device found :o
		_log "WARN: ERROR: Disk $OMV_DISK was not found or is not mounted!"
		_log "WARN: Sync job cancelled to prevent data loss."
		_log "WARN: Please check your disks!"
		_log "WARN: Replace damaged Disk and proceed with 'snapraid fix'"
		_log "WARN: to rebuild the raid array and recover your data."
		_log "WARN: Then, use 'snapraid check' to verify the integrity of the recovered files."
		_log "WARN: Lastly, use 'snapraid sync' to update the parity files."

		echo "ERROR - Data Disk $OMV_DISK not found or is not mounted!" >> $TMP_OUTPUT
		echo "Sync job cancelled to prevent data loss." >> $TMP_OUTPUT
		echo " " >> $TMP_OUTPUT
		echo "Please check your disks!"  >> $TMP_OUTPUT
		echo "Replace damaged Disk and proceed with 'snapraid fix'" >> $TMP_OUTPUT
		echo "to rebuild the raid array and recover your data." >> $TMP_OUTPUT
		echo "Then, use 'snapraid check' to verify the integrity of the recovered files." >> $TMP_OUTPUT
		echo "Lastly, use 'snapraid sync' to update the parity files." >> $TMP_OUTPUT

		_send_email "$EMAIL_SUBJECT_PREFIX - ERROR: Data disk not found!" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"
		_check_success && _rmtmp
		_log "WARN: script exits here due to missing disk ..."
		exit 1;
	fi
done

################################################################
# start snapraid DIFF command
_log "INFO: SnapRAID DIFF started"
echo "SnapRAID DIFF started - $(date)" >> $TMP_OUTPUT

# run diff:
# output: stdout: WARNINGS only (filtering 'Ignoring mount point') and $TMP_OUTPUT (all WARNINGS)
/usr/bin/snapraid diff --conf ${SNAPRAID_CONF} 2>&1 | tee -a $TMP_OUTPUT | grep WARNING | grep -v 'WARNING! Ignoring mount point' | while read line
do
	_log "WARN: $line"
done

# wait for the above cmd to finish
wait

_log "INFO: SnapRAID DIFF finished"
_log "INFO: ----------------------------------------"
echo "SnapRAID DIFF finished - $(date)" >> $TMP_OUTPUT
echo "----------------------------------------" >> $TMP_OUTPUT

# read the values of deleted, added, etc. files since the last sync
DEL_COUNT=$(grep -w '^ \{1,\}[0-9]* removed$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
ADD_COUNT=$(grep -w '^ \{1,\}[0-9]* added$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
MOVE_COUNT=$(grep -w '^ \{1,\}[0-9]* moved$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
COPY_COUNT=$(grep -w '^ \{1,\}[0-9]* copied$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)
UPDATE_COUNT=$(grep -w '^ \{1,\}[0-9]* updated$' $TMP_OUTPUT | sed 's/^ *//g' | cut -d ' ' -f1)

_log "INFO: SUMMARY of changes since last sync:"
_log "INFO: Added: [$ADD_COUNT] - Deleted: [$DEL_COUNT] - Moved: [$MOVE_COUNT] - Copied: [$COPY_COUNT] - Updated: [$UPDATE_COUNT]"

# set pre-hash option
PREHASH_VALUE=$(omv_config_get "/config/services/snapraid/prehash")
if $DEBUG; then
	_log "DEBUG: Pre-Hash option: ${PREHASH_VALUE}"
fi

# check if files have changed
if [ $DEL_COUNT -gt 0 ] || [ $ADD_COUNT -gt 0 ] || [ $MOVE_COUNT -gt 0 ] || [ $COPY_COUNT -gt 0 ] || [ $UPDATE_COUNT -gt 0 ]; then

	UPD_THRESHOLD=$(omv_config_get "/config/services/snapraid/updthreshold")
	DEL_THRESHOLD=$(omv_config_get "/config/services/snapraid/delthreshold")

	# New Logic for v 1.2.0 - check whether update threshold is enabled or not
	if [ $UPD_THRESHOLD -gt 0 ]; then
		# Update threshold is enabled; check if count exceeds threshold
		if [ $UPDATE_COUNT -gt $UPD_THRESHOLD ]; then
			# Too many files updated; inform user and don't proceed with sync
			_log "INFO: Number of updated files ($UPDATE_COUNT) exceeded threshold ($UPD_THRESHOLD). NOT proceeding with sync job."
			_log "INFO: Please run sync manually if this is not an error condition."
			echo "Number of updated files ($UPDATE_COUNT) exceeded threshold ($UPD_THRESHOLD). NOT proceeding with sync job." >> $TMP_OUTPUT
			echo "Please run sync manually if this is not an error condition" >> $TMP_OUTPUT

			# Increment check variable
			UPD_DEL_CHECK=$((UPD_DEL_CHECK+1))
		else
			# Update threshold is set but not exceeded.  Inform user that UPDATE_COUNT is below threshold and allow to proceed
			_log "INFO: Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,C-$COPY_COUNT,U-$UPDATE_COUNT] -> updated files ($UPDATE_COUNT) is below threshold ($UPD_THRESHOLD)."
			echo "Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,C-$COPY_COUNT,U-$UPDATE_COUNT] -> updated files ($UPDATE_COUNT) is below threshold ($UPD_THRESHOLD)." >> $TMP_OUTPUT
		fi
	else
		# UPD_THRESHOLD is disabled, so we reflect this information in email and log
		_log "INFO: Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,C-$COPY_COUNT,U-$UPDATE_COUNT] -> there are updated files ($UPDATE_COUNT) but update threshold ($UPD_THRESHOLD) is disabled."
		echo "Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,C-$COPY_COUNT,U-$UPDATE_COUNT] -> there are updated files ($UPDATE_COUNT) but update threshold ($UPD_THRESHOLD) is disabled." >> $TMP_OUTPUT
	fi

	# New Logic for v 1.2.0 - check whether delete threshold is enabled or not
	if [ $DEL_THRESHOLD -gt 0 ]; then
		# Delete threshold is enabled; check if count exceeds threshold
		if [ $DEL_COUNT -gt $DEL_THRESHOLD ]; then
			# Too many files deleted; inform user and don't proceed with sync
			_log "INFO: Number of deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD). NOT proceeding with sync job."
			_log "INFO: Please run sync manually if this is not an error condition."
			echo "Number of deleted files ($DEL_COUNT) exceeded threshold ($DEL_THRESHOLD). NOT proceeding with sync job." >> $TMP_OUTPUT
			echo "Please run sync manually if this is not an error condition" >> $TMP_OUTPUT

			# Increment check variable
			UPD_DEL_CHECK=$((UPD_DEL_CHECK+2))
		else
			# Delete threshold is set but not exceeded.  Inform user that DEL_COUNT is below threshold and allow to proceed
			_log "INFO: Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,C-$COPY_COUNT,U-$UPDATE_COUNT] -> deleted files ($DEL_COUNT) is below threshold ($DEL_THRESHOLD)."
			echo "Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,C-$COPY_COUNT,U-$UPDATE_COUNT] -> deleted files ($DEL_COUNT) is below threshold ($DEL_THRESHOLD)." >> $TMP_OUTPUT
		fi
	else
		# DEL_THRESHOLD is disabled, so we reflect this information in email and log
		_log "INFO: Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,C-$COPY_COUNT,U-$UPDATE_COUNT] -> there are deleted files ($DEL_COUNT) but delete threshold ($DEL_THRESHOLD) is disabled."
		echo "Changes detected [A-$ADD_COUNT,D-$DEL_COUNT,M-$MOVE_COUNT,C-$COPY_COUNT,U-$UPDATE_COUNT] -> there are deleted files ($DEL_COUNT) but delete threshold ($DEL_THRESHOLD) is disabled." >> $TMP_OUTPUT
	fi

	# Proceed with sync if no check errors
	if [ $UPD_DEL_CHECK -eq 0 ]; then
				# start snapraid SYNC
		_log "INFO: "
		_log "INFO: SnapRAID SYNC Job started"
		_log "INFO: ----------------------------------------"
		echo "" >> $TMP_OUTPUT
		echo "SnapRAID SYNC Job started - $(date)" >> $TMP_OUTPUT
		echo "----------------------------------------" >> $TMP_OUTPUT

		# we should eventually grep %
		if [ ${PREHASH_VALUE} -eq 1 ]; then
			/usr/bin/snapraid sync --conf ${SNAPRAID_CONF} --pre-hash >> $TMP_OUTPUT
		else
			/usr/bin/snapraid sync --conf ${SNAPRAID_CONF} >> $TMP_OUTPUT
		fi
		#wait for the job to finish
		wait

		_log "INFO: ----------------------------------------"
		_log "INFO: SnapRAID SYNC Job finished"
		echo "SnapRAID SYNC Job finished - $(date)" >> $TMP_OUTPUT
		echo "----------------------------------------" >> $TMP_OUTPUT

		# start snapraid SCRUB
		if [ $RUN_SCRUB = "true" ]; then
			if [ $COUNTER -lt $SCRUB_FREQUENCY_IN_DAYS ]; then
				_log "INFO: SnapRAID SCRUB-Cycle count ($SCRUB_FREQUENCY_IN_DAYS) not met ($COUNTER). No scrub was run."
				echo "SnapRAID SCRUB-Cycle count ($SCRUB_FREQUENCY_IN_DAYS) not met ($COUNTER). No scrub was run. - $(date)" >> $TMP_OUTPUT
				echo "$COUNTER" > $TMP_SCRUB_DIR/snapraid_scrub_counter
			else
				_log "INFO: SnapRAID SCRUB Job started"
				_log "INFO: ----------------------------------------"
				echo "SnapRAID SCRUB Job started - $(date)" >> $TMP_OUTPUT
				echo "----------------------------------------" >> $TMP_OUTPUT
				/usr/bin/snapraid scrub --conf ${SNAPRAID_CONF} -p ${SCRUB_PERCENT} -o ${SCRUB_OLDER_THAN_DAYS} >> $TMP_OUTPUT

				# wait for the job to finish
				wait

				# Reset counter
				echo "1" > $TMP_SCRUB_DIR/snapraid_scrub_counter

				_log "INFO: ----------------------------------------"
				_log "INFO: SnapRAID SCRUB Job finished"
				echo "SnapRAID SCRUB Job finished - $(date)" >> $TMP_OUTPUT
				echo "----------------------------------------" >> $TMP_OUTPUT
			fi
		else
			   echo "Array scrubbing is not enabled.  If you would like to enable it, please set the RUN_SCRUB option to true."  >> $TMP_OUTPUT
			   _log "INFO: Array scrubbing is not enabled.  If you would like to enable it, please set the RUN_SCRUB option to true."
		fi
		
		# Send success email
		_send_email "$EMAIL_SUBJECT_PREFIX - Sync/Scrub Job COMPLETED" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"
	
	# If update threshold exceeded
	elif [ $UPD_DEL_CHECK -eq 1 ]; then
		_send_email "$EMAIL_SUBJECT_PREFIX - WARNING - Number of updated files (${UPDATE_COUNT}) exceeded threshold (${UPD_THRESHOLD})" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"
	
	# If delete threshold exceeded
	elif [ $UPD_DEL_CHECK -eq 2 ]; then
		_send_email "$EMAIL_SUBJECT_PREFIX - WARNING - Number of deleted files (${DEL_COUNT}) exceeded threshold (${DEL_THRESHOLD})" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"

	# IF both thresholds exceeded (UPD_DEL_CHECK -eq 3)
	else
		_send_email "$EMAIL_SUBJECT_PREFIX - WARNING - Number of updated (${UPDATE_COUNT}) and deleted (${DEL_COUNT}) files exceeded updated (${UPD_THRESHOLD}) and deleted (${DEL_THRESHOLD}) thresholds" "${TMP_OUTPUT}" "${EMAIL_SUBJECT_PREFIX}" "${OMV_MAIL_sender}"
	fi
	_check_success && _rmtmp
else
	# NO, so lets log it and exit
	_log "INFO: No change detected. Nothing to do"
fi

_log "INFO: SnapRAID Job ended."
_rmtmp
exit 0
