#!/bin/bash

# EXAMPLE RUN:
#       ./ScriptsLib/RunLocalWindowsDockerRegressionTests
#       CONTAINER_IMAGE=sophistsolutionsinc/stroika-buildvm-windows-cygwin-vs2k22 USE_TEST_BASENAME=Windows_Cygwin-VS2k22-In-Docker ./ScriptsLib/RunLocalWindowsDockerRegressionTests
#       CONTAINER_IMAGE=sophistsolutionsinc/stroika-buildvm-windows-msys-vs2k22 USE_TEST_BASENAME=Windows_MSYS-VS2k22-In-Docker ./ScriptsLib/RunLocalWindowsDockerRegressionTests
#
#       for var in "Cygwin-VS2k22" "MSYS-VS2k22" ; do LCV=`echo "${var}" | tr '[:upper:]' '[:lower:]'` CONTAINER_IMAGE=sophistsolutionsinc/stroika-buildvm-windows-${LCV} USE_TEST_BASENAME=Windows_${var}-In-Docker ./ScriptsLib/RunLocalWindowsDockerRegressionTests ; done
#
CONTAINER_IMAGE="${CONTAINER_IMAGE:-sophistsolutionsinc/stroika-buildvm-windows-msys-vs2k22}"
CONTAINER_NAME="${CONTAINER_NAME:-DockerWindowsStroikaRegressionTester1}"
USE_TEST_BASENAME="${USE_TEST_BASENAME:-Windows_MSYS-VS2k22-In-Docker}"
BRANCH="${BRANCH:-v3-Dev}"
VERSION=`./ScriptsLib/ExtractVersionInformation STROIKA_VERSION FullVersionString`
DOCKER="${DOCKER:-docker.exe}"

# Due to https://stroika.atlassian.net/browse/STK-742 - add   --network "Default Switch" 
# DOCKER_NETWORK="${DOCKER_NETWORK:-Default Switch}"

DOCKER_RUN_ARGS_=
DOCKER_RUN_ARGS_+=" --detach"
DOCKER_RUN_ARGS_+=" --interactive"
DOCKER_RUN_ARGS_+=" --memory 10G"
DOCKER_RUN_ARGS_+=" --cpus 5"
DOCKER_RUN_ARGS_+=" --name $CONTAINER_NAME"
if [[ "$DOCKER_NETWORK" != "" ]] ; then
    DOCKER_RUN_ARGS_ = "${DOCKER_RUN_ARGS_} --network ${DOCKER_NETWORK}"
fi

[[ "$($DOCKER ps -a | grep $CONTAINER_NAME)" ]] && $DOCKER rm -f $CONTAINER_NAME

# FYI possible error: https://stackoverflow.com/questions/67162945/docker-for-windows-the-requested-resource-is-in-use-error-when-mem-limit-is-set

echo "Running docker build (${CONTAINER_IMAGE} for ${USE_TEST_BASENAME})(usually takes about 7 hrs)"
$DOCKER run \
    ${DOCKER_RUN_ARGS_} \
    --storage-opt "size=125GB" \
    -v `cygpath --windows --absolute .`:c:\\LAUNCHEDFROM \
    $CONTAINER_IMAGE

$DOCKER exec \
    $CONTAINER_NAME \
    bash -c "git clone https://github.com/SophistSolutions/Stroika.git --branch $BRANCH"

# NB: https://stroika.atlassian.net/browse/STK-762 -  -l needed for building openssl on MSYS
$DOCKER exec \
    --env USE_TEST_BASENAME=$USE_TEST_BASENAME \
    $CONTAINER_NAME \
    bash -l -c "export PARALELLMAKEFLAG=-j5; cd c:/Stroika; ./ScriptsLib/RegressionTests; cp Tests/HistoricalRegressionTestResults/REGRESSION-TESTS-*-OUT.txt c:/launchedfrom/Tests/HistoricalRegressionTestResults/; cp Tests/HistoricalPerformanceRegressionTestResults/PerformanceDump-* c:/launchedfrom/Tests/HistoricalPerformanceRegressionTestResults/"

echo "Done with build, and copied out results to (Tests/HistoricalRegressionTestResults/REGRESSION-TESTS-$USE_TEST_BASENAME-$VERSION-OUT.txt Tests/HistoricalPerformanceRegressionTestResults/PerformanceDump-$USE_TEST_BASENAME-x86-$VERSION.txt Tests/HistoricalPerformanceRegressionTestResults/PerformanceDump-$USE_TEST_BASENAME-x86_64-$VERSION.txt)"
# Try copy with (above) -v bind-mount, because stopping and copying now (used to work) - causes docker destkop installation to fail/become corrupt
# $DOCKER exec \
#     --env USE_TEST_BASENAME=$USE_TEST_BASENAME \
#     $CONTAINER_NAME \
#     bash -l -c "cd c:/Stroika; cp Tests/HistoricalRegressionTestResults/REGRESSION-TESTS-$USE_TEST_BASENAME-$VERSION-OUT.txt c:/launchedfrom/Tests/HistoricalRegressionTestResults/"
# $DOCKER exec \
#     --env USE_TEST_BASENAME=$USE_TEST_BASENAME \
#     $CONTAINER_NAME \
#     bash -l -c "cd c:/Stroika; cp Tests/HistoricalRegressionTestResults/PerformanceDump-$USE_TEST_BASENAME-* c:/launchedfrom/Tests/HistoricalPerformanceRegressionTestResults/"

$DOCKER stop $CONTAINER_NAME
# echo "Done with build, so copying out results to (Tests/HistoricalRegressionTestResults/REGRESSION-TESTS-$USE_TEST_BASENAME-$VERSION-OUT.txt Tests/HistoricalPerformanceRegressionTestResults/PerformanceDump-$USE_TEST_BASENAME-x86-$VERSION.txt Tests/HistoricalPerformanceRegressionTestResults/PerformanceDump-$USE_TEST_BASENAME-x86_64-$VERSION.txt)"
# $DOCKER cp $CONTAINER_NAME:Stroika/Tests/HistoricalRegressionTestResults/REGRESSION-TESTS-$USE_TEST_BASENAME-$VERSION-OUT.txt Tests/HistoricalRegressionTestResults/
# $DOCKER cp $CONTAINER_NAME:Stroika/Tests/HistoricalPerformanceRegressionTestResults/PerformanceDump-$USE_TEST_BASENAME-x86-$VERSION.txt Tests/HistoricalPerformanceRegressionTestResults/
# $DOCKER cp $CONTAINER_NAME:Stroika/Tests/HistoricalPerformanceRegressionTestResults/PerformanceDump-$USE_TEST_BASENAME-x86_64-$VERSION.txt Tests/HistoricalPerformanceRegressionTestResults/
echo "Container $CONTAINER_NAME stopped but still around to examine/post-mortem"
