#!/bin/bash


#-----------------------------------------------------------------------------------------------------
# Some settings
#-----------------------------------------------------------------------------------------------------
URI_BASE="https://core-11.t3c.test/styleguide-demo-167"
URI_DEFAULT="${URI_BASE}/menu-section-pages"
URI_LANGUAGE_1="${URI_BASE}/da/menu-section-pages"
URI_LANGUAGE_2="${URI_BASE}/de/menu-section-pages"
PATH_CORE="/var/www/work/t3c/core-11/core-11"
TYPO3_BIN="bin/typo3"
# TYPO3_BIN="php7.4 bin/typo3"
#-----------------------------------------------------------------------------------------------------
#URI_BASE="https://core-main.t3c.test/styleguide-demo-206"
#URI_DEFAULT="${URI_BASE}/menu-section-pages"
#URI_LANGUAGE_1="${URI_BASE}/da/menu-section-pages"
#URI_LANGUAGE_2="${URI_BASE}/de/menu-section-pages"
#PATH_CORE="/var/www/work/t3c/core-main/core-main"
#TYPO3_BIN="bin/typo3"
#-----------------------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------------------------
# working variables
#-----------------------------------------------------------------------------------------------------
requestCounter=0
total=0
success=0
failed=0
#-----------------------------------------------------------------------------------------------------
allRequests=()
successRequests=()
failedRequests=()
#-----------------------------------------------------------------------------------------------------


function callUriWithExpectation()
{
    callUrl="$1"
    expectDebugCache="$2"
    callResult="$( curl -v "${callUrl}" 2>&1 | grep x-typo3-debug-cache )"
    hasDebugCache=0
    [[ -n "${callResult}" ]] && hasDebugCache=1

    ((total+=1))
    echo -n " $( printf 'URL: %-80s EXPECT: %s HAS: %s' \"${callUrl}\" \"${expectDebugCache}\" \"${hasDebugCache}\" )"
    if [[ "${expectDebugCache}" -eq 0 ]]; then
        if [[ "${hasDebugCache}" -eq 1 ]]; then
            ((failed+=1))
            allRequests+=("$( printf '[#%4s] Called \"%80s\" expecting \"%s\" , got \"%s\" : %s' "${total}" "${callUrl}" "${expectDebugCache}" "${hasDebugCache}" "failed")")
            failedRequests+=("$( printf '[#%4s] Called \"%80s\" expecting \"%s\" , got \"%s\" : %s' "${total}" "${callUrl}" "${expectDebugCache}" "${hasDebugCache}" "failed")")
            echo " - failed"
        else            
            ((success+=1))
            allRequests+=("$( printf '[#%4s] Called \"%80s\" expecting \"%s\" , got \"%s\" : %s' "${total}" "${callUrl}" "${expectDebugCache}" "${hasDebugCache}" "success")")
            successRequests+=("$( printf '[#%4s] Called \"%80s\" expecting \"%s\" , got \"%s\" : %s' "${total}" "${callUrl}" "${expectDebugCache}" "${hasDebugCache}" "success")")
            echo " - success"
        fi    
    else
        if [[ "${hasDebugCache}" -eq 1 ]]; then
            ((success+=1))
            allRequests+=("$( printf '[#%4s] Called \"%80s\" expecting \"%s\" , got \"%s\" : %s' "${total}" "${callUrl}" "${expectDebugCache}" "${hasDebugCache}" "success")")
            successRequests+=("$( printf '[#%4s] Called \"%80s\" expecting \"%s\" , got \"%s\" : %s' "${total}" "${callUrl}" "${expectDebugCache}" "${hasDebugCache}" "success")")            
            echo " - success"
        else
            ((failed+=1))
            allRequests+=("$( printf '[#%4s] Called \"%80s\" expecting \"%s\" , got \"%s\" : %s' "${total}" "${callUrl}" "${expectDebugCache}" "${hasDebugCache}" "failed")")
            failedRequests+=("$( printf '[#%4s] Called \"%80s\" expecting \"%s\" , got \"%s\" : %s' "${total}" "${callUrl}" "${expectDebugCache}" "${hasDebugCache}" "failed")")            
            echo " - failed"
        fi    
    fi
}

# clear caches
cd "${PATH_CORE}" && ${TYPO3_BIN} cache:flush && ${TYPO3_BIN} cache:warmup && echo "[I] Cache cleared, start with requests now ..."

# Calls on default language
echo "[I] DEFAULT LANGUAGE #1"
echo -n "#001" ; callUriWithExpectation "${URI_DEFAULT}" 0
echo -n "#002" ; callUriWithExpectation "${URI_DEFAULT}" 1
echo -n "#003" ; callUriWithExpectation "${URI_DEFAULT}" 1
echo -n "#004" ; callUriWithExpectation "${URI_DEFAULT}" 1

# Calls on first language
echo "[I] FIRST LANGUAGE #1"
echo -n "#005" ; callUriWithExpectation "${URI_LANGUAGE_1}" 0
echo -n "#006" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#007" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#008" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1

# Calls on second language
echo "[I] SECOND LANGUAGE #1"
echo -n "#009" ; callUriWithExpectation "${URI_LANGUAGE_2}" 0
echo -n "#010" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#011" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#012" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1

# Calls on first language again, should now all delivered from cache
echo "[I] FIRST LANGUAGE #2"
echo -n "#013" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#014" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#015" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#016" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1

# Calls on second language again, should now all delivered from cache
echo "[I] SECOND LANGUAGE #2"
echo -n "#017" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#018" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#019" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#020" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1

# Calls on first language again, should now all delivered from cache
echo "[I] FIRST LANGUAGE #3"
echo -n "#021" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#022" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#023" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#024" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1

# Calls on second language again, should now all delivered from cache
echo "[I] SECOND LANGUAGE #3"
echo -n "#025" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#026" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#027" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#028" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1

# Calls on first language again, should now all delivered from cache
echo "[I] FIRST LANGUAGE #4"
echo -n "#029" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#030" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#031" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1
echo -n "#032" ; callUriWithExpectation "${URI_LANGUAGE_1}" 1

# Calls on second language again, should now all delivered from cache
echo "[I] SECOND LANGUAGE #4"
echo -n "#033" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#034" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#035" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1
echo -n "#036" ; callUriWithExpectation "${URI_LANGUAGE_2}" 1

echo ""
echo "---------------------------------------------------------------------------------------------------------------------------------------------------------------------"
echo "[I] Requests completed. TOTAL: ${total} SUCCESS: ${success} FAILED: ${failed}"
echo "---------------------------------------------------------------------------------------------------------------------------------------------------------------------"
