#!/bin/bash
mkdir -p master || exit 1;
FIND_DIR=$(find -O3 $(pwd)/master -mindepth 1 -maxdepth 1 -type d -iregex '^.*[0-9]+\.[0-9]+.*$' | sort --version-sort --reverse | head -n 1)
if [[ -n $FIND_DIR ]];
  then CREATED_COMMIT_REF_FOLDER="NO"
else
  CREATED_COMMIT_REF_FOLDER="YES"
fi
TEST_WORKSPACE_LOCATION=$(pwd)/master/5.6.0-alpha-70-g601658dc3c-5135663/linux-rocky8/bctoolbox|| exit 1;
mkdir -p $TEST_WORKSPACE_LOCATION || exit 1
PARENT_BRANCH=$(echo 'master')
if [[ $CREATED_COMMIT_REF_FOLDER = "YES" && -n $PARENT_BRANCH ]];
then
  LAST_MODIFIED_PATH=$(find -O3 $(pwd)/master -mindepth 5 -maxdepth 5 -type d -iregex '^.*/master/[0-9]+\.[0-9]+.*/linux-rocky8/bctoolbox/allure-report/history$'| sort --version-sort --reverse | head -n 1)
  cp -r $LAST_MODIFIED_PATH $(pwd)/master/5.6.0-alpha-70-g601658dc3c-5135663/linux-rocky8/bctoolbox/history;
else
  cp -r $(find -O3 $FIND_DIR/linux-rocky8/bctoolbox/allure-report/history -maxdepth 0) $TEST_WORKSPACE_LOCATION/history;
fi;
mv $(pwd)/environment.properties $TEST_WORKSPACE_LOCATION && mv $(pwd)/BCUnit* $TEST_WORKSPACE_LOCATION
mkdir -p $(pwd)/master/sdk-team/5.6.0-alpha-70-g601658dc3c-5135663/linux-rocky8
ln -s $TEST_WORKSPACE_LOCATION $(pwd)/master/sdk-team/5.6.0-alpha-70-g601658dc3c-5135663/linux-rocky8
~/allure-2.16.1/bin/allure generate --clean $TEST_WORKSPACE_LOCATION -o $TEST_WORKSPACE_LOCATION/allure-report
pwd
ls -lah
du -h $TEST_WORKSPACE_LOCATION
rm -r $TEST_WORKSPACE_LOCATION/history || echo 'deletion of temp history failed. History could not be gathered ?'
du -h $TEST_WORKSPACE_LOCATION
