#!/bin/sh
#
#  Returns XMLRPC caller IP (tcosmonitor IP)
#
# Copyright (C) 2006-2011  mariodebian at gmail
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#


if [ -e /conf/tcos-run-functions ]; then
  STANDALONE=/var
  export `grep SSL /conf/tcos.conf`
else
  STANDALONE=/var/lib/tcos/standalone
  . /etc/default/tcos-standalone
fi

pidssl="$(pidof stunnel4 | sed '/^$/d')"

# SSL enabled
if [ -f $STANDALONE/log/stunnel.log -a "$pidssl" != "" -a "$1" = "1" ]; then
  if [ "$TCOS_XMLRPC_SSL" = "1" ] || [ "$TCOS_ENABLE_SSL" = "1" ]; then
    grep "tcosxmlrpc accepted connection" $STANDALONE/log/stunnel.log |tail -1 | awk '{print $NF}'| awk -F ":" '{print $1}'
    exit 0
  fi
fi

# SSL disabled (not stunnel4 logs) read XMLRPC abyss log
if [ -f $STANDALONE/log/access.log ]; then
    tail -1 $STANDALONE/log/access.log | awk '{print $1}'
    exit 0
fi  



