#!/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
		Get RediSearch module binaries

		get-redisearch [--help|help]

		Argument variables:
		OSNICK=nick     Get binaries for given osnick
		BRANCH=name     Use given branch
		REPO_PATH=dir   Get binaries from given repo path
		FORCE=1         Download module even if already present
		NOP=1           No operation
		HELP=1          Show help

	END
	exit 0
fi

BRANCH=${BRANCH:-master}
if [[ $BRANCH == edge ]]; then
    BRANCH=master
fi

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

os="$($READIES/bin/platform --os)"
arch="$($READIES/bin/platform --arch)"

if [[ ! -z $REPO_PATH ]]; then
	platform="$($READIES/bin/platform -t)"
else
	if [[ $os != linux && $os != macos || $arch != x64 ]]; then
		eprint "Cannot match binary artifacts - build RediSearch and set REPO_PATH"
		exit 1
	fi

	dist="$($READIES/bin/platform --dist)"
	nick="$($READIES/bin/platform --osnick)"

	[[ $os == linux ]] && os=Linux
	[[ $arch == x64 ]] && arch=x86_64

	if [[ $dist == ubuntu ]]; then
		if [[ $nick == jammy ]]; then
			nick="ubuntu22.04"
		elif [[ $nick == focal ]]; then
			nick="ubuntu20.04"
		elif [[ $nick == bionic ]]; then
			nick="ubuntu18.04"
		elif [[ $nick == xenial ]]; then
			nick="ubuntu16.04"
		elif [[ $nick == trusty ]]; then
			nick="ubuntu14.04"
		fi
	elif [[ $dist == debian ]]; then
		if [[ $nick != bullseye ]]; then
			nick=ubuntu18.04
		fi
	elif [[ $dist == centos || $dist == redhat || $dist == fedora || $dist == ol ]]; then
		if [[ $nick == centos9 || $nick == ol9 || $nick == rocky9 || $nick == rhel9 ]]; then
			nick="rhel9"
		elif [[ $nick == centos8 || $nick == ol8 || $nick == rocky8 ]]; then
			nick="rhel8"
		else
			nick="rhel7"
		fi
	elif [[ $OSNICK == bigsur ]]; then
		nick=catalina
	else
		nick="$($READIES/bin/platform --osnick)"
	fi
	platform="${os}-${nick}-${arch}"
fi

MOD_S3_URL="https://redismodules.s3.amazonaws.com/redisearch-oss/snapshots"
MOD_RAMP="redisearch-oss.${platform}.${BRANCH}.zip"

DEST_DIR="$ROOT/bin/$($READIES/bin/platform -t)/RediSearch"
if [[ $FORCE != 1 && -d $DEST_DIR && -f $DEST_DIR/redisearch.so ]]; then
	echo "RediSearch is in ${DEST_DIR}:"
	$OP du -ah --apparent-size $DEST_DIR
	exit 0
fi

$OP mkdir -p $(dirname ${DEST_DIR})
WORK_DIR=$(mktemp -d /tmp/redisearch.XXXXXX)

if [[ -z $REPO_PATH ]]; then
	F_MOD_RAMP="${WORK_DIR}/${MOD_RAMP}"
	if [[ $FORCE == 1 || ! -f $F_MOD_RAMP ]]; then
		echo "Download RediSearch [${MOD_S3_URL}/${MOD_RAMP}] ..."
		runn wget -P ${WORK_DIR} ${MOD_S3_URL}/${MOD_RAMP}
	fi
else
	F_MOD_RAMP="${REPO_PATH}/artifacts/snapshots/${MOD_RAMP}"
	if [[ ! -f $F_MOD_RAMP ]]; then
		eprint "$F_MOD_RAMP is missing - build RediSearch and set REPO_PATH"
		exit 1
	fi
fi

runn unzip $F_MOD_RAMP -d $WORK_DIR
if [[ -e ${DEST_DIR} ]]; then
	echo "Removing existing ${DEST_DIR}"
	$OP rm -rf ${DEST_DIR}
fi
# $OP mv $WORK_DIR $DEST_DIR
runn rsync -a --no-owner --no-group --remove-source-files $WORK_DIR/* $DEST_DIR

echo "RediSearch installed into ${DEST_DIR}:"
if [[ $os == linux ]]; then
	$OP du -ah --apparent-size $DEST_DIR
else
	$OP du -ah $DEST_DIR
fi
