#!/bin/gawk -f

func usage()
{
	print														\
		"Usage:			j2j [option]\n"							\
		"Valid options are:\n"									\
		"	--ip		component host\n"						\
		"	--port		component port\n"						\
		"	--host		component JID\n"						\
		"	--password	component password" > "/dev/stderr"
	exit(1)
}

func getopt()
{
	switch (ARGV[1]) {
		case /^--ip$/ : { return "component.Host" }
		case /^--port$/ : { return "component.Port" }
		case /^--host$/ : { return "component.JID" }
		case /^--password$/ : { return "component.Password" }
		default : { usage() }
	}
}

func ini_parse(key, v, sect)
{
	split("", v)
	while ((getline < CONF) > 0) {
		if ($0 ~ /[[[:alpha:]]]/) sect=gensub(/[][ 	]/, "", "g", $0)
		if ($0 ~ /[[:alpha:]]+=/) v[sect "." gensub(/=.+$/, "", "", $0)] = gensub(/^[^=]+=/, "", "", $0)
	}
	print v[key]
}

func need_conv(ret)
{
	ret=0
	while ((getline < CONF) > 0) {
		if ($0 ~ /class[[:blank:]]+config:/) ret=1
	}
	close(CONF)
	return ret
}

func py_to_ini(qry, key, value, v, fs, s)
{
	split("", v)
	fs = FS
	FS = "="
	while ((getline < CONF) > 0) {
		key = gensub(/^[[:blank:]]+/, "", "", $1)
		value = gensub(/^u*\"/, "", "", gensub(/\"$/, "", "", $2))
		switch (key) {
			case /^$|class/ : { break }
			case /^JID/ : { v["component.JID"] = value; break }
			case /^HOST/ : { v["component.Host"] = value; break }
			case /^PORT/ : { v["component.Port"] = value; break }
			case /^PASSWORD/ : { v["component.Password"] = value; break }
			case /^DB_TYPE/ : { v["database.Type"] = value; break }
			case /^DB_HOST/ : { v["database.Host"] = value; break }
			case /^DB_USER/ : { v["database.User"] = value; break }
			case /^DB_NAME/ : { v["database.Name"] = value; break }
			case /^DB_PASS/ : { v["database.Password"] = value; break }
			case /^DB_PREFIX/ : { v["database.Prefix"] = value; break }
			case /^ADMINS/ : {
				value = ""
				split(gensub(/[][[:blank:]]/, "", "g", $2), s, "[[:blank:]]*,[[:blank:]]*")
				for (i in s) value = value (value == "" ? "" : ",")		\
					gensub(/^u*\"/, "", "", gensub(/\"$/, "", "", s[i]))
				v["admins.List"] = value
				break
			}
		}
	}

	FS = fs

	if (v["database.Type"] == "") v["database.Type"] = "postgres"
	if (v["database.Host"] == "None") delete v["database.Host"]

	close(CONF)

	print											\
		"[component]\n"								\
		"JID=" v["component.JID"] "\n"				\
		"Host=" v["component.Host"] "\n"			\
		"Port=" v["component.Port"] "\n"			\
		"Password=" v["component.Password"] "\n"	\
		"\n"										\
		"[database]\n" 								\
		"Type=" v["database.Type"] "\n"				\
		"Host=" v["database.Host"] "\n"				\
		"User=" v["database.User"] "\n"				\
		"Name=" v["database.Name"] "\n"				\
		"Password=" v["database.Password"] "\n"		\
		"Prefix=" v["database.Prefix"] "\n"			\
		"\n"										\
		"[admins]\n"								\
		"List=" v["admins.List"] "\n"				\
		"\n"										\
		"[debug]\n"									\
		"logfile=/var/log/j2j/j2j.log\n"			\
		"registrations=yes\n"						\
		"logins=yes\n"								\
		"xml_logging=/var/log/j2j/xml.log\n"		\
		"component_xml=no\n"						\
		"clients_xml=no\n"							\
		"clients_jids_to_log=All" > CONF

	print v[qry]
}

BEGIN {
	CONF="/etc/j2j.conf"
	if (ARGC < 2) usage()
	key=getopt()
	# check for old j2j.conf and convert to new style
	if (need_conv())
		py_to_ini(key)
	else
		ini_parse(key)

	exit(0)
}
