#!/bin/bash

# file: ping_host
# bk_version  26.05.1


# Copyright (C) 2017-2026 Richard Albrecht
# www.rleofield.de

# 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 3 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, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------------------------

# prefixes of variables in backup:
# bv_*  - global vars, all files
# lv_*  - local vars, global in file
# lc_*  - local constants, global in file
# _*    - local in functions or loops
# BK_*  - exitcodes, upper case, BK_
# cfg_*  - set in cfg.* file_



. ./cfg.working_folder
. ./cfg.ssh_login
. ./src_log.sh


lv_cc_logname="ping"

# copy from cfg.ssh_login
readonly cfg_sshhost="$sshhost"
readonly cfg_sshhost2="$sshhost2"
readonly cfg_sshlogin="$sshlogin"
readonly cfg_sshlogin2="$sshlogin2"

readonly cfg_sshport="$sshport"
readonly cfg_sshport2="$sshport2"

readonly cfg_sshtargetfolder="$sshtargetfolder"
readonly cfg_sshtargetfolder2="$sshtargetfolder2"




function do_ping_host {

	local _USER=$1
	local _HOST=$2
        local _FOLDER=$3
        local _PORT=$4

        if [ -z $_PORT ]
        then
                _PORT=22
        fi
#	dlog "in ping, host: $_HOST"

        ping -c1 $_HOST &> /dev/null
        if test $? -eq 0
        then
#		dlog "ping ok  ping -c1 $_HOST "
                sshstr="x=99; y=88; if test  -d $sshtargetfolder; then  exit 99; else exit 88; fi"
                sshstr2="ssh -p $_PORT $_USER@$_HOST '${sshstr}'"
		dlog "in ping sshstr2: $sshstr2"
                eval ${sshstr2}  &> /dev/null
                local _RET=$?
#		dlog "ping ret: $_RET"
                if test  $_RET -eq 99; then
                        # host exists
                        return 0
                fi
        fi
        return 1
}


function func_sshport {
        local p="22"
        if [[ -n $sshport ]]
        then
                p=$sshport
        fi
        echo $p
}

echo "===  test ssh login entries for desktop messages  ==="
echo ""

if [ ${cfg_sshhost} = "localhost" ]
then
	echo "is localhost"
	exit
fi
if [ ${cfg_sshhost} = "127.0.0.1" ]
then
	echo "is 127.0.0.1"
	exit
fi


echo "===  sshlogin  ==="
echo "login: '${cfg_sshlogin}' "
echo "host: '${cfg_sshhost}'"
echo "folder: '${cfg_sshtargetfolder}'"
echo "port: '${cfg_sshport}'"

do_ping_host ${cfg_sshlogin} ${cfg_sshhost} ${cfg_sshtargetfolder} ${cfg_sshport}
RET=$?
if [ $RET -eq 0 ]
then
	echo "message host is ok"
else
	echo "message host is not ok"
fi
if [  -n  "${cfg_sshlogin2}" ]
then
	echo "===  sshlogin2  ==="
	echo "login: '${cfg_sshlogin2}' "
	echo "host: '${cfg_sshhost2}'"
	echo "folder: '${cfg_sshtargetfolder2}'"
	echo "port: '${cfg_sshport2}'"
	do_ping_host ${cfg_sshlogin2} ${cfg_sshhost2} ${cfg_sshtargetfolder2} ${cfg_sshport2}
	if [ $RET -eq 0 ]
	then
		echo "message host2 is ok"
	else
		echo "message host2 is not ok"
	fi
fi

# EOF


