#!/bin/bash -e
#
# -----------------------------------------------------------------------------
#
# Package       : rq
# Version       : v2.3.2
# Source repo   : https://github.com/nvie/rq
# Tested on     : UBI 9.3
# Language      : c
# Travis-Check  : True
# Script License: Apache License 2.0
# Maintainer    : Sai Kiran Nukala <sai.kiran.nukala@ibm.com>
#
# Disclaimer: This script has been tested in root mode on given
# ==========  platform using the mentioned version of the package.
#             It may not work as expected with newer versions of the
#             package and/or distribution. In such case, please
#             contact "Maintainer" of this script.
#
# ----------------------------------------------------------------------------

PACKAGE_NAME=rq
PACKAGE_VERSION=${1:-v2.3.2}
PACKAGE_URL=https://github.com/nvie/rq
PACKAGE_DIR=rq

yum install -y wget python3 python3-pip python3-devel git gcc-toolset-13 gcc-toolset-13-gcc gcc-toolset-13-gcc-c++ procps make
export PATH=/opt/rh/gcc-toolset-13/root/usr/bin:$PATH
export LD_LIBRARY_PATH=/opt/rh/gcc-toolset-13/root/usr/lib64:$LD_LIBRARY_PATH

pip3 install psutil pytest mock build hatchling
wget http://download.redis.io/releases/redis-5.0.8.tar.gz
tar xzf redis-5.0.8.tar.gz
cd redis-5.0.8
make
cd ..
# Start Redis and keep track of its PID
./redis-5.0.8/src/redis-server > /dev/null 2>&1 &
REDIS_PID=$!
trap "kill $REDIS_PID" EXIT

git clone $PACKAGE_URL
cd $PACKAGE_NAME
git checkout $PACKAGE_VERSION

# Install the package
if ! pip3 install .; then
    echo "------------------$PACKAGE_NAME:Install_fails-------------------------------------"
    echo "$PACKAGE_VERSION $PACKAGE_NAME"
    echo "$PACKAGE_NAME  | $PACKAGE_VERSION | GitHub | Fail |  Build_Fails"
    exit 1
fi


# skipping test: This test is timing-sensitive and may fail intermittently because the job does not always appear
# in FinishedJobRegistry immediately after worker execution. Exclude it during pytest runs using:
if ! pytest --deselect=tests/test_spawn_worker.py; then
	echo "------------------$PACKAGE_NAME:install_success_but_test_fails---------------------"
    echo "$PACKAGE_URL $PACKAGE_NAME"
    echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub | Fail |  Install_success_but_test_Fails"
    exit 2
else
    echo "------------------$PACKAGE_NAME:install_&_test_both_success-------------------------"
    echo "$PACKAGE_URL $PACKAGE_NAME"
    echo "$PACKAGE_NAME  |  $PACKAGE_URL | $PACKAGE_VERSION | $OS_NAME | GitHub  | Pass |  Both_Install_and_Test_Success"
    exit 0
fi
