cmake_minimum_required(VERSION 3.5)
get_filename_component(VCPKG_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR} DIRECTORY)

string(REGEX REPLACE "([^-]*)-([^-]*)" "\\1" TRIPLET_SYSTEM_ARCH ${TARGET_TRIPLET})
string(REGEX REPLACE "([^-]*)-([^-]*)" "\\2" TRIPLET_SYSTEM_NAME ${TARGET_TRIPLET})

if(NOT EXISTS ${VCPKG_ROOT_DIR}/triplets/${TARGET_TRIPLET}.cmake)
    message(FATAL_ERROR "Unsupported target triplet. Toolchain file does not exist: ${VCPKG_ROOT_DIR}/triplets/${TARGET_TRIPLET}.cmake")
endif()

set(CMAKE_TOOLCHAIN_FILE ${VCPKG_ROOT_DIR}/triplets/${TARGET_TRIPLET}.cmake)
list(APPEND CMAKE_MODULE_PATH ${VCPKG_ROOT_DIR}/scripts/cmake)
set(CURRENT_INSTALLED_DIR ${VCPKG_ROOT_DIR}/installed/${TARGET_TRIPLET} CACHE PATH "Location to install final packages")
set(DOWNLOADS ${VCPKG_ROOT_DIR}/downloads CACHE PATH "Location to download sources and tools")
set(PACKAGES_DIR ${VCPKG_ROOT_DIR}/packages CACHE PATH "Location to store package images")
set(BUILDTREES_DIR ${VCPKG_ROOT_DIR}/buildtrees CACHE PATH "Location to perform actual extract+config+build")

if(PORT)
    set(CURRENT_BUILDTREES_DIR ${BUILDTREES_DIR}/${PORT})
    set(CURRENT_PACKAGES_DIR ${PACKAGES_DIR}/${PORT}_${TARGET_TRIPLET})
endif()

macro(debug_message)
    if(DEFINED PORT_DEBUG AND PORT_DEBUG)
        message(STATUS "[DEBUG] ${ARGN}")
    endif()
endmacro()

if(CMD MATCHES "^SCAFFOLD$")
    if(EXISTS ports/${PORT}/portfile.cmake)
        message(FATAL_ERROR "Portfile already exists: '${VCPKG_ROOT_DIR}/ports/${PORT}/portfile.cmake'")
    endif()
    if(NOT FILENAME)
        get_filename_component(FILENAME "${URL}" NAME)
    endif()
    string(REGEX REPLACE "(\\.(zip|gz|tar|tgz|bz2))+\$" "" ROOT_NAME ${FILENAME})
    if(EXISTS ${DOWNLOADS}/${FILENAME})
        message(STATUS "Using pre-downloaded: ${DOWNLOADS}/${FILENAME}")
        message(STATUS "If this is not desired, delete the file and ${VCPKG_ROOT_DIR}/ports/${PORT}/portfile.cmake")
    else()
        include(vcpkg_download_distfile)
        file(DOWNLOAD ${URL} ${DOWNLOADS}/${FILENAME} STATUS error_code)
        if(NOT error_code MATCHES "0;")
            message(FATAL_ERROR "Error downloading file: ${error_code}")
        endif()
    endif()
    file(MD5 ${DOWNLOADS}/${FILENAME} MD5)

    file(MAKE_DIRECTORY ports/${PORT})
    configure_file(scripts/templates/portfile.in.cmake ports/${PORT}/portfile.cmake @ONLY)

    message(STATUS "Generated portfile: ${VCPKG_ROOT_DIR}/ports/${PORT}/portfile.cmake")
elseif(CMD MATCHES "^BUILD$")
	if(NOT DEFINED CURRENT_PORT_DIR)
        message(FATAL_ERROR "CURRENT_PORT_DIR was not defined")
    endif()
    set(TO_CMAKE_PATH "${CURRENT_PORT_DIR}" CURRENT_PORT_DIR)
    if(NOT EXISTS ${CURRENT_PORT_DIR})
        message(FATAL_ERROR "Cannot find port: ${PORT}\n  Directory does not exist: ${CURRENT_PORT_DIR}")
    endif()
    if(NOT EXISTS ${CURRENT_PORT_DIR}/portfile.cmake)
        message(FATAL_ERROR "Port is missing portfile: ${CURRENT_PORT_DIR}/portfile.cmake")
    endif()
    if(NOT EXISTS ${CURRENT_PORT_DIR}/CONTROL)
        message(FATAL_ERROR "Port is missing control file: ${CURRENT_PORT_DIR}/CONTROL")
    endif()

    message(STATUS "CURRENT_INSTALLED_DIR=${CURRENT_INSTALLED_DIR}")
    message(STATUS "DOWNLOADS=${DOWNLOADS}")

    message(STATUS "CURRENT_PACKAGES_DIR=${CURRENT_PACKAGES_DIR}")
    message(STATUS "CURRENT_BUILDTREES_DIR=${CURRENT_BUILDTREES_DIR}")
    message(STATUS "CURRENT_PORT_DIR=${CURRENT_PORT_DIR}")

    unset(PACKAGES_DIR)
    unset(BUILDTREES_DIR)

    file(REMOVE_RECURSE ${CURRENT_PACKAGES_DIR})
    if(EXISTS ${CURRENT_PACKAGES_DIR})
        message(FATAL_ERROR "Unable to remove directory: ${CURRENT_PACKAGES_DIR}\n  Files are likely in use.")
    endif()
    file(MAKE_DIRECTORY ${CURRENT_BUILDTREES_DIR} ${CURRENT_PACKAGES_DIR})

    include(${CURRENT_PORT_DIR}/portfile.cmake)
endif()
