#!/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
		BETA=1        Upload to beta folder with version

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

	END
	exit 0
fi

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

ARCH=$($READIES/bin/platform --arch)
[[ $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)
if [[ -r /etc/os-release ]]; then
	# shellcheck disable=SC1091
	. /etc/os-release
	[[ ${ID:-} == almalinux ]] && OSNICK=alma${VERSION_ID%%.*}
fi
[[ $OSNICK == trusty ]]  && OSNICK=ubuntu14.04
[[ $OSNICK == xenial ]]  && OSNICK=ubuntu16.04
[[ $OSNICK == focal ]]   && OSNICK=ubuntu20.04
[[ $OSNICK == jammy ]]   && OSNICK=ubuntu22.04
[[ $OSNICK == noble ]]   && OSNICK=ubuntu24.04
[[ $OSNICK == resolute ]] && OSNICK=ubuntu26.04
[[ $OSNICK == centos7 ]] && OSNICK=rhel7
[[ $OSNICK == centos8 ]] && OSNICK=rhel8
[[ $OSNICK == centos9 ]] && OSNICK=rhel9
[[ $OSNICK == centos10 ]] && OSNICK=rhel10
[[ $OSNICK == ol8 ]]     && OSNICK=rhel8
[[ $OSNICK == rocky8 ]]  && OSNICK=rhel8
[[ $OSNICK == rocky9 ]]  && OSNICK=rhel9
[[ $OSNICK == rocky10 ]] && OSNICK=rhel10

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
		for file in `ls ${prefix}.*${PLATFORM}*.zip ${prefix}.*${PLATFORM}*.tgz 2> /dev/null`; do
			s3_upload_file $file $upload_dir
		done
	else
		for file in `ls ${prefix}.*.zip ${prefix}.*.tgz 2> /dev/null`; do
			s3_upload_file $file $upload_dir
		done
	fi
	[[ $VERBOSE == 1 ]] && s3_ls $upload_dir

	return 0
}

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

# Set S3 directory based on BETA flag
if [[ $BETA == 1 && -n $BETA_VERSION ]]; then
	PROD=redistimeseries/beta PREFIX=redistimeseries s3_upload
else
	PROD=redistimeseries PREFIX=redistimeseries s3_upload
fi
