#!/bin/bash

## Copyright (C) 2012 - 2023 ENCRYPTED SUPPORT LP <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -x
set -e

true "INFO: Currently running script: $BASH_SOURCE $@"

MYDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$MYDIR"

source ~/derivative-maker/help-steps/pre
source ~/derivative-maker/help-steps/colors
source ~/derivative-maker/help-steps/variables

show_files() {
   local file_name_item
   for file_name_item in "${dist_build_files_to_upload[@]}"; do
      if [ -z "$file_name_item" ]; then
         continue
      fi

      test -f "$file_name_item"

      upload_file_list+=("${file_name_item}")
      upload_file_list+=("${file_name_item}.asc")

      if echo "${file_name_item}.sig" | grep dist-installer ; then
         upload_file_list+=("${file_name_item}.sig")
      else
         if test -f "${file_name_item}.sig" ; then
            upload_file_list+=("${file_name_item}.sig")
         fi
      fi

      upload_file_list+=("${file_name_item}.torrent")
      upload_file_list+=("${file_name_item}.torrent.asc")
      upload_file_list+=("${file_name_item}.torrent.sig")
      upload_file_list+=("${file_name_item}.sha512sums")
      upload_file_list+=("${file_name_item}.sha512sums.asc")
      upload_file_list+=("${file_name_item}.sha512sums.sig")
   done

   true "File list:"
   printf "%s\n" "${upload_file_list[@]}"

   local upload_file_item
   for upload_file_item in "${upload_file_list[@]}"; do
      test -f "$upload_file_item"
   done
}

upload_files() {
   if [ "$dist_build_installer_dist" = "true" ]; then
      dist_server_with_upload_location_item_with_version_maybe="${dist_server_with_upload_location_item}/"
   else
      dist_server_with_upload_location_item_with_version_maybe="${dist_server_with_upload_location_item}/${dist_build_version}/"
   fi
   true "dist_server_with_upload_location_item_with_version_maybe: $dist_server_with_upload_location_item_with_version_maybe"

   if [ ! "$build_upload_noninteractive" = "true" ]; then
      true "${cyan}INFO: Press any enter to continue.${reset}"
      read -r temp
   fi

   rsync_dry_run_maybe="--dry-run"

   attempts_counter="0"
   attempts_max="100"
   [ -n "$sleep_seconds" ] || sleep_seconds="1"

   while true; do
      attempts_counter="$(( attempts_counter + 1 ))"
      sleep_seconds="$(( sleep_seconds * 2 ))"

      if [ "$rsync_dry_run_maybe" = "--dry-run" ]; then
         true "${cyan}INFO: --dry-run only...${reset}"
      else
         true "${cyan}INFO: uploading...${reset}"
      fi

      ## Test if DNS is functional.
      ## Sometimes 'rsync' fails to resolve DNS but running 'nslookup' beforehand fixes it.
      ## Overrule with '||true' as the failure would be handled below by 'rsync'.
      nslookup "$dist_server_with_upload_location_list_list" || true

      rsync_exit_code="0"
      $rsync_cmd \
         $rsync_dry_run_maybe \
         $rsync_opts \
         "${upload_file_list[@]}" \
         "$dist_server_with_upload_location_item_with_version_maybe" \
         || { rsync_exit_code="$?" ; true; };

      if [ "$rsync_exit_code" = "0" ]; then
         if [ "$rsync_dry_run_maybe" = "--dry-run" ]; then
            if [ ! "$build_upload_noninteractive" = "true" ]; then
               true "${cyan}INFO: Press any enter to continue.${reset}"
               read -r temp
            fi
            rsync_dry_run_maybe=""
            continue
         fi
         break
      fi

      if [ "$attempts_counter" -ge "$attempts_max" ]; then
         error "${bold}${red}ERROR: Upload failed.${reset}"
         ## Allow to ignore the error such as after fixing the internet connection.
         continue
      fi

      sleep "$sleep_seconds" || true

   done
}

main() {
   if [ ! "$dist_build_image_upload_supported" = "true" ]; then
      error "upload support for this --target is not yet implemented."
   fi

   if [ "$dist_build_version" = "" ]; then
      error "Variable dist_build_version is empty."
   fi

   if [ "$dist_server_with_upload_location_list" = "" ]; then
      error "Variable dist_server_with_upload_location_list is empty."
   fi

   show_files

   for dist_server_with_upload_location_item in $dist_server_with_upload_location_list ; do
      upload_files
   done

   true
}

main
