1#!/bin/bash
2
3# set -e
4
5HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
6ROOT=$(cd $HERE/.. && pwd)
7READIES=$ROOT/deps/readies
8. $READIES/shibumi/defs
9
10if [[ $1 == --help || $1 == help || $HELP == 1 ]]; then
11	cat <<-END
12		Get RedisJSON module binaries
13
14		get-redisjson [--help|help]
15
16		Argument variables:
17		OSNICK=nick     Get binaries for given osnick
18		BRANCH=name     Use given branch
19		REPO_PATH=dir   Get binaries from given repo path
20		FORCE=1         Download module even if already present
21		NOP=1           No operation
22		HELP=1          Show help
23
24	END
25	exit 0
26fi
27
28BRANCH=${BRANCH:-2.0}
29
30OP=""
31[[ $NOP == 1 ]] && OP=echo
32
33os="$($READIES/bin/platform --os)"
34arch="$($READIES/bin/platform --arch)"
35
36if [[ ! -z $REPO_PATH ]]; then
37	platform="$($READIES/bin/platform -t)"
38else
39	if [[ $os != linux && $os != macos || $arch != x64 ]]; then
40		eprint "Cannot match binary artifacts - build RedisJSON and set REPO_PATH"
41		exit 1
42	fi
43
44	dist="$($READIES/bin/platform --dist)"
45	nick="$($READIES/bin/platform --osnick)"
46
47	[[ $os == linux ]] && os=Linux
48	[[ $arch == x64 ]] && arch=x86_64
49
50	if [[ $dist == "ubuntu" ]]; then
51		if [[ $nick == "focal" ]]; then
52			nick="ubuntu20.04"
53		elif [[ $nick == "bionic" ]]; then
54			nick="ubuntu18.04"
55		elif [[ $nick == "xenial" ]]; then
56			nick="ubuntu16.04"
57		elif [[ $nick == "trusty" ]]; then
58			nick="ubuntu14.04"
59		fi
60	elif [[ $dist == debian ]]; then
61		if [[ $nick != "bullseye" ]]; then
62			nick=ubuntu18.04
63		fi
64	elif [[ $dist == centos || $dist == redhat || $dist == fedora ]]; then
65		if [[ $nick == "centos8" ]]; then
66			nick="rhel8"
67		else
68			nick="rhel7"
69		fi
70	elif [[ ! -z $OSNICK ]]; then
71		nick=$OSNICK
72	fi
73	platform="${os}-${nick}-${arch}"
74fi
75
76MOD_S3_URL="http://redismodules.s3.amazonaws.com/rejson/snapshots"
77MOD_RAMP="rejson.${platform}.$BRANCH.zip"
78
79DEST_REJSON_DIR="$ROOT/bin/$($READIES/bin/platform -t)/RedisJSON"
80
81if [[ $FORCE != 1 && -d $DEST_REJSON_DIR && -f $DEST_REJSON_DIR/rejson.so ]]; then
82	echo "RedisJSON is in ${DEST_REJSON_DIR}:"
83	$OP du -ah --apparent-size $DEST_REJSON_DIR
84	exit 0
85fi
86
87$OP mkdir -p $(dirname $DEST_REJSON_DIR)
88WORK_DIR=$(mktemp -d /tmp/rejson.XXXXXX)
89
90if [[ -z $REPO_PATH ]]; then
91	F_MOD_RAMP="$WORK_DIR/$MOD_RAMP"
92	if [[ $FORCE == 1 || ! -f $F_MOD_RAMP ]]; then
93		echo "Download RedisJSON [$MOD_S3_URL/$MOD_RAMP] ..."
94		$OP wget -q -P $WORK_DIR $MOD_S3_URL/$MOD_RAMP
95	fi
96else
97	F_MOD_RAMP="${REPO_PATH}/artifacts/snapshots/${MOD_RAMP}"
98	if [[ ! -f $F_MOD_RAMP ]]; then
99		eprint "$F_MOD_RAMP is missing - build RedisJSON and set REPO_PATH"
100		exit 1
101	fi
102fi
103
104$OP unzip -q $F_MOD_RAMP -d $WORK_DIR
105if [[ -e ${DEST_REJSON_DIR} ]]; then
106	echo "Removing existing ${DEST_REJSON_DIR}"
107	$OP rm -rf ${DEST_REJSON_DIR}
108fi
109# $OP mv $WORK_DIR $DEST_REJSON_DIR
110$OP rsync -a --no-owner --no-group --remove-source-files $WORK_DIR/* $DEST_REJSON_DIR
111
112echo "RedisJSON installed into ${DEST_REJSON_DIR}:"
113if [[ $os == linux ]]; then
114	$OP du -ah --apparent-size $DEST_REJSON_DIR
115else
116	$OP du -ah $DEST_REJSON_DIR
117fi
118