#!/bin/bash
############################################
# Helper script to collect the DTB files from winedir and to put them into /var/log/dtb_log
# Usage: ./dtb-collect
# 
# Changelog:
# rpm77 - moving from /var/tmp/dtb into /var/pgsql/dtb_config/
############################################

# 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

LOG_DIR="/var/log/dtb_log"
restart="0"

# Stop DTB if it is running
/opt/dtb/bin/dtb-wrapper DTBServer status 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
		plog "DTB is running. Stopping DTB..."
		crm resource stop rsc4k_dtb
		waitDTBstop
		restart="1"
else
		plog "DTB is not running."
fi

# Create the dtb_log where the logs will be copied
if [ ! -d "$LOG_DIR" ]; then
	 plog "$LOG_DIR does not exists. Creating it now."
     mkdir -p $LOG_DIR
else
	 plog "$LOG_DIR exists. Deleting it's content."
	 rm -r $LOG_DIR/*
	 plog "Deletion finished."
fi

# Copy both winedirs into /var/log/dtb_log except the exe files
plog "Copying DTB logs into $LOG_DIR..."
rsync -av --exclude='*.exe' --exclude='*.EXE' /var/pgsql/dtb/DTBServer/winedir $LOG_DIR/DTBServer;
rsync -av --exclude='*.exe' --exclude='*.EXE' /var/pgsql/dtb/DTBMaster/winedir $LOG_DIR/DTBMaster; 
rsync -av --exclude='*.exe' --exclude='*.EXE' $DTB_CFGDIR $LOG_DIR/Config; 
plog "File copy finished."

if [ $restart == "1" ]; then
    plog "DTB was running. Restarting DTB..."
    crm resource start rsc4k_dtb
else
	plog "DTB wasn't running. Nothing to do."
fi
