#!/bin/bash
#
#
#    Scalix Public License
#
#
#    The contents of this file are subject to the Scalix Public License
#    Version 1.1 ("License"); you may not use this file except in
#    compliance with the License. You may obtain a copy of the License at
#
# 
#    http://www.scalix.com/community/licensing 
#
# 
#    Software distributed under the License is distributed on an "AS IS"
#    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
#    the License for the specific language governing rights and limitations
#    under the License.
#
# 
#    The Original Code is Scalix Server. 
#    The Initial Developer of the Original Code is Scalix Corporation. 
#    Copyright (C) 2006 Scalix Corporation. 
#    All Rights Reserved.
#    Additional Copyrights may apply - see the notice in each file
#
#
#
#
#

GLOBIGNORE='*:?'
SXBIN=/opt/scalix/bin

declare -a MAILNODES

function omshowrt_all
{
	((i = 0))
$SXBIN/omshowrt -q all | while read queue node type; do
	case "$queue" in
	"LOCAL")
		type="local"
		;;
	"UNIX")
		case "$type" in
			"MIME")
				type="mime"
				;;
			"TNEF")
				type="tnef"
				;;
			*)
				type="unknown"
				;;
		esac
		;;
	"SMINTFC")
		case "$node" in
		"internet")
			type="mime"
			;;
		"internet,tnef")
			type="tnef"
			;;
		*)
			continue
			;;
		esac
		;;
	esac
	nodeid=${node//[^a-zA-Z0-9_]/_}
	echo MN_${nodeid}_NAME="$node"
	echo MN_${nodeid}_TYPE="$type"
	echo MN_${nodeid}_DOMAIN=Null
	echo MN_${nodeid}_RULE=Null
	echo MAILNODES[$i]=$nodeid
	((i++))
done
}

function omshowmn_all
{
$SXBIN/omshowmn -D -f | while read mn; do
	IFS_BAK="$IFS";
	IFS="|"; mninfo=( ${mn//  /|} ); IFS="$IFS_BAK"
	if [ "${mninfo[0]}" == "**" ]; then
		node=${mninfo[1]}
		nodeid=${node//[,\-]/_}
		echo MN_${nodeid}_TYPE=primary
		echo MN_${nodeid}_DOMAIN=\'${mninfo[3]}\'
		echo MN_${nodeid}_RULE=\'${mninfo[5]}\'
	else
		node=${mninfo[0]}
		nodeid=${node//[,\-]/_}
		echo MN_${nodeid}_DOMAIN=\'${mninfo[2]}\'
		echo MN_${nodeid}_RULE=\'${mninfo[4]}\'
	fi
done
}

eval $(omshowrt_all)
eval $(omshowmn_all)
#omshowmn_all

for nodeid in ${MAILNODES[*]}; do
	#eval "echo name=\$MN_${nodeid}_NAME"
	#eval "echo type=\$MN_${nodeid}_TYPE"
	#eval "echo domain=\$MN_${nodeid}_DOMAIN"
	#eval "echo rule=\$MN_${nodeid}_RULE"

	eval "echo -e \"\$MN_${nodeid}_NAME\t\$MN_${nodeid}_TYPE\t\$MN_${nodeid}_DOMAIN\t\$MN_${nodeid}_RULE\""
done
