#!/bin/bash

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

export HOMEBREW_NO_AUTO_UPDATE=1

$READIES/bin/getpy3
pyenv="$(get_profile_d)/pyenv.sh"
if [[ -f $pyenv ]]; then
	. $pyenv
fi
if [[ $VERBOSE == 1 ]]; then
	python3 --version
	python3 -m pip list
fi

$ROOT/sbin/system-setup.py $SETUP_ARGS
if [[ $VERBOSE == 1 ]]; then
	python3 -m pip list
fi

# This entire section purpose is to make sure that on amazon_linux_2
# we link to openssl1.1. To achieve it, we uninstall 'openssl-devel'
# which was already installed by this setup file and then install 'openssl11-devel'.
# Notice that we can not install them both at the same time because they conflict with
# each other. When we remove this 'setup' file from the build steps we should perform
# this installation on '.install/amazon_linux_2.sh' (same as we do for RediSearch).
OS_TYPE=$(uname -s)
if [[ $OS_TYPE = 'Darwin' ]]
then
    OS='macos'
else
    VERSION=$(grep '^VERSION_ID=' /etc/os-release | sed 's/"//g')
    VERSION=${VERSION#"VERSION_ID="}
    OS_NAME=$(grep '^NAME=' /etc/os-release | sed 's/"//g')
    OS_NAME=${OS_NAME#"NAME="}
    OS=${OS_NAME,,}_${VERSION}
    OS=$(echo $OS | sed 's/[/ ]/_/g') # replace spaces and slashes with underscores
fi
echo $OS

if [[ $OS = 'amazon_linux_2' ]]
then
	yum remove -y openssl-devel
	yum install -y openssl11 openssl11-devel
	ln -s /usr/lib64/pkgconfig/libssl11.pc /usr/lib64/pkgconfig/libssl.pc
	ln -s /usr/lib64/pkgconfig/libcrypto11.pc /usr/lib64/pkgconfig/libcrypto.pc
	ln -s /usr/lib64/pkgconfig/openssl11.pc /usr/lib64/pkgconfig/openssl.pc
fi
