#!/bin/bash
#  Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
#
#  Allows access to server config information, without running a server process
#  or "config list" commands.

case "$1" in
--peerdns) arg="^tarantella\.config\.server\.dns\.peer=";;
--*extdns) arg="^tarantella\.config\.server\.netservice\.proxy\.externalnames=";;
--*) arg=`echo ^$(expr "$1" : "--\(.*\)") | sed 's#\\.#\\\\.#g'`
esac

ttacommand=$0
case "$ttacommand" in
/*) cd `dirname $ttacommand`
    ;;
*)  fullpath=`pwd`/$ttacommand
    cd `dirname $fullpath`
esac


configfiles="../../var/serverconfig/local/bootstrap.properties ../../var/serverconfig/local/server.properties ../../var/serverconfig/local/rest.properties"

reply=`grep "${arg}" ${configfiles} 2>/dev/null | sed 's/^[^=]*= *//' | tr -d '\\\\'`

case "$1" in
--peerdns)
    [ -z "${reply}" ] && reply=`../bin/ttahostname`
    ;;
--extdns)
    case "${reply}" in	# remove leading "*:" from (eg) "*:thishost.sun.com"
    \\*:*) reply=`expr "${reply}" : '..\(.*\)'`
    esac
    case "${reply}" in	# if comma separated list, use first one
    *:*) reply=`expr "${reply}" : '[^:]*:\([^,]*\).*'`
    esac
    [ -z "${reply}" ] && reply=`../bin/ttahostname`
    ;;
esac

[ -n "${reply}" ] && echo "${reply}"

exit 0
