#!/bin/bash

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

set -x
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
shopt -s inherit_errexit
shopt -s shift_verbose

true "INFO: Currently running script: ${BASH_SOURCE[0]} $*"

dist_build_source_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/variables"

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

if [ ! "${build_upload_noninteractive:-}" = "true" ]; then
   true "${cyan}INFO ${BASH_SOURCE[0]}: Please press enter to continue canary github.${reset}"
   read -r temp
fi

## XXX: hardcoded git repositories
pushd -- ~/sourcesown/canary/
git push -- github-kicksecure refs/heads/master:refs/heads/master
git push -- github-whonix refs/heads/master:refs/heads/master
git push -- adrelanos refs/heads/master:refs/heads/master
popd

## XXX: hardcoded project domain names
for upload_location in root@whonix.org root@kicksecure.com ; do

   if [ ! "${build_upload_noninteractive:-}" = "true" ]; then
      true "${cyan}INFO ${BASH_SOURCE[0]}: Please press enter to continue canary --dry-run.${reset}"
      read -r temp
   fi

   true "${cyan}INFO ${BASH_SOURCE[0]}: Test only. Running rsync with --dry-run...${reset}"
   rsync \
      --dry-run \
      $rsync_opts \
      --recursive \
      --rsh ssh \
      "$binary_build_folder_dist/canary/canary.txt.asc" \
      "$binary_build_folder_dist/canary/canary.txt" \
      "$binary_build_folder_dist/canary/canary.txt.clearsign.asc" \
      "$binary_build_folder_dist/canary/canary.txt.sig" \
      "$binary_build_folder_dist/canary/canary.txt.embed.sig" \
      "${upload_location}:/var/rsync/developer-meta-files/canary"

   if [ ! "${build_upload_noninteractive:-}" = "true" ]; then
      true "${cyan}INFO ${BASH_SOURCE[0]}: Please press enter to continue canary.${reset}"
      read -r temp
   fi

   true "${cyan}INFO ${BASH_SOURCE[0]}: Uploading canary...${reset}"
   rsync \
      $rsync_opts \
      --recursive \
      --rsh ssh \
      "$binary_build_folder_dist/canary/canary.txt.asc" \
      "$binary_build_folder_dist/canary/canary.txt" \
      "$binary_build_folder_dist/canary/canary.txt.clearsign.asc" \
      "$binary_build_folder_dist/canary/canary.txt.sig" \
      "$binary_build_folder_dist/canary/canary.txt.embed.sig" \
      "${upload_location}:/var/rsync/developer-meta-files/canary"

done
