#!/bin/bash
#########################################################
# Overwrites the input style setting for all devices         
# Usage: ./inputstyle-overwrite                      
#                  
# Changelog:                                      
# rpm80 - initial creation
# rpm02 - config.dtb restore
#########################################################

# 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

plog "Stopping DTB"
crm resource stop rsc4k_dtb
waitDTBstop
sleep 5 # wait for backup to finish
	
plog "Updating the input style"
style=$(readConfigXML "inputstyle")
if [[ "$style" == "D" ]]; then
	plog "Forcing all devices for DTB style"
	sed -i '3~6 s/S/D/' /var/pgsql/dtb_backup/DTBServer/config.dtb
elif [[ "$style" == "S" ]]; then
	plog "Forcing all devices for SMS style"
	sed -i '3~6 s/D/S/' /var/pgsql/dtb_backup/DTBServer/config.dtb
else
	plog "Could not read the inputstyle, config.dtb cannot be updated"
fi

# copy the config.dtb into winedir. The dtb-backup may give false positive for restore.
BACKUP_DIR=/var/pgsql/dtb_backup
plog "Restore config.dtb from $BACKUP_DIR into $DTB_DRBD_SHARE/DTBServer/winedir"
chmod -R 777 $BACKUP_DIR
chown -R dtbuser:users $BACKUP_DIR
sudo -u dtbuser cp $BACKUP_DIR/DTBServer/config.dtb $DTB_DRBD_SHARE/DTBServer/winedir; [ $? -eq 0 ] && plog "config.dtb restored." || plog "config.dtb restore failed."

plog "Starting DTB"
crm resource start rsc4k_dtb


