#!/bin/bash

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

set -x
set -e

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

dist_build_internal_run="true"

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

## XXX: hardcoded path
source "$HOME/derivative-maker/help-steps/pre"
source "$HOME/derivative-maker/help-steps/colors"
source "$HOME/derivative-maker/help-steps/variables"

root_check_no() {
   if [ "$(id -u)" = "0" ]; then
      true "ERROR: Do not run this as root!"
      exit 1
   fi
}

preparation() {
   shopt -s globstar
}

show_dists() {
   true "${cyan}INFO $BASH_SOURCE: Showing dists...${reset}"
   set +x
   test -d "$binary_build_folder_dist/aptrepo_remote/$derivative_name_item/dists"
   for file_name in "$binary_build_folder_dist/aptrepo_remote/$derivative_name_item/dists/"**; do
      if [ -d "$file_name" ]; then
         continue
      fi
      echo "$file_name"
   done
   unset file_name
   set -x
}

show_pool() {
   if [ ! "$build_upload_noninteractive" = "true" ]; then
      true "${cyan}INFO $BASH_SOURCE: Please press enter to continue showing pool.${reset}"
      read temp
   fi
   true "${cyan}INFO $BASH_SOURCE: Showing pool...${reset}"
   set +x
   test -d "$binary_build_folder_dist/aptrepo_remote/$derivative_name_item/pool"
   for file_name in "$binary_build_folder_dist/aptrepo_remote/$derivative_name_item/pool/"**; do
      if [ -d "$file_name" ]; then
         continue
      fi
      echo "$file_name"
   done
   unset file_name
   set -x
}

upload_real_ask() {
   if [ ! "$build_upload_noninteractive" = "true" ]; then
      true "${cyan}INFO $BASH_SOURCE: Please press enter to continue to uploading. $rsync_dry_run_maybe${reset}"
      read temp
   fi
}

upload_dists() {
   true "${cyan}INFO $BASH_SOURCE: Uploading dists... $rsync_dry_run_maybe${reset}"
   test -d "$binary_build_folder_dist/aptrepo_remote/$derivative_name_item/dists"
   rsync_dry_run_maybe="$rsync_dry_run_maybe" upload_real_ask
   rsync \
      $rsync_opts \
      --recursive \
      $rsync_delete_maybe \
      $rsync_dry_run_maybe \
      --rsh ssh \
      "$binary_build_folder_dist/aptrepo_remote/$derivative_name_item/dists/"* \
      "${upload_location}/dists/"
}

upload_pool() {
   true "${cyan}INFO $BASH_SOURCE: Uploading pool... $rsync_dry_run_maybe${reset}"
   test -d "$binary_build_folder_dist/aptrepo_remote/$derivative_name_item/pool"
   rsync_dry_run_maybe="$rsync_dry_run_maybe" upload_real_ask
   rsync \
      $rsync_opts \
      --recursive \
      $rsync_delete_maybe \
      $rsync_dry_run_maybe \
      --rsh ssh \
      "$binary_build_folder_dist/aptrepo_remote/$derivative_name_item/pool/"* \
      "${upload_location}/pool/"
}

main() {
   root_check_no
   preparation
   show_dists
   show_pool

   #rsync_dry_run_maybe="--dry-run" upload_dists
   upload_dists

   #rsync_dry_run_maybe="--dry-run" upload_pool
   upload_pool
}

## used to work very well
rsync_delete_maybe="--delete"

derivative_name_item=whonix
upload_location="root@whonix.org:/var/rsync/developer-meta-files/internal"
main "$@"

derivative_name_item=kicksecure
upload_location="root@kicksecure.com:/var/rsync/developer-meta-files/internal"
main "$@"

# derivative_name_item=XXX
# upload_location="adrelanos,developer-meta-files@frs.sourceforge.net:/home/frs/project/developer-meta-files/internal"
# main "$@"
