#!/bin/bash

#
# s  |   m    |   f/u     |      files
# 000|0 0000 0|000 0000 00|00 0000 0000 0000
# 8     64       512            16,384


declare -i xdb_l_servers=0
declare -a xdb_servers
declare -i xdb_current_server=0
declare -i xdb_offset_server=3

declare -i xdb_l_mods=0
declare -a xdb_mods
declare -i xdb_current_mod=0
declare -i xdb_offset_mod=6

declare -i xdb_l_insts=0
declare -a xdb_insts
declare -i xdb_current_inst=0
declare -i xdb_offset_inst=9

declare -i xdb_l_files=0
declare -a xdb_files
declare -i xdb_current_file=0
declare -i xdb_offset_file=14


declare -a xdb_elem_serv
declare -a xdb_elem_mod
declare -a xdb_elem_inst
declare -a xdb_elem_file

# Function return vale
xdb_funcret=""




xdb_debug=0

#
# add_server path
#
function xdb_add_server() {
	if [ x"$1" = 'x' ] ; then return 1; fi

	xdb_servers[xdb_l_servers]="$xdb_l_servers:$1"
#echo "added ${xdb_servers[$xdb_l_servers]}"
	xdb_current_server=$xdb_l_servers

	#old=${xdb_elem_serv[$xdb_l_servers]}
	xdb_elem_serv[xdb_current_server]=0

	xdb_l_servers=$((xdb_l_servers+1))
}

#
# add_mod name
#
function xdb_add_mod() {
	if [ x"$1" = 'x' ] ; then return 1; fi

	local index
	local old

	# assign this to the current server
	index=$((xdb_current_server<<xdb_offset_mod))
	# add the mod index
	index=$((index+xdb_l_mods))

	xdb_mods[index]="$index:$1"
#echo "added ${xdb_mods[$index]}"
	xdb_current_mod=$index


	old=${xdb_elem_serv[$xdb_current_server]}
	xdb_elem_serv[xdb_current_server]=$((old+1))
	xdb_elem_mod[xdb_current_mod]=0

	xdb_l_mods=$((xdb_l_mods+1))
}

#
# xdb_add_inst full|upd version
#
function xdb_add_inst() {
	if [ x"$1" != 'xfull' -a x"$1" != 'xupd' ] ; then return 1; fi
	if [ x"$2" = 'x' ] ; then return 2; fi

	local index
	local old

	# assign this to the current mod
	#echo "index = $xdb_current_mod << $xdb_offset_inst"
	index=$((xdb_current_mod<<xdb_offset_inst))
	#echo "$index = $((xdb_current_mod<<xdb_offset_inst))"
	# add the install index
	#echo "index = $index + $xdb_l_insts"
	index=$((index+xdb_l_insts))
	#echo "$index = $((index+xdb_l_insts))"

	xdb_insts[index]="$index:$1:$2"
#echo "added ${xdb_insts[$index]}"
	xdb_current_inst=index
	
	old=${xdb_elem_mod[$xdb_current_mod]}
	xdb_elem_mod[xdb_current_mod]=$((old+1))
	xdb_elem_inst[xdb_current_inst]=0

	xdb_l_insts=$((xdb_l_insts+1))
}


#
# xdb_add_file type name path version
#
function xdb_add_file() {
	if [ x"$1" != 'xdll' -a x"$1" != 'xscript' -a x"$1" != 'xconfig' ] ; then return 1; fi
	if [ x"$2" = 'x' ] ; then return 2; fi
	if [ x"$3" = 'x' ] ; then return 3; fi
	if [ x"$4" = 'x' ] ; then return 4; fi

	local index
	local old

	# assign this to the current install
	#echo "index = $xdb_current_inst << $xdb_offset_file"
	index=$((xdb_current_inst<<xdb_offset_file))
	#echo "$index = $((xdb_current_inst<<xdb_offset_file))"
	# add the file index
	#echo "index = $index + $xdb_l_files"
	index=$((index+xdb_l_files))
	#echo "$index = $((index+xdb_l_files))"

	xdb_files[index]="$index:$1:$2:$3:$4"
#echo "added ${xdb_files[$index]}"
	xdb_current_file=index
	
	old=${xdb_elem_inst[$xdb_current_inst]}
	xdb_elem_inst[xdb_current_inst]=$((old+1))
	xdb_elem_file[xdb_current_file]=0

	xdb_l_files=$((xdb_l_files+1))
}

function decho(){
	if [ $xdb_debug -eq 1 ] ; then
		echo "echo $@"
	fi
}



function xdb_run() {
	echo "$* ;"
}

#
# parse an amidb file into a complete tree
#
function xdb_parse_full_tree() {
	if [ x"$1" = 'x' ] ; then return 1 ; fi

	local filename=$1

	# make sure the xdb_current_* cariables are initialized
	xdb_current_server=-1
	xdb_current_mod=-1
	xdb_current_inst=-1
	xdb_current_file=-1
	# parse the file line by line
	eval `cat $filename | while read line ; do
		# determine the node type
		if [ x"${line/*Server*/yes}" = 'xyes' ] ; then
			# this is a server tag
			if [ x"${line/*<Server*/yes}" = 'xyes' ] ; then
				# This must is an opening tag.
				# get the path
				server_path=${line#*<Server *path=\"}
				server_path=${server_path%\">}
				#xdb_add_server $server_path
				xdb_run "xdb_add_server $server_path"
			else
				# This must be a closing tag.
				xdb_run "xdb_current_server=-1"
			fi
		elif [ x"${line/*Mod*/yes}" = 'xyes' ] ; then
			# this is a mod tag
			if [ x"${line/*<Mod*/yes}" = 'xyes' ] ; then
				# This is an opening tag
				# get the name
				mod_name=${line#*<Mod *name=\"}
				mod_name=${mod_name%\">}
				#xdb_add_mod $mod_name
				xdb_run "xdb_add_mod $mod_name"
			else
				# This is a closing tag
				xdb_run "xdb_current_mod=-1"
			fi
		
		elif [ x"${line/*Install*/yes}" = 'xyes' ] ; then
			# this is an install tag
			if [ x"${line/*<Install*/yes}" = 'xyes'  ] ; then
				# This is an opening tag
				# get the parameters
				inst_type=${line#*<Install *type=\"}
				inst_type=${inst_type%%\"*}
				inst_ver=${line#*<Install *ver=\"}
				inst_ver=${inst_ver%%\"*}
				#xdb_add_inst $inst_type "$inst_ver"
				xdb_run "xdb_add_inst $inst_type \"$inst_ver\""
			else
				# This is a closing tag
				xdb_run "xdb_current_inst=-1"
			fi

		elif [ x"${line/*File*/yes}" = 'xyes' ] ; then
			# this is a file tag
			# get the parameters
			file_type=${line#*<File *type=\"}
			file_type=${file_type%%\"*}
			file_path=${line#*<File *path=\"}
			file_path=${file_path%%\"*}
			file_ver=${line#*<File *ver=\"}
			file_ver=${file_ver%%\"*}
			file_name=${line#*<File *>}
			file_name=${file_name%%</File*}
			#xdb_add_file $file_type $file_name $file_path $file_ver
			xdb_run "xdb_add_file $file_type $file_name $file_path $file_ver"
		fi
	done`
}




#
# Searching functions
#

#
# find_server path_glob
# e.g. find_server '/*/one'
# if no path glob is specified, the first server is returned
# returns list of indexes into server array or -1
#
function xdb_find_server() {
	local matches="all" 
	local pat="$1"

	if [ -z "$1" ] ; then 
		matches="first"
		pat='*'
	fi

	local server_index
	local server_path

	xdb_funcret=-1

	for s in ${xdb_servers[*]} ; do
		server_index=${s%*:*}
		server_path=${s#*:}
		#echo "index: $server_index path: $server_path get: ${server_path/$pat/yes}"
		if [ x"${server_path/$pat/yes}" = 'xyes' ] ; then
			if [ x"$matches" = 'xfirst' ] ; then
				xdb_funcret=$server_index
				break
			elif [ x"$xdb_funcret" = 'x-1' ] ; then
				xdb_funcret=$server_index
			else
				xdb_funcret="$xdb_funcret $server_index"
			fi
		fi
	done;


	if [ "$xdb_funcret" = '-1' ] ; then
		return 1
	fi

	return 0
}

#
# find_mod serv_index name_glob
# e.g. find_mod 1 'cstrike'
#
# Searches for a mod of name "name_glob" for server with index serv_index
# If don't care is used (X) for serv_index, a list of indexes for the matching
# mod for all servers is returned
# returns index into xdb_mods array or -1
# 
function xdb_find_mod() {
	local matches="one"	
	local serv_indx=-1
	local pat

	if [ -z "$2" ] ; then 
		matches="first"
		pat="$1"
	elif [ x"$1" = 'xX' ] ; then
		matches="all"
		pat="$2"
	else
		pat="$2"
		serv_indx="$1"
	fi


	local mod_index
	local mod_name
	local server_index

	xdb_funcret=-1

	for m in ${xdb_mods[*]} ; do
		mod_index=${m%*:*}
		mod_name=${m#*:}
		server_index=$((mod_index>>$xdb_offset_mod))
		#echo "index: $mod_index name: $mod_name server_index: $server_index get: ${mod_name/$pat/yes}"
		if [ x"${mod_name/$pat/yes}" = 'xyes' ] ; then
			if [ "$matches" = 'one' ] ; then
				if [ $server_index -eq $serv_indx ] ; then
					xdb_funcret=$mod_index
					break
				fi
			elif [ x"$matches" = 'xfirst' ] ; then
				xdb_funcret="$mod_index"
				break
			elif [ "$xdb_funcret" = '-1' ] ; then
				xdb_funcret=$mod_index
			else
				xdb_funcret="$xdb_funcret $mod_index"
			fi
		fi
	done;

	if [ "$xdb_funcret" = '-1' ] ; then
		return 1
	fi

	return 0
}


#
# find_install serv_index mod_index type ver_glob
# e.g. find_install 1 133 full 2.50.50
#
# Searches for an install of version "ver_glob" and type "type" for server with 
# index serv_index and mod with index mod_index. 
# If don't care is used (X) for serv_index and/or mod_index , a list of indexes
# for the matching install for all servers and mods is returned
# returns index into xdb_insts array or -1
# 
function xdb_find_install() {
	local serv_matches="one"	
	local mod_matches="one"	
	local serv_indx=-1
	local mod_indx=-1
	local type
	local pat

	if [ -z "$4" ] ; then
		if [ -z "$3" ] ; then
			serv_matches="first"
			mod_matches="first"
			type="$1"
			pat="$2"
		else
			serv_matches="first"
			mod_matches="first"
			type="$2"
			pat="$3"
		fi
	else
		if [ x"$1" = 'xX' ] ; then
			serv_matches="all"
		else
			serv_indx="$1"
		fi
		if [ x"$2" = 'xX' ] ; then
			mod_matches="all"
		else
			mod_indx="$2"
		fi

		type="$3"
		pat="$4"
	fi


	local inst_index
	local inst_ver
	local inst_type
	local server_index
	local mod_index

	xdb_funcret=-1

	for i in ${xdb_insts[*]} ; do
		inst_type=${i#*:}
		inst_type=${inst_type%*:*}
		inst_ver=${i##*:}
		inst_index=${i%*:*:*}
		mod_index=$((inst_index>>$xdb_offset_inst))
		server_index=$((mod_index>>$xdb_offset_mod))
		#echo "index: $inst_index ver: $inst_ver si: $server_index mi: $mod_index get: ${inst_ver/$pat/yes}"
		if [ x"${inst_ver/$pat/yes}" = 'xyes' -a x"${inst_type/$type/yes}" = 'xyes' ] ; then
			if [ "$serv_matches" = 'one' ] ; then
				if [ $server_index -eq $serv_indx ] ; then
					if [ "$mod_matches" = 'one' ] ; then
						if [ $mod_index -eq $mod_indx ] ; then
							xdb_funcret=$inst_index
							break
						fi

					elif [ x"$mod_matches" = 'xfirst' ] ; then
						xdb_funcret="$inst_index"
						break
					elif [ "$xdb_funcret" = '-1' ] ; then
						xdb_funcret=$inst_index
					else
						xdb_funcret="$xdb_funcret $inst_index"
					fi
				fi
			elif [ x"$serv_matches" = 'xfirst' ] ; then
				if [ "$mod_matches" = 'one' ] ; then
					if [ $mod_index -eq $mod_indx ] ; then
						xdb_funcret=$inst_index
						break
					fi
				elif [ x"$mod_matches" = 'xfirst' ] ; then
					xdb_funcret="$inst_index"
					break
				elif [ "$xdb_funcret" = '-1' ] ; then
					xdb_funcret=$inst_index
				else
					xdb_funcret="$xdb_funcret $inst_index"
				fi
			else
				if [ "$mod_matches" = 'one' ] ; then
					if [ $mod_index -eq $mod_indx ] ; then
						xdb_funcret=$inst_index
						break
					fi
				elif [ x"$mod_matches" = 'xfirst' ] ; then
					xdb_funcret="$inst_index"
					break
				elif [ "$xdb_funcret" = '-1' ] ; then
					xdb_funcret=$inst_index
				else
					xdb_funcret="$xdb_funcret $inst_index"
				fi
			fi
		fi
	done;

	if [ "$xdb_funcret" = '-1' ] ; then
		return 1
	fi

	return 0
}



#
# find_file serv_index mod_index iinst_index type ver_glob name_glob
# e.g. find_file 1 133 4321 dll 2.50.50 admin_MM_i386.so
#
# 
function xdb_find_file_unfinished() {
	local serv_matches="one"	
	local mod_matches="one"	
	local inst_matches="one"	
	local serv_indx=-1
	local mod_indx=-1
	local inst_indx=-1
	local type
	local ver_pat
	local name_pat
	local path_pat

	if [ -z "$4" ] ; then
		if [ -z "$3" ] ; then
			serv_matches="first"
			mod_matches="first"
			type="$1"
			pat="$2"
		else
			serv_matches="first"
			mod_matches="first"
			type="$2"
			pat="$3"
		fi
	else
		if [ x"$1" = 'xX' ] ; then
			serv_matches="all"
		else
			serv_indx="$1"
		fi
		if [ x"$2" = 'xX' ] ; then
			mod_matches="all"
		else
			mod_indx="$2"
		fi

		type="$3"
		pat="$4"
	fi


	local inst_index
	local inst_ver
	local inst_type
	local server_index
	local mod_index

	xdb_funcret=-1

	for i in ${xdb_insts[*]} ; do
		inst_type=${i#*:}
		inst_type=${inst_type%*:*}
		inst_ver=${i##*:}
		inst_index=${i%*:*:*}
		mod_index=$((inst_index>>$xdb_offset_inst))
		server_index=$((mod_index>>$xdb_offset_mod))
		#echo "index: $inst_index ver: $inst_ver si: $server_index mi: $mod_index get: ${inst_ver/$pat/yes}"
		if [ x"${inst_ver/$pat/yes}" = 'xyes' -a x"${inst_type/$type/yes}" = 'xyes' ] ; then
			if [ "$serv_matches" = 'one' ] ; then
				if [ $server_index -eq $serv_indx ] ; then
					if [ "$mod_matches" = 'one' ] ; then
						if [ $mod_index -eq $mod_indx ] ; then
							xdb_funcret=$inst_index
							break
						fi

					elif [ x"$mod_matches" = 'xfirst' ] ; then
						xdb_funcret="$inst_index"
						break
					elif [ "$xdb_funcret" = '-1' ] ; then
						xdb_funcret=$inst_index
					else
						xdb_funcret="$xdb_funcret $inst_index"
					fi
				fi
			elif [ x"$serv_matches" = 'xfirst' ] ; then
				if [ "$mod_matches" = 'one' ] ; then
					if [ $mod_index -eq $mod_indx ] ; then
						xdb_funcret=$inst_index
						break
					fi
				elif [ x"$mod_matches" = 'xfirst' ] ; then
					xdb_funcret="$inst_index"
					break
				elif [ "$xdb_funcret" = '-1' ] ; then
					xdb_funcret=$inst_index
				else
					xdb_funcret="$xdb_funcret $inst_index"
				fi
			else
				if [ "$mod_matches" = 'one' ] ; then
					if [ $mod_index -eq $mod_indx ] ; then
						xdb_funcret=$inst_index
						break
					fi
				elif [ x"$mod_matches" = 'xfirst' ] ; then
					xdb_funcret="$inst_index"
					break
				elif [ "$xdb_funcret" = '-1' ] ; then
					xdb_funcret=$inst_index
				else
					xdb_funcret="$xdb_funcret $inst_index"
				fi
			fi
		fi
	done;

	if [ "$xdb_funcret" = '-1' ] ; then
		return 1
	fi

	return 0
}

#
# Get* functions to extract info from a db entry
#

function xdb_get_server_path() {
	local entry=${xdb_servers[$1]}

	xdb_funcret=${entry#*:}
}

function xdb_get_mod_name() {
	local entry=${xdb_mods[$1]}

	xdb_funcret=${entry#*:}
}


function xdb_get_inst_ver() {
	local entry=${xdb_insts[$1]}

	xdb_funcret=${entry##*:}
}


function xdb_get_inst_type() {
	local entry=${xdb_insts[$1]}

	xdb_funcret=${entry##*:}
}


function xdb_get_inst_type() {
	local entry=${xdb_insts[$1]}

	entry=${entry#*:}
	xdb_funcret=${entry%*:*}
}





function xdb_print_xml_tree() {
	local server_path
	local server_index
	local mod_index
	local mod_name
	local inst_index
	local inst_ver
	local inst_type
	local file_index
	local file_path
	local file_name
	local file_type
	local file_ver

	for s in ${xdb_servers[*]} ; do
		#echo "X - $s"
		#server_path=`echo $s | sed -e 's/[0-9]*://'`
		#server_index=`echo $s | sed -e 's/:..*//'`
		server_path=${s#*:}
		server_index=${s%*:*}
		#echo "X - server index: $server_index"
		#echo "X - server path: $server_path"
		echo "<Server path=\"$server_path\">"
		
		for m in ${xdb_mods[*]} ; do
			#echo "X - $m"
			#mod_name=`echo $m | sed -e 's/[0-9]*://'`
			#mod_index=`echo $m | sed -e 's/:..*//'`
			mod_index=${m%*:*}
			mod_name=${m#*:}
			#echo "X - mod index: $mod_index"
			#echo "X - mod name: $mod_name"
			#echo "X - mod index: $mod_index, offset: $((mod_index>>xdb_offset_mod))"
			if [ $((mod_index>>xdb_offset_mod)) -eq $server_index ] ; then
				echo "   <Mod name=\"$mod_name\">"

				for i in ${xdb_insts[*]} ; do
					#echo "X - $i"
					#inst_type=`echo $i | sed -e 's/[0-9]*:\([fulpd]\{3,4\}\).*/\1/'`
					#inst_ver=`echo $i | sed -e 's/[0-9]*:..*://'`
					#inst_index=`echo $i | sed -e 's/:..*$//'`
					inst_type=${i#*:}
					inst_type=${inst_type%*:*}
					inst_ver=${i##*:}
					inst_index=${i%*:*:*}
					#echo "X - inst index: $inst_index"
					#echo "X - inst type: $inst_type"
					#echo "X - inst ver: $inst_ver"
					
					#echo "X - inst_index: $inst_index, offset: $((inst_index>>xdb_offset_inst))"

					if [ $((inst_index>>xdb_offset_inst)) -eq $mod_index ] ; then
						echo "      <Install type=\"$inst_type\" ver=\"$inst_ver\">"

						for f in ${xdb_files[*]} ; do
							#echo "X - $f"
							#file_type=`echo $f | sed -e 's/[0-9]*:\([^@][^@]*\)@..*/\1/'`
							#file_name=`echo $f | sed -e 's/[0-9]*:..*@\([^:][^:]*\):..*/\1/'`
							#file_path=`echo $f | sed -e 's/[0-9]*:..*@..*:\([^:][^:]*\):..*/\1/'`
							#file_ver=`echo $f | sed -e 's/[0-9]*:..*@..*:..*:/\1/'`
							#file_index=`echo $f | sed -e 's/:..*$//'`
							file_type=${f#*:}
							file_type=${file_type%*:*:*:*}
							file_name=${f#*:*:}
							file_name=${file_name%*:*:*}
							file_path=${f#*:*:*:}
							file_path=${file_path%*:*}
							file_ver=${f##*:}
							file_index=${f%*:*:*:*:*}
							#echo "X - file index: $file_index"
							#echo "X - file type: $file_type"
							#echo "X - file ver : $file_ver"
							#echo "X - file path: $file_path"
							#echo "X - file name: $file_name"
							#echo "X - file_index: $file_index, offset: $((file_index>>xdb_offset_file))"
							#echo "file: t $file_type, n $file_name, p $file_path, v $file_ver, i $file_index"
							if [ $((file_index>>xdb_offset_file)) -eq $inst_index ] ; then
								echo "         <File type=\"$file_type\" path=\"$file_path\" ver=\"$file_ver\">$file_name</File>"
							fi
						done

						echo "      </Install>"
					fi
				done

				echo "   </Mod>"
			fi
		done
	
		echo "</Server>"
	done
}



function xdb_print_dir_tree() {

	local server_path
	local server_index
	local mod_index
	local mod_name
	local inst_index
	local inst_ver
	local inst_type
	local file_index
	local file_path
	local file_name
	local file_type
	local file_ver

	local c_serv=1
	local c_mod=1
	local c_inst=1
	local c_file=1

	local p_serv=0
	local p_mod=0
	local p_inst=0
	local p_file=0

	#echo "xdb_servers: ${xdb_elem_serv[*]}"
	#echo "xdb_mods: ${xdb_elem_mod[*]}"
	#echo "installs: ${xdb_elem_inst[*]}"

	echo "\\"
	echo "|"
	for s in ${xdb_servers[*]} ; do

		#echo "X - $s"
		server_path=${s#*:}
		server_index=${s%*:*}
		#echo "X - server index: $server_index"
		if [ $c_serv -ge ${#xdb_elem_serv[*]} ] ; then p_serv=0 ; else p_serv=1 ; fi
		echo "+-Server: \"$server_path\""
		if [ $p_serv -eq 1 ] ; then
			echo "| |"
		else
			echo "  |"
		fi

		for m in ${xdb_mods[*]} ; do
			#echo "X - $m"
			mod_index=${m%*:*}
			mod_name=${m#*:}
			#echo "X - mod index: $mod_index, offset: $((mod_index>>xdb_offset_mod))"
			if [ $((mod_index>>xdb_offset_mod)) -eq $server_index ] ; then
				if [ $c_mod -ge ${xdb_elem_serv[server_index]} ] ; then p_mod=0 ; else p_mod=1 ; fi
				if [ $p_serv -eq 1 ] ; then
					echo -n "| "
				else
					echo -n "  "
				fi
				if [ $p_mod -eq 1 ] ; then
					echo "+-Mod: \"$mod_name\""
				else
					echo "\-Mod: \"$mod_name\""
				fi
				if [ $p_serv -eq 1 -a $p_mod -eq 1 ] ; then 
					echo "| | |"
				elif [ $p_serv -eq 1 -a $p_mod -eq 0 ] ; then
					echo "|   |"
				elif [ $p_serv -eq 0 -a $p_mod -eq 1 ] ; then
					echo "  | |"
				else
					echo "    |"
				fi

				for i in ${xdb_insts[*]} ; do
					#echo "X - $i"
					inst_type=${i#*:}
					inst_type=${inst_type%*:*}
					inst_ver=${i##*:}
					inst_index=${i%*:*:*}
					#echo "X - inst_index: $inst_index, offset: $((inst_index>>xdb_offset_inst))"

					if [ $((inst_index>>xdb_offset_inst)) -eq $mod_index ] ; then
						if [ $c_inst -ge ${xdb_elem_mod[mod_index]} ] ; then p_inst=0 ; else p_inst=1 ; fi
						if [ $p_serv -eq 1 ] ; then
							echo -n "| "
						else 
							echo -n "  "
						fi
						if [ $p_mod -eq 1 ] ; then
							echo -n "| "
						else 
							echo -n "  "
						fi
						if [ $p_inst -eq 1 ] ; then 
							echo "+-Install: $inst_type, \"$inst_ver\""
						else
							echo "\-Install: $inst_type, \"$inst_ver\""
						fi

						if [ $p_serv -eq 1 ] ; then
							echo -n "| "
						else 
							echo -n "  "
						fi
						if [ $p_mod -eq 1 ] ; then
							echo -n "| "
						else 
							echo -n "  "
						fi
						if [ $p_inst -eq 1 ] ; then
							echo -n "| "
						else 
							echo -n "  "
						fi
						echo "|"
  
						for f in ${xdb_files[*]} ; do
							#echo "X - $f"
							file_type=${f#*:}
							file_type=${file_type%*:*:*:*}
							file_name=${f#*:*:}
							file_name=${file_name%*:*:*}
							file_path=${f#*:*:*:}
							file_path=${file_path%*:*}
							file_ver=${f##*:}
							file_index=${f%*:*:*:*:*}
							#echo "X - file_index: $file_index, offset: $((file_index>>xdb_offset_file))"
							#echo "file: t $file_type, n $file_name, p $file_path, v $file_ver, i $file_index"
							if [ $((file_index>>xdb_offset_file)) -eq $inst_index ] ; then
								if [ $c_file -ge ${xdb_elem_inst[inst_index]} ] ; then p_file=0 ; else p_file=1 ; fi
								if [ $p_serv -eq 1 ] ; then
									echo -n "| "
								else 
									echo -n "  "
								fi
								if [ $p_mod -eq 1 ] ; then
									echo -n "| "
								else 
									echo -n "  "
								fi
								if [ $p_inst -eq 1 ] ; then
									echo -n "| "
								else 
									echo -n "  "
								fi
								if [ $p_file -eq 1 ] ; then
									echo "+-File: $file_type, \"$file_ver\", \"$file_path/$file_ver\""
								else
									echo "\-File: $file_type, \"$file_ver\", \"$file_path/$file_ver\""
								fi
								c_file=$((c_file+1))
							fi
						done
						c_file=1
						
						c_inst=$((c_inst+1))
						if [ $p_serv -eq 1 ] ; then
							echo -n "| "
						else 
							echo -n "  "
						fi
						if [ $p_mod -eq 1 ] ; then
							echo -n "| "
						else 
							echo -n "  "
						fi
						if [ $p_inst -eq 1 ] ; then
							echo  "| "
						else 
							echo  "  "
						fi
					fi
				done
				c_inst=1

				c_mod=$((c_mod+1))
				if [ $p_serv -eq 1 ] ; then
					echo -n "| "
				else 
					echo -n "  "
				fi
				if [ $p_mod -eq 1 ] ; then
					echo  "| "
				else 
					echo  "  "
				fi
			fi
		done
		c_mod=1
	
		c_serv=$((c_serv+1))
		if [ $p_serv -eq 1 ] ; then
			echo  "| "
		else 
			echo  "  "
		fi
	done
	c_serv=1
}



