#!/bin/bash
############################################
# dtb-reconfigure
#
# Helper script to reconfigure a running DTB application. DTB will be restarted when anything changes with the ini files or with the import directory.
# Usage: ./dtb-reconfigure
#
# Changelog:
# rpm67 - moving the wsi-gendb before change-verification, otherwise if the kunde is unchanged, the wsi db will not be sent to SG
# rpm69 - OSFOURK-18160 - restarting dtb anyway (removing the check of the file change of the database)
# rpm75 - OSFOURK-19895 - verifying the empty import.
############################################

# 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

IMPORT_DIR="/var/pgsql/dtb/DTBServer/import" # the Database files are imported here
SERVER_WINE_DIR="/var/pgsql/dtb/DTBServer/winedir"
BACKUP_DIR="/var/pgsql/dtb_backup"

import_empty=0;

# Case 1: if DTB was started before => a winedir exists.
if [ -d "$SERVER_WINE_DIR" ]; then
	plog "DTB is installed."
	
	# If the import directory is not empty, then this is a DB import.
	if [ "$(ls -A $IMPORT_DIR)" ]; then
		plog "The import directory was not empty, verifying the mandatory database files..."
		
		# Check the mandatory files
		if [ ! -f $IMPORT_DIR/kunde.nam ] || [ ! -f $IMPORT_DIR/kunde.rnr ] || [ ! -f $IMPORT_DIR/kunde.t9i ]; then
			plog "The mandatory files are missing. Clearing the import directory and return."
			sudo -u $DTB_USER rm -r $IMPORT_DIR/* 1>/dev/null 2>/dev/null
			if [ $config_changed == 0 ]; then
				plog "exiting script"
				exit 4
			fi
		else
			plog "The mandatory files are present."
		fi
		
	else
		plog "The import directory is empty."
		import_empty=1;
	fi
	
# Case 2: if DTB was not started before, but a backup directory exits
elif [ -d "$BACKUP_DIR" ]; then
	plog "DTB is not installed, but the backup directory is present"
	
	# If the import directory is not empty, then this is a DB import.
	if [ "$(ls -A $IMPORT_DIR)" ]; then
		plog "The import directory was not empty, verifying the mandatory database files..."
		# Check the mandatory files
		if [ ! -f $IMPORT_DIR/kunde.nam ] || [ ! -f $IMPORT_DIR/kunde.rnr ] || [ ! -f $IMPORT_DIR/kunde.t9i ]; then
			plog "The mandatory files are missing. Clearing the import directory and return."
			sudo -u $DTB_USER rm -r $IMPORT_DIR/* 1>/dev/null 2>/dev/null
			if [ $config_changed == 0 ]; then
				plog "exiting script"
				exit 4
			fi
		else
			plog "The mandatory files are present."
		fi
	else
		plog "The import directory is empty."
		import_empty=1;
	fi
	
# Case 3: if DTB was never started, nothing exists yet.
else
	plog "DTB is not installed. Nothing to do."
	exit 5
fi

# the language and sync mode can also be affected when dtb-reconfigure is called
plog "Checking for synchronization and language setup changes..."
sync_c=$(cat $DTB_SYNC_FLAG_FILE) # current sync config
lang_c=$(cat $DTB_LANG_FLAG_FILE) # current lang config
sync_i=$(/opt/webservice/scripts/gethaipsetupentry.sh system.dtb-sync)
lang_i=$(/opt/webservice/scripts/gethaipsetupentry.sh system.dtb-lang)

#Generate the database file for WSI (manual import, LDAP, SFTP)
/opt/dtb/bin/wsi-gendb

# When something changes, the DTB will stop and all files will be backed up into backup dir. 
# After the DTB starts, the files will be restored from backup dir into winedir. => Import to backup dir.

isDTBrunning=$(crm resource status rsc4k_dtb 2>&1)
restart=0
if [[ $isDTBrunning == "resource rsc4k_dtb is NOT running" ]]; then
	plog "DTB is stopped. The changes will apply on startup."
	restart=0
else
	plog "The DTB needs to be restarted after the import."
	crm resource stop rsc4k_dtb
	waitDTBstop
	restart=1
	# now the winedir files are backed up. The next step is to overwrite them.
fi

# If we get here, then the import is ready to start
if [ $import_empty == 0 ]; then
	plog "Importing files..."
	sudo -u $DTB_USER mv -f $IMPORT_DIR/*.nam $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .nam imported" || plog ".nam not found in import"
	sudo -u $DTB_USER mv -f $IMPORT_DIR/*.nax $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .nax imported" || plog ".nax not found in import"
	sudo -u $DTB_USER mv -f $IMPORT_DIR/*.rnr $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .rnr imported" || plog ".rnr not found in import"
	sudo -u $DTB_USER mv -f $IMPORT_DIR/*.rnx $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .rnx imported" || plog ".rnx not found in import"
	sudo -u $DTB_USER mv -f $IMPORT_DIR/*.t9i $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .t9i imported" || plog ".t9i not found in import"
	sudo -u $DTB_USER mv -f $IMPORT_DIR/*.dtb $BACKUP_DIR/DTBServer; [ $? -eq 0 ] && plog "all .dtb imported" || plog ".dtb not found in import"
	plog "Import finished."
fi
# The ini files will be imported at the DTB start in dtb-wrapper script.

if [ $restart == 1 ]; then
    plog "Restarting DTB to apply the changes."
    crm resource start rsc4k_dtb
else
	plog "The imported files will apply on startup."
fi

if [ $import_empty == 1 ]; then
	exit 3
fi
	
exit 2
