#!/bin/bash -e

# ******************************************************************************
# EOS - the CERN Disk Storage System
# Copyright (C) 2019 CERN/Switzerland
#
# 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/>.
# ******************************************************************************

secs_to_human() {
  echo "$(( ${1} / 3600 ))h $(( (${1} / 60) % 60 ))m $(( ${1} % 60 ))s"
}

# Forward the given command to the proper executor Docker or Kubernetes. Gets
# as argument a container name and a shell command to be executed
function exec_cmd() {
  if [[ "${IS_DOCKER}" == true ]]; then
      exec_cmd_docker "$@"
  elif [[ "${IS_LOCAL}" == true ]]; then
      exec_cmd_local "$@"
  else
      exec_cmd_k8s "$@"
  fi
}

# Execute command in Docker setup where the first argument is the name of the container
# and the rest is the command to be executed
function exec_cmd_docker() {
  set -o xtrace
  docker exec -i $1 /bin/bash -l -c "${@:2}"
  set +o xtrace
}

# Execute command in Kubernetes setup where the first argument is the name of the Pod
# and the rest is the command to be executed
function exec_cmd_k8s() {
  set -o xtrace
  kubectl exec --namespace=$K8S_NAMESPACE $(kubectl get pods --namespace=$K8S_NAMESPACE --no-headers -o custom-columns=":metadata.name" -l app=$1) -- /bin/bash -l -c "${@:2}"
  set +o xtrace
}

function exec_cmd_local() {
  /bin/bash -l -c "EOS_MGM_URL=${EOS_MGM_URL} ${@:2}"
}

# Copy a file preserving xattrs. The containers need to have `tar` for this to work
# Arguments: `src_container` `src_dir` `src_file` `dest_container` `dest_dir`
function cp_file_with_xattr_cmd() {
  if [[ "${IS_DOCKER}" == true ]]; then
      cp_file_with_xattr_cmd_docker "$@"
  elif [[ "${IS_LOCAL}" == true ]]; then
      cp_file_with_xattr_cmd_local "$@"
  else
      cp_file_with_xattr_cmd_k8s "$@"
  fi
}

function cp_file_with_xattr_cmd_docker() {
  set -o xtrace
  docker exec -i "${1}" tar cf - --xattrs -C "${2}" "${3}" | docker exec -i "${4}" tar xf - -C "${5}" --xattrs
  set +o xtrace
}

function cp_file_with_xattr_cmd_k8s() {
  set -o xtrace
  kubectl exec -n "$K8S_NAMESPACE" "$(get_podname "${1}")" -- tar cf - --xattrs -C "${2}" "${3}" | kubectl exec -i -n "$K8S_NAMESPACE" "$(get_podname "${4}")" -- tar xf - -C "${5}" --xattrs
  set +o xtrace
}

function cp_file_with_xattr_cmd_local() {
  tar cf - --xattrs -C "${2}" "${3}" | tar xf - -C "${5}" --xattrs
}

function get_podname () {
  kubectl get pods -n "$K8S_NAMESPACE" --no-headers -o custom-columns=":metadata.name" -l app="${1}"
}
