#!/bin/bash
set -eu

# See bug https://bugs.launchpad.net/percona-xtradb-cluster/+bug/1213855
#
# xtradb-cluster is built expecting libssl and libcryto have the following
# versions. Doing this is terrifying stability-wise for a database.

DISTRO=`lsb_release -si` || true

if [[ "RedHatEnterpriseServer CentOS Fedora" =~ "$DISTRO" ]]; then
    if [ "$ARCH" == "amd64" ]; then
        LIB_PREFIX=/usr/lib64
    else
        LIB_PREFIX=/usr/lib
    fi
else
    if [ "$ARCH" == "amd64" ]; then
        LIB_PREFIX=/lib/x86_64-linux-gnu
    else
        LIB_PREFIX=/lib/$ARCH-linux-gnu
    fi
fi

LIBSSL_FILE=$(find $LIB_PREFIX -maxdepth 1 -type f -name "libssl.so.1.0.*" | head -n 1 || echo "")
LIBCRYPTO_FILE=$(find $LIB_PREFIX -maxdepth 1 -type f -name "libcrypto.so.1.0.*" | head -n 1 || echo "")

if [ -z LIBSSL_FILE ]; then
    echo "Unable to find compatible libssl"
    exit 1
fi
if [ -z LIBCRYPTO_FILE ]; then
    echo "Unable to find compatible libcrypto"
    exit 1
fi

if [ ! -L $LIB_PREFIX/libssl.so.6 ]; then
    ln -s $LIBSSL_FILE $LIB_PREFIX/libssl.so.6
fi

if [ ! -L $LIB_PREFIX/libcrypto.so.6 ]; then
    ln -s $LIBCRYPTO_FILE $LIB_PREFIX/libcrypto.so.6
fi
