##
# Project: one.migration: Scripts for the migration from opennebula 1.4 to 2.0.1 
# Description:  This bash source file contains functions for the SQL migration.
#   
# @author Marlon Nerling, Abacus Research AG, 9301 Wittenbach, Schweiz http://www.abacus.ch Copyright (C) 2010.
#   
# @see The GNU Public License (GPL)
#
## 
# This program is free software; you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by 
# the Free Software Foundation; either version 2 of the License, or 
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
# for more details.
# 
# You should have received a copy of the GNU General Public License along 
# with this program; if not, write to the Free Software Foundation, Inc., 
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

ORIGINAL_DB=${ORIGINAL_DB?"Variable ORIGINAL_DB may be set"}
ONEMIGRATION_COMMON=${ONEMIGRATION_COMMON?"ONEMIGRATION_COMMON source (one.migration.common.sh) was not sourced"}

vm.get_insert_text(){
	local vm_id=${1?"[FAIL]:$FUNCNAME waits for a vm id as parameter"};
	local oldfields='oid, uid, name, last_poll, state, lcm_state, stime, etime, deploy_id, memory ,cpu, net_tx, net_rx'
	
	local oldvalues=`query_sqlite_prepare " SELECT $oldfields FROM vm_pool WHERE oid=$vm_id ;"`
	[ -z "${oldvalues}" ] && return 
	local allfields="$oldfields, cluster, template"
	local allvalues="$oldvalues, 0, '`vm.get_template_text $vm_id`'"
	
	echo "INSERT INTO vm_pool ( $allfields ) VALUES ( $allvalues );"
}

vm.get_template_text(){
	local vm_id=${1?"[FAIL]:$FUNCNAME waits for a vm id as parameter"}
	previa=`query_sqlite "SELECT id FROM vm_attributes WHERE value=$vm_id AND name='VMID';"`
	template_text='<TEMPLATE>';
	local NIC_count=0
	local HD_count=0
	while read complet 
	do
		varia="${complet%%=*}"
#		debug varia=\"$varia\"
		#if [ "$varia" == NIC ]   
		if [ "$varia" == DISK ] || [ "$varia" == NIC ] || [ "$varia" == GRAPHICS ] 
		then
			template_text=$template_text"
    <$varia>"
			[ "$varia" == NIC ] && template_text=$template_text"<NETWORK_ID><![CDATA[$NIC_count]]></NETWORK_ID>" && ((NIC_count++))
			[ "$varia" == DISK ] && template_text=$template_text"<DISK_ID><![CDATA[$HD_count]]></DISK_ID>" && ((HD_count++))
			while read recomplet
			do
				[ -z "$recomplet" ] && continue
				revaria="${recomplet%%=*}"
				revalue="${recomplet#*=}"
#				debug $revaria=$revalue
				this_text="
       <$revaria>
          <![CDATA[$revalue]]>
       </$revaria>"
				template_text=$template_text$this_text
			done < <(  echo ${complet#*=} | awk -F'\@\^_\^\@' '{print $1"\n "$2"\n "$3"\n "$4"\n "$5"\n "$6}' ) 
			template_text=$template_text"
    </$varia>"
		else
			value="${complet#*=}"
			template_text=$template_text"
    <$varia>
       <![CDATA[$value]]>
    </$varia>"
		fi
	done < <( get_attributes vm_attributes $previa )
	template_text=$template_text"
</TEMPLATE>";
	echo -n ${template_text} | sed -e 's:>\ :>:g'
#	sed -e 's:$\ \ \ ::g'  << EOF
#${template_text}
#EOF
}
