#!/bin/sh

RC=0
RSYSTEM2="" #empty

#######################################################
#
# MAIN
#
#######################################################
. /opt/rcon/bin/sshr-base

cutLog
tracef "== Start of execution (getremintIP) [$(/bin/date)]"

# evaluate options
while getopts "c:" OPTION; do
    case $OPTION in
        c) RSYSTEM2=$OPTARG;;
    esac
done
shift $(($OPTIND - 1))


#Step1: Detect current system type
SYSTEMTYPE=$(detectSystem)

#Step2: Detect remote system type
RSYSTEM=$(getRSystem $SYSTEMTYPE "$RSYSTEM2")
RC=$?
if [ $RC -ne 0 ];then
  tracef "ERROR:unable to identify remote system"
  exit $RC
fi

if [ "$SYSTEMTYPE" == "host_intl" ]; then
  REM_ILAN=$(getHostRecord $RSYSTEM);
  RC=$?
else
  if [ "$RSYSTEM" == "host_intl" ]; then
    REM_ILAN=$(getHostRecord os4kplt)
    RC=$?
    if [ $RC -ne 0 ];then
      #Step3:Detect hostname of remote system
      HNAME=$(getValue $RSYSTEM hname)
      REM_ILAN=$(hostname2IP $SYSTEMTYPE $HNAME)
      RC=$?
    fi
  else
    #Detect hostname of remote system
    HNAME=$(getValue $RSYSTEM hname)

    #Detect internal IP of remote system
    REM_ILAN=$(hostname2IP $SYSTEMTYPE $HNAME)
    RC=$?
  fi
fi

tracef "remote IP for internal communication is \"$REM_ILAN\" (res=$RC)"
tracef "== End of execution (getremintIP) [$(/bin/date)] (err=$RC)"
tracef "."

echo $REM_ILAN
exit $RC 

