#!/bin/bash

# check if /tmp/emailcheck.sms exists, if so, send its content to ignace's phone
FILE_ALL='/tmp/emailcheck.*'
FILE_SMS='/tmp/emailcheck.sms'
FILE_WARNING='/tmp/emailcheck.warning'
FILE_ESCALATE='/tmp/emailcheck.ESCALATE'
TEXT=''

echo $(date)

function sendsms {
    echo "  SMS: $1 $TEXT"
    clicksend sms send \
      --to "$1" \
      --body "$TEXT" \
      --csuser 'ignace.suy@gmail.com' \
      --cstoken 'BDAED8AF-29CF-F730-145F-0864C7D86813' >/dev/null 2>&1
}


if [ -f "$FILE_SMS" ]; then
    echo "  sms file found"
    TEXT="$(cat $FILE_SMS)"
    rm -f $FILE_SMS
    sendAlertMail.sh "$TEXT"
    if [ -f "$FILE_ESCALATE" ] ; then
	# do this, then restart as if it never happened
	rm -rf "$FILE_ALL"
	echo "  sending escalation messages"
	sendsms '+31655195180'
	# sendsms '+31624276531'  # janine
	# sendsms '+31624593799'  # xander
	# sendsms '+31621301914'  # querijn
	
	# send emails to all involved
	python3 /opt/Monitor/monitor/sendEscalationMail.py
	/usr/local/bin/enableKatja
    else
        if [ -f "$FILE_WARNING" ] ; then
            echo "  sending warning message"
	    sendsms '+31655195180' 
        fi
    fi
fi

