#!/bin/bash

if [ $# -eq 0 ]; then
  echo "Usage: omarchy-iso-upload [release/omarchy.iso]"
  exit 1
fi

if [[ ! -f ~/.config/rclone/rclone.conf ]]; then
  echo "You must first configure rclone with the R2 credentials: omarchy-iso-rclone-config"
  exit 1
fi

# Every copy reports, and one failure fails the upload. Without the tally the
# last copy decides the exit status, so a signature that never made it is
# hidden by a checksum that did.
status=0

rclone copy "$1" Omarchy:omarchy/ -P || status=1

# The signature and the checksum are only worth anything to someone who can
# reach them beside the ISO, so they go up in the same run rather than a step
# a release can finish without.
for sidecar in "$1.sig" "$1.sha256"; do
  if [[ -f $sidecar ]]; then
    rclone copy "$sidecar" Omarchy:omarchy/ -P || status=1
  fi
done

if [[ ! -f $1.sha256 ]]; then
  echo "Warning: no $1.sha256 to publish, so nobody can verify this download" >&2
fi

exit $status
