
# [2020y-08m-24d][00:00:00]. WorkSpace project.
# [2021y-03m-07d][07:17:23]. Idrisov D. R.
################################################################################

function(check_exist_symptom path symptom result)
    set(content)
    FILE (GLOB content "${path}/${symptom}")
    list(FILTER content EXCLUDE REGEX "^_.*")
    list(FILTER content EXCLUDE REGEX "^.*/_.*")
    if(content)
        # message(STATUS "check_exist_symptom: ${symptom}: ON")
        set(${result} "ON" PARENT_SCOPE)
    else()
        # message(STATUS "check_exist_symptom: ${symptom}: OFF")
        set(${result} "" PARENT_SCOPE)
    endif()
endfunction()

function(check_current_symptoms var_output where symptoms)
    if(NOT symptoms)
        set(${var_output} "ON" PARENT_SCOPE)
        return()
    endif()
    foreach(symptom ${symptoms})
        set(result)
        check_exist_symptom("${where}" "${symptom}" result)
        if(result)
            set(${var_output} "ON" PARENT_SCOPE)
            return()
        endif()
    endforeach()
    set(${var_output} "" PARENT_SCOPE)
endfunction()

function(check_symptoms3 var_output where symptoms1 symptoms2 symptoms3)
    set(result)
    check_current_symptoms(result "${where}" "${symptoms1}")
    if(result)
        check_current_symptoms(result "${where}" "${symptoms2}")
        if(result)
            check_current_symptoms(result "${where}" "${symptoms3}")
            if(result)
                set(${var_output} "${where}" PARENT_SCOPE)
                return()
            endif()
        endif()
    endif()
    set(${var_output} "" PARENT_SCOPE)
endfunction()

function(find_symptoms var_output where)
    set(result)
    set(again ON)
    while(again)
        check_symptoms3(result "${where}" "${ARGV2}" "${ARGV3}" "${ARGV4}")
        if(result) 
            break()
        endif()
        get_filename_component(parent "${where}/.." ABSOLUTE)
        if("${parent}" STREQUAL "${where}")
            break()
        endif()
        set(where "${parent}")
    endwhile()
    set(${var_output} "${result}" PARENT_SCOPE)
endfunction()

################################################################################

