#!/usr/bin/env bash
#
# Transfer required files from local machine to cluster.
# OS: macOS 12.4

set -o errexit
set -o pipefail
set -o nounset

_rsync_to_cluster() {
  local opts=(
    --archive
    --compress
    --partial
    --progress
    --human-readable
    --verbose
    --exclude=.DS_Store
  )
  local src="$1"
  local dst="$2"
  local file_description="$3"

  # Check if additional rsync parameters were passed to function
  if [ -n "${4+empty}" ]; then
    opts+=("$4")
  fi

  echo
  echo "Transferring $file_description from local machine to cluster"
  echo "Executing: rsync ${src} ${dst}"
  rsync "${opts[@]}" "${src}" "${dst}"

  echo
  echo "Done transferring $file_description from local machine to cluster"
  echo
}

remote_cleanup() {
  echo "Clean up directories on cluster"
  local -r dist_path='/home/rodrigo/dist'

  echo "Deleting old builds under $dist_path"
  # Quoting from shellcheck's documentation:
  # Use "${var:?}" to ensure this never expands to /* .
  # Source: https://www.shellcheck.net/wiki/SC2115
  #
  # Use rm -f to ignore if no file exist.
  ssh -o SendEnv=dist_path ml "ls ${dist_path:?}/ && rm -f ${dist_path:?}/*"
}

transfer_config() {
  local -r src="$(pwd)/experiments/cluster/bashrc"
  local -r dst="ml:/home/rodrigo/bashrc"

  _rsync_to_cluster "${src}" "${dst}" "bashrc"
}

transfer_data() {
  local -r src="$(pwd)/data"
  local -r dst="ml:/home/rodrigo/"

  _rsync_to_cluster "${src}" "${dst}" "dataset"
}

transfer_code() {
  local -r dist_path="$(pwd)/dist"

  echo "Transfer code to cluster"

  _rsync_to_cluster "${dist_path}" "ml:/home/rodrigo/" "dist"
  _rsync_to_cluster "experiments" "ml:/home/rodrigo/" "experiments" '--exclude=notebooks'
}

main() {
  echo "Transfer build to cluster"
  remote_cleanup
  transfer_config
  transfer_data
  transfer_code
}

main
