# require 3.15 for GNUInstallDirs
cmake_minimum_required(VERSION 3.15...3.18)

project(QuaZip VERSION 1.7.0)

include(cmake/clone-repo.cmake)

set(QUAZIP_LIB_VERSION ${QuaZip_VERSION})
set(QUAZIP_LIB_SOVERSION ${QuaZip_VERSION_MAJOR}.${QuaZip_VERSION_MINOR})

if(EMSCRIPTEN)
  #option(ZLIB_INCLUDE "Path to include dir" "")
  #option(ZLIB_LIBRARY "Path to library dir" "")
  option(BUILD_SHARED_LIBS "" OFF)
  option(QUAZIP_INSTALL "" OFF)
  option(QUAZIP_USE_QT_ZLIB "" OFF)
  option(QUAZIP_ENABLE_TESTS "Build QuaZip tests" OFF)
else()
  option(BUILD_SHARED_LIBS "" ON)
  option(QUAZIP_INSTALL "" ON)
  option(QUAZIP_USE_QT_ZLIB "" OFF)
  option(QUAZIP_ENABLE_TESTS "Build QuaZip tests" OFF)
endif()

OPTION(ZLIB_CONST "Sets ZLIB_CONST preprocessor definition" OFF)
option(QUAZIP_ENABLE_QTEXTCODEC "Enable use of QTextCodec (requires Qt5 or Qt6 Core5Compat)" ON)

# Make BZIP2 optional
option(QUAZIP_BZIP2 "Enables BZIP2 compression" ON)
option(QUAZIP_BZIP2_STDIO "Output BZIP2 errors to stdio" ON)

option(QUAZIP_FETCH_LIBS "Enables fetching third-party libraries if not found" ${WIN32})
option(QUAZIP_FORCE_FETCH_LIBS "Enables fetching third-party libraries always" OFF)

if (QUAZIP_USE_QT_ZLIB AND BUILD_SHARED_LIBS)
    message(FATAL_ERROR "Using BUILD_SHARED_LIBS=ON together with QUAZIP_USE_QT_ZLIB=ON is not supported." )
endif()

# Set the default value of `${QUAZIP_QT_MAJOR_VERSION}`.
# We search quietly for Qt6 and Qt5 in that order.
find_package(
  QT NAMES Qt6 Qt5
  QUIET COMPONENTS Core
)
if (NOT QT_FOUND)
    set(QT_VERSION_MAJOR 6)
endif()

set(QUAZIP_QT_MAJOR_VERSION ${QT_VERSION_MAJOR} CACHE STRING "Qt version to use (6 or 5), defaults to ${QT_VERSION_MAJOR}")

if (QUAZIP_QT_MAJOR_VERSION EQUAL 6)
    set(CMAKE_CXX_STANDARD 17)
else()
    set(CMAKE_CXX_STANDARD 14)
endif()

if(NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE RELEASE)
endif()

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_DEBUG_POSTFIX d)

set(QUAZIP_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(QUAZIP_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
if(NOT QUAZIP_LIB_FILE_NAME)
	set(QUAZIP_LIB_FILE_NAME quazip${QuaZip_VERSION_MAJOR}-qt${QUAZIP_QT_MAJOR_VERSION})
endif()
set(QUAZIP_LIB_TARGET_NAME QuaZip)
set(QUAZIP_DIR_NAME QuaZip-Qt${QUAZIP_QT_MAJOR_VERSION}-${QUAZIP_LIB_VERSION})
set(QUAZIP_PACKAGE_NAME QuaZip-Qt${QUAZIP_QT_MAJOR_VERSION})

message(STATUS "QUAZIP_QT_MAJOR_VERSION set to ${QUAZIP_QT_MAJOR_VERSION}")
message(STATUS "CMAKE_CXX_STANDARD set to ${CMAKE_CXX_STANDARD}")

if(QUAZIP_QT_MAJOR_VERSION EQUAL 6)
    find_package(Qt6 REQUIRED COMPONENTS Core 
            OPTIONAL_COMPONENTS Network Test)
    message(STATUS "Found Qt version ${Qt6_VERSION} at ${Qt6_DIR}")
    set(QUAZIP_QT_ZLIB_COMPONENT BundledZLIB)
    set(QUAZIP_QT_ZLIB_HEADER_COMPONENT ZlibPrivate)
    set(QUAZIP_LIB_LIBRARIES Qt6::Core )
    set(QUAZIP_TEST_QT_LIBRARIES Qt6::Core Qt6::Network Qt6::Test)
    set(QUAZIP_PKGCONFIG_REQUIRES "zlib, Qt6Core")
elseif(QUAZIP_QT_MAJOR_VERSION EQUAL 5)
    find_package(Qt5 REQUIRED COMPONENTS Core
            OPTIONAL_COMPONENTS Network Test)
    message(STATUS "Found Qt version ${Qt5_VERSION} at ${Qt5_DIR}")
    set(QUAZIP_QT_ZLIB_COMPONENT Zlib)
    set(QUAZIP_LIB_LIBRARIES Qt5::Core)
    set(QUAZIP_TEST_QT_LIBRARIES Qt5::Core Qt5::Network Qt5::Test)
    set(QUAZIP_PKGCONFIG_REQUIRES "zlib, Qt5Core")
else()
	message(FATAL_ERROR "Qt version ${QUAZIP_QT_MAJOR_VERSION} is not supported")
endif()

message(STATUS "Using Qt version ${QUAZIP_QT_MAJOR_VERSION}")

set(QUAZIP_QT_ZLIB_USED OFF)
if(QUAZIP_USE_QT_ZLIB)
    find_package(Qt${QUAZIP_QT_MAJOR_VERSION} OPTIONAL_COMPONENTS ${QUAZIP_QT_ZLIB_COMPONENT})
    set(QUAZIP_QT_ZLIB_COMPONENT_FOUND Qt${QUAZIP_QT_MAJOR_VERSION}${QUAZIP_QT_ZLIB_COMPONENT}_FOUND)
    if (DEFINED QUAZIP_QT_ZLIB_HEADER_COMPONENT)
        find_package(Qt${QUAZIP_QT_MAJOR_VERSION} OPTIONAL_COMPONENTS ${QUAZIP_QT_ZLIB_HEADER_COMPONENT})
        set(QUAZIP_QT_ZLIB_HEADER_COMPONENT_FOUND Qt${QUAZIP_QT_MAJOR_VERSION}${QUAZIP_QT_ZLIB_HEADER_COMPONENT}_FOUND)
    else()
        set(QUAZIP_QT_ZLIB_HEADER_COMPONENT_FOUND ON)
    endif()
    if(QUAZIP_QT_ZLIB_COMPONENT_FOUND AND QUAZIP_QT_ZLIB_HEADER_COMPONENT_FOUND)
        message(STATUS "Qt component ${QUAZIP_QT_ZLIB_COMPONENT} found")
        set(QUAZIP_LIB_LIBRARIES ${QUAZIP_LIB_LIBRARIES} Qt${QUAZIP_QT_MAJOR_VERSION}::${QUAZIP_QT_ZLIB_COMPONENT})
        if(DEFINED QUAZIP_QT_ZLIB_HEADER_COMPONENT)
            message(STATUS "Qt component ${QUAZIP_QT_ZLIB_HEADER_COMPONENT} found")
            set(QUAZIP_LIB_LIBRARIES ${QUAZIP_LIB_LIBRARIES} Qt${QUAZIP_QT_MAJOR_VERSION}::${QUAZIP_QT_ZLIB_HEADER_COMPONENT})
        endif()
        set(QUAZIP_QT_ZLIB_USED ON)
    else()
        message(FATAL_ERROR "QUAZIP_USE_QT_ZLIB was set but bundled zlib was not found. Terminating to prevent accidental linking to system libraries.")
    endif()
endif()

if(QUAZIP_QT_ZLIB_USED AND QUAZIP_QT_ZLIB_COMPONENT STREQUAL BundledZLIB)
  # Qt's new BundledZLIB uses z-prefix in zlib
  add_compile_definitions(Z_PREFIX)
endif()

if(NOT QUAZIP_QT_ZLIB_USED)

    if(EMSCRIPTEN)
      if(NOT DEFINED ZLIB_LIBRARY)
        message(WARNING "ZLIB_LIBRARY is not set")
      endif()

      if(NOT DEFINED ZLIB_INCLUDE)
        message(WARNING "ZLIB_INCLUDE is not set")
      else()
        include_directories(${ZLIB_INCLUDE})
      endif()

      if(NOT DEFINED ZCONF_INCLUDE)
        message(WARNING "ZCONF_INCLUDE is not set")
      else()
        include_directories(${ZCONF_INCLUDE})
      endif()

      set(QUAZIP_LIB_LIBRARIES ${QUAZIP_LIB_LIBRARIES} ${ZLIB_LIBRARY})
    else()
      find_package(ZLIB REQUIRED)
      set(QUAZIP_LIB_LIBRARIES ${QUAZIP_LIB_LIBRARIES} ZLIB::ZLIB)
    endif()
endif()

if (ZLIB_CONST)
    add_compile_definitions(ZLIB_CONST)
endif()

set(QUAZIP_DEP)
set(QUAZIP_INC)
set(QUAZIP_LIB)
set(QUAZIP_LBD)

if(QUAZIP_BZIP2)
    # Check if bzip2 is present
    set(QUAZIP_BZIP2 ON)

    if(NOT QUAZIP_FORCE_FETCH_LIBS)
        find_package(BZip2 QUIET)
    endif()

    if(BZIP2_FOUND AND NOT QUAZIP_FORCE_FETCH_LIBS)
        message(STATUS "Using BZIP2 ${BZIP2_VERSION_STRING}")

        list(APPEND QUAZIP_INC ${BZIP2_INCLUDE_DIRS})
        list(APPEND QUAZIP_LIB ${BZIP2_LIBRARIES})
        list(APPEND QUAZIP_LBD ${BZIP2_LIBRARY_DIRS})

        set(PC_PRIVATE_LIBS "${PC_PRIVATE_LIBS} -lbzip2")

        set(QUAZIP_BZIP2_BUILD FALSE)
    elseif(QUAZIP_FETCH_LIBS)
        include(FetchContent)
        set(_bzip2_primary_url "https://sourceware.org/pub/bzip2/bzip2-1.0.8.tar.gz")
        set(_bzip2_fallback_url "https://mirror.bazel.build/sourceware.org/pub/bzip2/bzip2-1.0.8.tar.gz")
        set(_bzip2_hash "SHA256=ab5a03176ee106d3f0fa90e381da478ddae405918153cca248e682cd0c4a2269")
        if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24")
            FetchContent_Declare(bzip2
                URL "${_bzip2_primary_url}" "${_bzip2_fallback_url}"
                URL_HASH ${_bzip2_hash}
                SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/bzip2)
        else()
            FetchContent_Declare(bzip2
                URL "${_bzip2_primary_url}"
                URL_HASH ${_bzip2_hash}
                SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/bzip2)
        endif()
        FetchContent_GetProperties(bzip2 POPULATED bzip2_POPULATED)
        if(NOT bzip2_POPULATED)
            FetchContent_MakeAvailable(bzip2)
        endif()
        set(BZIP2_SOURCE_DIR ${bzip2_SOURCE_DIR})
        set(BZIP2_BINARY_DIR ${bzip2_BINARY_DIR})

        # BZip2 repository does not support cmake so we have to create
        # the bzip2 library ourselves
        set(BZIP2_SRC
            ${BZIP2_SOURCE_DIR}/blocksort.c
            ${BZIP2_SOURCE_DIR}/bzlib.c
            ${BZIP2_SOURCE_DIR}/compress.c
            ${BZIP2_SOURCE_DIR}/crctable.c
            ${BZIP2_SOURCE_DIR}/decompress.c
            ${BZIP2_SOURCE_DIR}/huffman.c
            ${BZIP2_SOURCE_DIR}/randtable.c)

        set(BZIP2_HDR
            ${BZIP2_SOURCE_DIR}/bzlib.h
            ${BZIP2_SOURCE_DIR}/bzlib_private.h)

        add_library(bzip2 STATIC ${BZIP2_SRC} ${BZIP2_HDR})

        set_target_properties(bzip2 PROPERTIES
            PUBLIC_HEADER "${BZIP2_SOURCE_DIR}/bzlib.h"
        )
        target_include_directories(bzip2 PUBLIC
          $<BUILD_INTERFACE:${BZIP2_SOURCE_DIR}>
          $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
        )

        if(NOT QUAZIP_BZIP2_STDIO)
            target_compile_definitions(bzip2 PRIVATE -DBZ_NO_STDIO)
        endif()

        list(APPEND QUAZIP_DEP bzip2)
        list(APPEND QUAZIP_LIB bzip2)
        list(APPEND QUAZIP_INC ${BZIP2_SOURCE_DIR})

        set(QUAZIP_BZIP2_BUILD ON)
    else()
        message(STATUS "BZip2 library not found")

        set(QUAZIP_BZIP2 OFF)
    endif()

    if(QUAZIP_BZIP2)
        find_package(BZip2)
        add_compile_definitions(HAVE_BZIP2)
    endif()
endif()

if(QUAZIP_ENABLE_QTEXTCODEC)
    if(${QT_VERSION} VERSION_GREATER_EQUAL 6.0.0)
        find_package(Qt6 OPTIONAL_COMPONENTS Core5Compat)
        if(Qt6Core5Compat_FOUND)
            set(QUAZIP_LIB_LIBRARIES ${QUAZIP_LIB_LIBRARIES} Qt6::Core5Compat)
            set(QUAZIP_TEST_QT_LIBRARIES ${QUAZIP_TEST_QT_LIBRARIES} Qt6::Core5Compat)

            add_compile_definitions(QUAZIP_CAN_USE_QTEXTCODEC)
            message("-- Quazip can use QTextCodec")
        else()
            message("-- Core5Compat not found, QTextCodec support disabled")
        endif()
    else()
        add_compile_definitions(QUAZIP_CAN_USE_QTEXTCODEC)
        message("-- Quazip can use QTextCodec")
    endif()
else()
    message("-- QTextCodec explicitly disabled by QUAZIP_ENABLE_QTEXTCODEC=OFF")
endif()

add_subdirectory(quazip)

if(QUAZIP_ENABLE_TESTS)
    message(STATUS "Building QuaZip tests")
    enable_testing()
    add_subdirectory(qztest)
endif()
