#!/bin/bash

RC=1
# If necessary, get environment from 'fenv'
LOCAL_BIN_DIR=`dirname \`readlink -f $0\``
LOCAL_ENV="${LOCAL_BIN_DIR}/fenv"
if [ ! "${FENV_HAS_BEEN_SOURCED}" = "1" ];then
    if [ ! -e "${LOCAL_ENV}" ];then
        printf "Read environment file...\tERROR\n"
        printf "'${LOCAL_ENV}' does not exist.\n"
        exit $RC
    fi
    . "${LOCAL_ENV}"
    if [ $? -ne 0 ];then
        printf "ERROR:\n"
        printf "Could not source '${LOCAL_ENV}'.\n"
        exit $RC
    fi
fi

printf "Check SSH connectivity...\t\t"
CONNECTION_COMMAND="${SSH_COMMAND} -q -o BatchMode=yes -o ConnectTimeout=10 ${REMOTE_SERVER} exit"
${CONNECTION_COMMAND}
if [ $? -eq 0 ]; then
    printf "OK (${SSH_COMMAND} ${REMOTE_SERVER})\n"
    RC=0
else
    printf "ERROR\n"
    printf "Check the following settings in '${LOCAL_CFG}':\n\n"
    printf "  REMOTE_SERVER=${REMOTE_SERVER=}\n"
    printf "  REMOTE_PORT=${REMOTE_PORT=}\n\n"
    printf "To run this command manually:\n%s\n" "${CONNECTION_COMMAND} && echo OK || echo ERROR"
fi
exit $RC
