#!/bin/bash

BINDING_NAME="performance-binding"
BINDING_TEST_NAME="performance-binding-test"
AFB_TEST_BINDER="afbtest"

# Get the name of the test binder
TEST_BINDER=$(afm-util list -a | jq -r .[].id | grep ${AFB_TEST_BINDER})

# Get the name of the package inside afb environment
APP=$(afm-util list -a | jq -r .[].id | grep "${BINDING_NAME}$")

# Start the corresponding app if it does not already run
RUNNING_APP=$(afm-util ps | jq -r .[].id | grep $APP)
if [ -z "$RUNNING_APP" ]
then
    afm-util start $APP
fi

# Get the binding test name
TEST_BINDING=$(afm-util list -a | jq -r .[].id | grep ${BINDING_TEST_NAME})
# Launch the test included in the pacakge
afm-util start ${TEST_BINDING}

# Create the directory where the logs need to be
mkdir -p /var/log/redtest/${BINDING_NAME}/

# Copy the logs from source to new location
cp -a /home/`id -u`/app-data/${BINDING_TEST_NAME}/* /var/log/redtest/${BINDING_NAME}/

# Assigns the correct rights
chmod 770 -R /var/log/redtest/${BINDING_NAME}/
chown root:users -R /var/log/redtest/${BINDING_NAME}/

# Stop the app that we were testing
afm-util terminate $APP
