#!/bin/bash

. ${TOOLKIT}-functions.sh

set -e
set -u

file="odbc.ini"
done="done.tmp"

cp $file $file.tmp
cp /dev/null $done

echo -n "  odbc instances: "
for instance in `jtconf --list odbc.`
do
	driver=`jtconf $instance.driver`	|| exit_missing_define $instance.driver
	desc=`jtconf $instance.desc`		|| exit_missing_define $instance.desc
	server=`jtconf $instance.server`	|| exit_missing_define $instance.server
	port=`jtconf $instance.port`		|| exit_missing_define $instance.port
	database=`jtconf $instance.database`	|| exit_missing_define $instance.database
	user=`jtconf $instance.user 2>/dev/null` || :
	password=`jtconf $instance.password 2>/dev/null` || :



	name=`echo $instance | sed -e 's/.*\.//'`

	echo -n "$name "

	used=`grep "^$name " $done` && {
		echo "duplicate instance on $instance"
		echo "already used: $used"
		exit 1
	}

	echo "
[$name]
Driver		= $driver
Description	= $desc
Server		= $server
Port		= $port
Database	= $database" >>$file.tmp

	[ "$user" != "" ] && echo "Username	= $user" >>$file.tmp
	[ "$password" != "" ] && echo "Password	= $password" >>$file.tmp

done
echo

exit 0
