#!/bin/bash
############################################
# Synchronisation status checker for Assistant and Manager synchronizations.
# Usage: ./dtb-syncstatus
#
# Copyright (c) 2017 Unify GmbH & Co. KG
# Demeter Aron, Kemenes Robert 
# aron.demeter.ext@unify.com, robert.kemenes.ext@unify.com
#
# rpm69 - script is called from DTB after sync with Assistant or Manager
#		- adding chmod 777 for syncstatus
############################################

# Import definitions
CMD=$(basename $0)
if [ -f /opt/dtb/bin/_common_definitions ]; then
    . /opt/dtb/bin/_common_definitions
else
	logger -t $CMD -p ERROR -s "The /opt/dtb/bin/_common_definitions file does not exist, exiting the script..."
	exit 1
fi

# Verify if the syncstatus file exsists. If not create it.
if [ ! -f $DTB_SYNCSTATUS ]; then
	echo "" > $DTB_SYNCSTATUS
	plog "File syncstatus created."
fi

chmod 777 $DTB_SYNCSTATUS

syncsetting=$(tr -d '\0' </var/opt/dtb_sync)
output=$(tr -d '\0' </var/pgsql/dtb/DTBMaster/winedir/SyncTrig.cfg)
ATIME=$(stat -c "%y" /var/pgsql/dtb/DTBMaster/winedir/SyncTrig.cfg) # the last modification date can be the result also of a parameter setting action
ATIME=$(echo "$ATIME" | rev | cut -c 17- | rev) # remove last 17 characters
plog "Checking the synchronization status."

if [ $output == "0" ]; then # No synchronisation action currently running, last sync was ok. 
	if [ -s /var/pgsql/dtb/DTBMaster/winedir/kunde.nam ]; then #OSFOURK-15646, check if we have data in kunde.nam
		if [ $syncsetting == "1" ]; then
			message="Successfully completed at $ATIME with Assistant (PERSDAT)."
		elif [ $syncsetting == "4" ]; then
			message="Successfully completed at $ATIME with Assistant (DISPLAY_NAME)."
		elif [ $syncsetting == "2" ]; then
			message="Successfully completed at $ATIME with Manager (PERSDAT)."
		elif [ $syncsetting == "5" ]; then
			message="Successfully completed at $ATIME with Manager (DISPLAY_NAME)."
		fi
	else #OSFOURK-15646
		if [ $syncsetting == "1" ]; then
			message="Action completed at $ATIME with Assistant (PERSDAT). WARNING: There was no synchronisation; this may be caused by the fact that there is no valid database present on the destination server."
		elif [ $syncsetting == "4" ]; then
			message="Action completed at $ATIME with Assistant (DISPLAY_NAME). WARNING: There was no synchronisation; this may be caused by the fact that there is no valid database present on the destination server."
		elif [ $syncsetting == "2" ]; then
			message="Action completed at $ATIME with Manager (PERSDAT). WARNING: There was no synchronisation; this may be caused by the fact that there is no valid database present on the destination server."
		elif [ $syncsetting == "5" ]; then
			message="Action completed at $ATIME with Manager (DISPLAY_NAME). WARNING: There was no synchronisation; this may be caused by the fact that there is no valid database present on the destination server."
		fi
	fi
elif [ $output == "2" ]; then # Sync is running.
	message="Still running..."
elif [ $output == "3" ]; then # No Sync,Last sync failed.
	if [ $syncsetting == "1" ]; then
		message="Failed at $ATIME with Assistant (PERSDAT)."
	elif [ $syncsetting == "4" ]; then
		message="Failed at $ATIME with Assistant (DISPLAY_NAME)."
	elif [ $syncsetting == "2" ]; then
		message="Failed at $ATIME with Manager (PERSDAT)."
	elif [ $syncsetting == "5" ]; then
		message="Failed at $ATIME with Manager (DISPLAY_NAME)."
	fi
fi
# The SFTP and LDAP case is done in the dtb-sftp and dtb-ldap scripts

plog $message
echo "$message" > "$DTB_SYNCSTATUS"

