#!/usr/bin/env bash

rdo_header common

# - function "${@}"
rdo__call_empty() {
  local arg_function="${1:?Missing function argument.}"
  shift

  if [[ -n "${*}" ]]; then
    print_error "${arg_function//_/ }: junk at end of input: ${@}"
    exit 1
  fi

  eval "${arg_function}" "${@}"
}

rdo__compare_id__file_image() {
  local file_name="${1:?Missing file argument.}"
  local image_name="${2:?Missing image argument.}"

  if ! [[ -f "${file_name}" ]]; then
    return 1
  fi

  local file_id="$(cat "${file_name}")"
  local image_id="$(rdo docker inspect --id "${image_name}")"

  [[ -n "${file_id}" ]] && [[ "${file_id}" == "${image_id}" ]]
}

rdo__compare_hash__dir_dir() {
  local path_lhs="${1:?Missing lhs path argument.}"
  local path_rhs="${2:?Missing rhs path argument.}"

  # Sort by size to detect if any files are empty or incomplete.

  if [[ "${path_lhs}" =~ : ]]; then
    local hash_lhs="$(docker exec "${path_lhs%%:*}" sh -c "cd '${path_lhs##*:}'; \ls -S | tail -n +2")"
  else
    local hash_lhs="$(cd "${path_lhs}"; \ls -S | tail -n +2)"
  fi

  if [[ "${path_rhs}" =~ : ]]; then
    local hash_rhs="$(docker exec "${path_rhs%%:*}" sh -c "cd '${path_rhs##*:}'; \ls -S | tail -n +2")"
  else
    local hash_rhs="$(cd "${path_rhs}"; \ls -S | tail -n +2)"
  fi

  [[ "${hash_lhs}" == "${hash_rhs}" ]]
}

# Move:

rdo__project_absolute_unix_path() {
  if [[ -z "${PROJECT_ABSOLUTE_UNIX_PATH}" ]]; then
    if [[ "${OSTYPE}" == "cygwin" ]]; then
      PROJECT_ABSOLUTE_UNIX_PATH="$(cd "${PROJECT_PATH}" && cygpath -au .)"
      PROJECT_ABSOLUTE_UNIX_PATH="/cygwin64${PROJECT_ABSOLUTE_UNIX_PATH%/}"
    else
      PROJECT_ABSOLUTE_UNIX_PATH="$(cd "${PROJECT_PATH}" && pwd)"
    fi
  fi

  echo "${PROJECT_ABSOLUTE_UNIX_PATH}"
}
