#!/usr/bin/env bash
# ==========================================================================
#     _   _ _ ____            ____          _____
#    | | | (_)  _ \ ___ _ __ / ___|___  _ _|_   _| __ __ _  ___ ___ _ __
#    | |_| | | |_) / _ \ '__| |   / _ \| '_ \| || '__/ _` |/ __/ _ \ '__|
#    |  _  | |  __/  __/ |  | |__| (_) | | | | || | | (_| | (_|  __/ |
#    |_| |_|_|_|   \___|_|   \____\___/|_| |_|_||_|  \__,_|\___\___|_|
#
#       ---  High-Performance Connectivity Tracer (HiPerConTracer)  ---
#                 https://www.nntb.no/~dreibh/hipercontracer/
# ==========================================================================
#
# High-Performance Connectivity Tracer (HiPerConTracer)
# Copyright (C) 2015-2026 by Thomas Dreibholz
#
# 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/>.
#
# Contact: dreibh@simula.no

# Bash options:
set -euo pipefail


# ###### Usage ##############################################################
usage () {
   local exitCode="$1"
   echo >&2 "Usage: $0 user@node_id [-c|--copy-id] [-q|--quiet] [-w|--verbose] [-h|--help] [-v|--version] -- [ssh_arguments ...]"
   exit "${exitCode}"
}


# ###### Version ############################################################
version () {
   echo "HiPerConTracer-SSH @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@"
   exit 0
}



# ###### Main program #######################################################

# ====== Handle arguments ===================================================
if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then
   echo "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!"
   exit 1
fi
GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt)"
if "${GETOPT}" -T >/dev/null 2>&1 || [[ $? -ne 4 ]]; then
   echo >&2 "ERROR: Enhanced/GNU getopt is required!"
   exit 1
fi
options="$(${GETOPT} -o cqwhv --long copy-id,quiet,verbose,help,version -a -- "$@")"
# shellcheck disable=SC2181
if [[ $? -ne 0 ]] ; then
   usage 1
fi

VERBOSE=1
COPY_ID=0
eval set -- "${options}"
while [ $# -gt 0 ] ; do
   case "$1" in
      -c | --copy-id)
         COPY_ID=1
         ;;
      -q | --quiet)
         VERBOSE=0
         ;;
      -w | --verbose)
         VERBOSE=1
         ;;
      -h | --help)
         usage 0
         ;;
      -v | --version)
         version
         ;;
      --)
         shift
         break
         ;;
      *)
         # This should not happen: wrong getopt parameters, or missing case?
         echo >&2 "INTERNAL ERROR: Unhandled option $1!"
         exit 1
         ;;
  esac
  shift
done
if [ $# -ne 1 ] ; then
   usage 1
fi

# shellcheck disable=SC2001
SSH_USER="$(echo "$1" | sed -e "s/@.*$//")"
if [[ ! "${SSH_USER}" =~ ^[a-zA-Z0-9_.-]+$ ]] ; then
   echo >&2 "ERROR: Invalid user ${SSH_USER}!"
   exit 1
fi

# shellcheck disable=SC2001
NODE_ID="$(echo "$1" | sed -e "s/^.*@//")"
if [[ ! "${NODE_ID}" =~ ^[0-9]+$ ]] ; then
   echo >&2 "ERROR: Invalid Node ID ${NODE_ID}!"
   exit 1
fi
if [ "${NODE_ID}" -lt 1 ] || [ "${NODE_ID}" -gt 9999 ] ; then
   echo >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!"
   exit 1
fi


# ====== SSH to node via Reverse Tunnel =====================================
port=$((10000+NODE_ID))

if [ ${COPY_ID} -eq 1 ] ; then
   echo "Copying ID to remote machine ..."
   ssh-copy-id -p ${port} "${SSH_USER}"@localhost && status=0 || status=$?
   if [ "${status}" -ne 0 ] ; then
      echo >&2 "ERROR: ssh-copy-id failed!"
      exit 1
   fi
fi

if [ ${VERBOSE} -ne 0 ] ; then
   # shellcheck disable=SC2145
   echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Running SSH: ssh -p ${port} ${SSH_USER}@localhost $@\x1b[0m"
fi
ssh -p ${port} "${SSH_USER}"@localhost "$@" && status=0 || status=$?
if [ "${status}" -ne 0 ] ; then
   if [ $# -eq 0 ] ; then
      error="SSH failed with exit code ${status}."
   else
      # shellcheck disable=SC2124
      error="SSH \"$@\" failed with exit code ${status}."
   fi
   echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") ${error}\x1b[0m"
   exit "${status}"
else
   if [ ${VERBOSE} -ne 0 ] ; then
      echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m"
   fi
fi
