#  Copyright (C) 2019 The authors of Py-ChemShell
#
#  This file is part of Py-ChemShell.
#
#  Py-ChemShell is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  Py-ChemShell is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with Py-ChemShell.  If not, see
#  <http://www.gnu.org/licenses/>.

# you.lu@stfc.ac.uk
# Oct 2019


function(needRebuild REBUILD REINSTALL EXTERNAL_STATIC_LIBS EXTERNAL_LIB_DIR CHEMSH_EXTERNAL_LIB_DIR)

    if(${REBUILD})
        message("${BoldRed}${REBUILD}${EndColour} set ON and a recompile will be enforced")
        set(${REBUILD}   1 PARENT_SCOPE)
        set(${REINSTALL} 1 PARENT_SCOPE)

    else()

        foreach(EXTERNAL_STATIC_LIB ${${EXTERNAL_STATIC_LIBS}})
            # count how many libs are already installed
            if(EXISTS ${${CHEMSH_EXTERNAL_LIB_DIR}}/${EXTERNAL_STATIC_LIB})
                list(APPEND EXTERNAL_INSTALLED_LIBS ${${CHEMSH_EXTERNAL_LIB_DIR}}/${EXTERNAL_STATIC_LIB})
            endif()
            # count the number of existing compiled lib files in given EXTERNAL directory
            if(EXISTS ${${EXTERNAL_LIB_DIR}}/${EXTERNAL_STATIC_LIB})
                list(APPEND EXTERNAL_FOUND_LIBS ${${EXTERNAL_LIB_DIR}}/${EXTERNAL_STATIC_LIB})
            else()
                message("${${EXTERNAL_LIB_DIR}}/${EXTERNAL_STATIC_LIB} not found, omitting...")
            endif()
        endforeach(EXTERNAL_STATIC_LIB)
        list(LENGTH EXTERNAL_INSTALLED_LIBS NUMBER_EXTERNAL_INSTALLED_LIBS)
        list(LENGTH EXTERNAL_FOUND_LIBS     NUMBER_EXTERNAL_FOUND_LIBS)
    
        # force to recompile if no pre-compiled libs found
        if(${NUMBER_EXTERNAL_INSTALLED_LIBS} EQUAL 0 AND ${NUMBER_EXTERNAL_FOUND_LIBS} EQUAL 0)
            set(${REBUILD}   1 PARENT_SCOPE)
            set(${REINSTALL} 1 PARENT_SCOPE)
            message("${BoldRed}${REBUILD}${EndColour} set OFF but no static library file(s) found under ${${EXTERNAL_LIB_DIR}} or ${${CHEMSH_EXTERNAL_LIB_DIR}}: a recompile is enforced")
        else()
            # do nothing if there are no less installed libs than found libs
            if(NOT ${NUMBER_EXTERNAL_INSTALLED_LIBS} LESS ${NUMBER_EXTERNAL_FOUND_LIBS})
                set(${REBUILD}   0 PARENT_SCOPE)
                set(${REINSTALL} 0 PARENT_SCOPE)
                message("${BoldRed}${REBUILD}${EndColour} set OFF: will not recompile")
            # reinstall but do not recompile if there are more found libs than installed libs
            else()
                set(${REBUILD}   0 PARENT_SCOPE)
                set(${REINSTALL} 1 PARENT_SCOPE)
                message("${BoldRed}${REBUILD}${EndColour} set OFF but new static library file(s) found under ${${EXTERNAL_LIB_DIR}}: all the libraries will be reinstalled")
            endif()
        endif()

    endif()

endfunction(needRebuild)
