#!/bin/bash

## Author Oriol Filter
# Date 29/03/2022


## Bring Shell to script folder
cd "$(dirname "$0")" || exit


### VAR
export _USERS="$(cat ./users)"
export _PROXY_ROUTES="$(cat ./proxy_routes)"
_SSH_TIMOUT=2

### COLORS

_RED="\033[1;31m"
_GREEN="\033[1;32m"
_RESET="\033[0m"
_YELLOW="\033[0;33m"


### Main

sed '/^$/d' h | tee ./.curr_h >/dev/null # remove empty lines

printf "Attempting to SSH\n"

if [[ "$_USERS" ]]
then
  for username in $_USERS
  do
    printf "${_YELLOW} ${username}\n${_RESET}"
    printf "" > "./${username}/h"
    for hostname in $(cat ./.curr_h)
    do
      printf "${hostname}: -->"
      result=""
      sshpass -f "./${username}/p" ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} -T "${username}@${hostname}" 'echo true' &> /dev/null && result="true";

      if [[ "$result" ]]
        then
          tee -a "./${username}/h" <<< "${hostname}" &> /dev/null
          printf "${_GREEN}YES${_RESET}\n"
        else
          printf "${_RED}No${_RESET}\n";
      fi
    done
    diff --new-line-format="" --unchanged-line-format="" ./.curr_h "${username}/h" > ./._curr_h
    mv ./._curr_h ./.curr_h
  done
fi

# check if hosts found, if not, print skipping...
#printf "Proxy routes"
# Check if hosts left


# Stores results in ./.proxy_results
printf "" >.proxy_results
if [[ "$_PROXY_ROUTES" ]]
then
  for proxy in $_PROXY_ROUTES
  do
    printf "${_YELLOW}>> Status proxy ${proxy}:${_RESET} "
    proxy_username="$(cat ./_${proxy}/u)"

    result=""
    sshpass -f "./_${proxy}/p" ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} -T "${proxy_username}@${proxy}" 'echo true' &> /dev/null && result="true";


    if [[ ! "$result" ]]
      then
        printf "${_RED}Cannot establish connection with the proxy, skipping ...${_RESET}\n";
      else
        printf "${_GREEN}OK${_RESET}\n"
        for username in $_USERS
        do
          for hostname in $(cat ./.curr_h)
          do
            printf ">> ($proxy) --> ${username}@${hostname}: ${_RESET}"
              result=""
              sshpass -f "./_${proxy}/p" ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} -T "${proxy_username}@${proxy}" "sshpass -f /dev/stdin ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=${_SSH_TIMOUT} -T \"${username}@${hostname}\" \"echo true\" && echo true " &> /dev/null <  "./${username}/p"  && result="true";
              if [[ "$result" ]]
                then
                  printf "${_GREEN}YES${_RESET}\n"
                  # hostname;proxy;username
                   printf "${hostname};${proxy};${username}\n" | tee -a ./.proxy_results > /dev/null
                   diff --new-line-format="" --unchanged-line-format="" ./.curr_h <(echo $hostname) > ./._curr_h
                   mv ./._curr_h ./.curr_h
                else
                  printf "${_RED}No${_RESET}\n";
              fi
          done
        done
    fi
  done
fi


mv .curr_h .discards_h

# Print Result
printf "Printing results:\n"
if [[ "$_USERS" ]]
then
  for username in $_USERS
  do
     printf ">> \033[0;32m ${username}\n${_RESET}"
    awk '{ printf"  %s\n",$0 }' < "./${username}/h"
  done
fi

printf ">> Printing results from proxies\n"
awk -F ';' '{printf "\t%s  -->\t%s (%s)",$2,$1,$3}' ./.proxy_results | col


printf "\n"
printf "\033[0;31mDISCARD HOSTS:${_RESET}\n"
awk '{ printf"  %s\n",$0 }' < ./.discards_h

printf "Discards are stored in './discards_h'\n"


# Send files
bash ./send_files.sh