#!/bin/bash

PROGNAME="${BASH_SOURCE[0]}"
HERE="$(cd "$(dirname "$PROGNAME")" &>/dev/null && pwd)"
ROOT=$(cd $HERE/.. && pwd)
READIES=$ROOT/deps/readies
. $READIES/shibumi/defs
set -e

#----------------------------------------------------------------------------------------------

if [[ $1 == --help || $1 == help || $HELP == 1 ]]; then
	cat <<-END
		Upload packages to S3

		upload-artifacts [--help|help] artifacts...

		Argument variables:
		SNAPSHOT=1    Upload snapshots (default)
		OSNICK=nick   Operate on packages for given OSNICK (default: host)

		RELEASE=1     Upload release artifacts
		STAGING=1     Upload into staging area

		NOP=1         No operation
		VERBOSE=1     Show artifacts details
		HELP=1        Show help

	END
	exit 0
fi

#----------------------------------------------------------------------------------------------

ARCH=$(uname -m)

[[ $ARCH == arm64 ]] && ARCH="aarch64"
[[ $ARCH == x64 ]] && ARCH="x86_64"
[[ $ARCH == arm64v8 ]] && ARCH="aarch64"

OS=$($READIES/bin/platform --os)
[[ $OS == linux ]] && OS="Linux"

[[ -z $OSNICK ]] && OSNICK=$($READIES/bin/platform --osnick)
[[ $OSNICK == trusty ]]  && OSNICK=ubuntu14.04
[[ $OSNICK == xenial ]]  && OSNICK=ubuntu16.04
[[ $OSNICK == bionic ]]  && OSNICK=ubuntu18.04
[[ $OSNICK == focal ]]   && OSNICK=ubuntu20.04
[[ $OSNICK == jammy ]]   && OSNICK=ubuntu22.04
[[ $OSNICK == centos7 ]] && OSNICK=rhel7
[[ $OSNICK == centos8 ]] && OSNICK=rhel8
[[ $OSNICK == centos9 ]] && OSNICK=rhel9
[[ $OSNICK == ol8 ]]     && OSNICK=rhel8
[[ $OSNICK == rocky8 ]]  && OSNICK=rhel8
[[ $OSNICK == rocky9 ]]  && OSNICK=rhel9

PLATFORM="$OS-$OSNICK-$ARCH"

#----------------------------------------------------------------------------------------------

OP=""
[[ $NOP == 1 ]] && OP=echo

if [[ $STAGING == 1 ]]; then
	S3_URL=s3://redismodules/lab/staging
else
	S3_URL=s3://redismodules
fi

if [[ $FORCE != 1 ]]; then
	if [[ -z $AWS_ACCESS_KEY_ID || -z $AWS_SECRET_ACCESS_KEY ]]; then
		eprint "No credentials for S3 upload."
		exit 1
	fi
fi

cd $ROOT/bin

if [[ $SNAPSHOT == 1 ]]; then
	MAYBE_SNAP=/snapshots
else
	MAYBE_SNAP=
fi

cd artifacts${MAYBE_SNAP}

if du --help | grep -q -- --apparent-size; then
	DU_ARGS='--apparent-size'
fi
[[ $VERBOSE == 1 ]] && du -ah ${DU_ARGS} *

#----------------------------------------------------------------------------------------------

s3_upload_file() {
	local file="$1"
	local s3_dir="$2"
	[[ $s3_dir != */ ]] && s3_dir="${s3_dir}/"

	$OP aws s3 cp $file $s3_dir --acl public-read --no-progress
}

s3_ls() {
	local s3_dir="$1"
	[[ $s3_dir != */ ]] && s3_dir="${s3_dir}/"

	echo "::group::S3 ls $s3_dir"
	$OP aws s3 ls $s3_dir
	echo "::endgroup::"
}

s3_upload() {
	local prod_subdir="$PROD"
	local prefix="$PREFIX"
	local upload_dir="${S3_URL}/${prod_subdir}${MAYBE_SNAP}"
	local file
	if [[ $SNAPSHOT == 1 ]]; then
		files=$(ls ${prefix}.*${PLATFORM}*.zip)
		for file in $files; do
			s3_upload_file $file $upload_dir
		done
	else
		files=$(ls ${prefix}.*.zip)
		for file in $files; do
			s3_upload_file $file $upload_dir
		done
	fi
	[[ $VERBOSE == 1 ]] && s3_ls $upload_dir

	return 0
}

#----------------------------------------------------------------------------------------------

if [[ $BETA == 1 ]]; then
	echo "# Uploading to BETA folder with version: $BETA_VERSION"
	
	# Upload beta files from beta directory if it exists
	if [[ -d $ROOT/bin/artifacts/beta ]]; then
		cd $ROOT/bin/artifacts/beta
		
		if du --help | grep -q -- --apparent-size; then
			DU_ARGS='--apparent-size'
		fi
		[[ $VERBOSE == 1 ]] && du -ah ${DU_ARGS} *
		
		# Upload beta files to beta directory (production only)
		beta_upload_dir="${S3_URL}/redisbloom/beta"
		
		files=$(ls redisbloom.*${PLATFORM}*.zip 2>/dev/null || true)
		for file in $files; do
			$OP aws s3 cp $file $beta_upload_dir/ --acl public-read --no-progress
		done
		[[ $VERBOSE == 1 ]] && $OP aws s3 ls $beta_upload_dir/
	else
		echo "# No beta directory found at $ROOT/bin/artifacts/beta"
	fi
else
	# Regular upload
	PROD=redisbloom PREFIX=redisbloom s3_upload
fi
