#!/bin/bash
#########################################################
# Converts old config into xml. Called at RPM installation.          
# Usage: ./dtb-upgrade                               
#                  
# Changelog:                                      
# rpm81 - initial creation
# rpm2.0-1 - creating xml only if doesn't exist
#########################################################

# 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 "Starting old configuration convertion after RPM installation..."
configdir="/var/pgsql/dtb_backup/config"
tmpdir="/var/pgsql/dtb_backup/tmp"

# Restores the XML files for the webpage, to have the latest config preset before DTB is installed.
function RestoreXML()
{
	plog "Restoring the xml files into dtb_config, for webpage"
	if [ -f $configdir/DTBconfigFile.xml ]; then
		cp $configdir/DTBconfigFile.xml /var/pgsql/dtb_config/DTBconfigFile.xml
		chmod -R 666 /var/pgsql/dtb_config/DTBconfigFile.xml
	fi
	if [ -f $configdir/wsi.xml ]; then
		cp $configdir/wsi.xml /var/pgsql/dtb_config/wsi.xml
		chmod -R 755 /var/pgsql/dtb_config/wsi.xml
	fi
}

if ([ ! -d $tmpdir ] && [ ! -d $configdir ]); then
	plog "No backup configuration exists. Exiting script..."
	exit 0
else
	if [ -d $tmpdir ]; then
		plog "Renaming dtb_backup/tmp to dtb_backup/config"
		cp -frp $tmpdir -T $configdir
		rm -r $tmpdir/*
		rmdir $tmpdir
	fi
	xml_file="$configdir/DTBconfigFile.xml"
	inputstyleFile="$configdir/inputstyle"
	managerFile="$configdir/manager"
	sftpFile="$configdir/sftpserver"
	if ([ -f $inputstyleFile ] || [ -f $managerFile ] || [ -f $sftpFile ]); then
		plog "Configuration files found. Starting the conversion."
	else
		plog "No configuration file found. Exiting script."
		RestoreXML
		exit 0
	fi
fi

# Starting convertion to XML
index=0;
synctime=$(/opt/webservice/scripts/gethaipsetupentry.sh system.dtb-syncTime)

my_array[0]="";
my_array[1]="";
my_array[2]="";
my_array[3]="";
my_array[4]="$synctime";
my_array[5]="";
my_array[6]="";	
my_array[7]="";	
my_array[8]="D";

#sftp data
if [ ! -f $sftpFile ]; then
	index=5;
else
	plog "SFTP configuration found, converting data..."
	while IFS= read -r line; do
		my_array[$index]=$line;
		index=$index+1;
	done < "$sftpFile"
	rm -r $sftpFile
fi

#manager data
if [ -f $managerFile ]; then
	plog "Manager configuration found, converting data..."
	while IFS= read -r line; do
		my_array[$index]=$line;
		index=$index+1;
	done < "$managerFile"
	rm -r $managerFile
fi
	
if [ -f $inputstyleFile ]; then
	plog "Inputstyle configuration found, converting data..."
	my_array[8]="$(cat $inputstyleFile)"
	rm -r $inputstyleFile
fi

if [ ! -f $xml_file ]; then

	plog "Creating XML file"

	#create XML
	touch "$xml_file"
	chmod 755 $xml_file
	
	#fill in with data, if it works
	echo '<?xml version="1.0" encoding="UTF-8"?>' >> "$xml_file"
	echo '<imported>' >> "$xml_file"
	echo '	<imported-sync></imported-sync>' >> "$xml_file"
	echo '	<imported-language></imported-language>' >> "$xml_file"
	echo '	<imported-time></imported-time>' >> "$xml_file"
	echo '</imported>' >> "$xml_file"
	echo '<inputstyle>'${my_array[8]}'</inputstyle>' >> "$xml_file"
	echo '<sftp>' >> "$xml_file"
	echo '	<sftp-ip>'${my_array[0]}'</sftp-ip>' >> "$xml_file"
	echo '	<sftp-path>'${my_array[3]}'</sftp-path>' >> "$xml_file"
	echo '	<sftp-user>'${my_array[1]}'</sftp-user>' >> "$xml_file"
	echo '	<sftp-password>'${my_array[2]}'</sftp-password>' >> "$xml_file"
	echo '	<sftp-time>'${my_array[4]}'</sftp-time>' >> "$xml_file"
	echo '</sftp>' >> "$xml_file"
	echo '<manager>' >> "$xml_file"
	echo '	<manager-ip>'${my_array[5]}'</manager-ip>' >> "$xml_file"
	echo '	<manager-user>'${my_array[6]}'</manager-user>' >> "$xml_file"
	echo '	<manager-password>'${my_array[7]}'</manager-password>' >> "$xml_file"
	echo '</manager>' >> "$xml_file"
	echo '<ldap>' >> "$xml_file"
	echo '	<ldap-ip></ldap-ip>' >> "$xml_file"
	echo '	<ldap-port></ldap-port>' >> "$xml_file"
	echo '	<ldap-user></ldap-user>' >> "$xml_file"
	echo '	<ldap-password></ldap-password>' >> "$xml_file"
	echo '	<ldap-baseDN></ldap-baseDN>' >> "$xml_file"
	echo '	<ldap-searchFilter></ldap-searchFilter>' >> "$xml_file"
	echo '	<ldap-name></ldap-name>' >> "$xml_file"
	echo '	<ldap-telephoneNumber></ldap-telephoneNumber>' >> "$xml_file"
	echo '	<ldap-time>02:00</ldap-time>' >> "$xml_file"
	echo '	<wsi-firstName></wsi-firstName>' >> "$xml_file"
	echo '	<wsi-surname></wsi-surname>' >> "$xml_file"
	echo '	<wsi-mobile></wsi-mobile>' >> "$xml_file"
	echo '	<wsi-homePhone></wsi-homePhone>' >> "$xml_file"
	echo '	<wsi-organization></wsi-organization>' >> "$xml_file"
	echo '	<wsi-fax></wsi-fax>' >> "$xml_file"
	echo '	<wsi-city></wsi-city>' >> "$xml_file"
	echo '	<wsi-email></wsi-email>' >> "$xml_file"
	echo '	<wsi-group></wsi-group>' >> "$xml_file"
	echo '</ldap>' >> "$xml_file"

	plog "Convertion finished"
	
else
	plog "XML file already exist. Skipping the upgrade of the XML file"
fi

plog "Cleaning up the old configuration files..."
if [ -d /var/tmp/dtb ]; then
	plog "Deleting /var/tmp/dtb"
	rm -r "/var/tmp/dtb"/*
	rmdir "/var/tmp/dtb"
fi

RestoreXML

plog "Cleanup finished. Exiting the script."
exit 0
