#!/bin/bash

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

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

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

		FORCE=1       Allow uploading outside of CI
		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"

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 == ol8 ]] && OSNICK=rhel8
[[ $OSNICK == rocky8 ]] && OSNICK=rhel8

[[ $OSNICK == bigsur ]]  && OSNICK=catalina

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 $CIRCLECI ]]; then
		eprint "Cannot upload outside of CircleCI. Override with FORCE=1."
		exit 1
	fi

	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}
[[ $VERBOSE == 1 ]] && du -ah --apparent-size *

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

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}/"
	
	$OP aws s3 ls $s3_dir
}

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
}

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

PROD=redistimeseries PREFIX=redistimeseries s3_upload
