#!/usr/bin/env bash

# Based on the template in: https://docs.digitalocean.com/reference/api/spaces-api/
bash -euo pipefail

allowed_targets=("linux-deb" "macos")
is_allowed_target() {
    for val in "${allowed_targets[@]}"; do
        if [[ "$1" == "$val" ]]; then
            return 0
        fi
    done
    return 1
}

if [[ -n "${1:-}" ]]; then
    if is_allowed_target "$1"; then
        target="$1"
    else
        echo "Error: Target '$1' is not allowed"
        echo "Usage: $0 [${allowed_targets[@]}]"
        exit 1
    fi
else
echo "Error: Target is not specified"
echo "Usage: $0 [${allowed_targets[@]}]"
exit 1
fi
echo "Uploading nightly for target: $target"

# Step 1: Define the parameters for the Space you want to upload to.
SPACE="zed-nightly-host" # Find your endpoint in the control panel, under Settings.
REGION="nyc3" # Must be "us-east-1" when creating new Spaces. Otherwise, use the region in your endpoint (e.g. nyc3).

# Step 2: Define a function that uploads your object via cURL.
function uploadToSpaces
{
  file_to_upload="$1"
  file_name="$2"
  space_path="nightly"
  date=$(date +"%a, %d %b %Y %T %z")
  acl="x-amz-acl:private"
  content_type="application/octet-stream"
  storage_type="x-amz-storage-class:STANDARD"
  string="PUT\n\n${content_type}\n${date}\n${acl}\n${storage_type}\n/${SPACE}/${space_path}/${file_name}"
  signature=$(echo -en "${string}" | openssl sha1 -hmac "${DIGITALOCEAN_SPACES_SECRET_KEY}" -binary | base64)

  curl --fail -vv -s -X PUT -T "$file_to_upload" \
    -H "Host: ${SPACE}.${REGION}.digitaloceanspaces.com" \
    -H "Date: $date" \
    -H "Content-Type: $content_type" \
    -H "$storage_type" \
    -H "$acl" \
    -H "Authorization: AWS ${DIGITALOCEAN_SPACES_ACCESS_KEY}:$signature" \
    "https://${SPACE}.${REGION}.digitaloceanspaces.com/${space_path}/${file_name}"
}

sha=$(git rev-parse HEAD)
echo ${sha} > target/latest-sha
case "$target" in
    macos)
        uploadToSpaces "target/aarch64-apple-darwin/release/Zed.dmg" "Zed-aarch64.dmg"
        uploadToSpaces "target/x86_64-apple-darwin/release/Zed.dmg" "Zed-x86_64.dmg"
        uploadToSpaces "target/release/Zed.dmg" "Zed.dmg"
        uploadToSpaces "target/latest-sha" "latest-sha"
        ;;
    linux-deb)
        find target/release -type f -name "*.deb" -print0 | while IFS= read -r -d '' bundle_file; do
            uploadToSpaces "$bundle_file" "$(basename "$bundle_file")"
        done
        uploadToSpaces "target/latest-sha" "latest-sha-linux-deb"
        ;;
    *)
        echo "Error: Unknown target '$target'"
        exit 1
        ;;
esac
