#!/bin/sh


RSYSTEM2="" #empty
RUSER2="" #empty

#########################################################################
# Definition of functions 

printUsage() {
  echo "sshr-rshell is shell access wrapper to connect to remote system"
  echo ""
  echo "USAGE: sshr-rshell"
  echo "sshr-rshell -h | [-c system] [-u user]"
  echo " -h        -print usage"
  echo " -c system -define system to communicate with" 
  echo " -u name   -define user to connect with"
  echo "Error code:" 
  echo " 202 -remote system is not accessible due to network problem"
  echo " 203 -remote system is not accessible due to problem with key pair"
  echo " 209 -unable to detect remote system, use -c parameter"
  echo "For more error codes check \"sshr -h\" output"

}

endOfProgram() {
  set +x
  tracef "== End of execution (sshr-rshell) [$(/bin/date)] (err=$1) == "
  tracef ""
  tracef ""
  exit $1

}

#################################################################
#
# MAIN function
#
#################################################################
# import common variable and functions
. /opt/rcon/bin/sshr-base

cutLog
tracef "== Start of execution (sshr-rshell) [$(/bin/date)] =="

# evaluate options
while getopts "n:c:vhu:" OPTION; do
    case $OPTION in
	c) RSYSTEM2=$OPTARG;;
        h) printUsage;endOfProgram 0;;
	v) VERBOST="yes";endOfProgram 0;;
	u) RUSER2=$OPTARG;;
    esac
done
shift $(($OPTIND - 1))


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

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

#Step3: Detect remote system IP
SERVERIP=$(/opt/rcon/bin/getremintIP -c $RSYSTEM)
tracef "param:remote IP=\"$SERVERIP\""

#Step4: Get name of user which will be used
if [ -n "$RUSER2" ];then
  RUSER="$RUSER2"
else
  RUSER=$(getValue $RSYSTEM user)
fi
tracef "param:remote user=\"$RUSER\""


#set -x
#####################################################################
#Step6: Test of remote connection, execute simple command (pwd)
TESTRES=$(connectivityTest $SERVERIP $RUSER $RSYSTEM)
RES=$?
if [ $RES -ne 0 ];then
   tracef "ERROR:test of connectivity failed (res=$RES)"
   exit $RES
fi


echo "--- REMOTE CONNECTION TO \"$SERVERIP\" ---"
echo ""

#TODO - implement some equivalent on sles15 - maybe read line by line and execute remotely to give the impression of interractivity
tracef "ERROR : sles15 missing implementation line 98"
exit 1

#####################################################################
# execution of remote command
ssh -q -l $RUSER $SERVERIP -i $PRIVATE_KEYP -F $SSH_CONFIGF sh -i
RES=$?
case "$RES" in
  255 )
    #Error during connection
    tracef "ERROR:unable to open ssh connection to remote system"
    #Verify if remote system is reachable, is yes, verify if local key pair is OK,
        #if yes, inform remote system to download public key
        #if no, generate new key pair
    ping -c 1 $SERVERIP >>$TRACEFILE 2>>$TRACEFILE
    PINGRES=$?
    if [ ! $PINGRES -eq 0 ]; then
      #Ping was not successful
      tracef "ERROR:remote system not accessible"
      endOfProgram 202
    fi

    ;;
  * )
    # command was succesfully executed, response value is in $RES
    ;;
esac

echo "--- REMOTE CONNECTION CLOSED ---"
echo ""


endOfProgram $RES


