include(FindPackageHandleStandardArgs)

if(DEFINED ENV{PHX_LOCATION})
    set(PHX_LOCATION $ENV{PHX_LOCATION})
endif()


set(PHX_INC_SUFFIX "include")
set(PHX_LIB_SUFFIX "lib")

find_path(PHX_INCLUDE_PATH phoenix_ver.h
            PATHS
            ${PHX_LOCATION}
            PATH_SUFFIXES
            "${PHX_INC_SUFFIX}"
            NO_DEFAULT_PATH
            DOC "PhoenixFD Include Dir")
            
set(PHX_VERSION_HEADER "${MTOA_INCLUDE_PATH}/phoenix_ver.h")

if(EXISTS ${PHX_VERSION_HEADER})
    file(STRINGS ${PHX_VERSION_HEADER} SRC_CONTENT)
    foreach(SRC_LINE ${SRC_CONTENT})
        if(${SRC_LINE} MATCHES "^#define PHOENIX_VERSION")
            set(PHX_VERSION_TYPE PHOENIX_VERSION)
        elseif(${SRC_LINE} MATCHES "^#define PHOENIX_VERSION_SHORT")
            set(PHX_VERSION_TYPE PHOENIX_VERSION_SHORT)
        elseif(${SRC_LINE} MATCHES "^#define PHOENIX_VERSION_NUMERIC")
            set(PHX_VERSION_TYPE PHOENIX_VERSION_NUMERIC)
        else()
            continue()
        endif()
        
        string(STRIP "${SRC_LINE}" LINE_BUFFER)
        string(REPLACE " " ";" LINE_BUFFER_LIST ${LINE_BUFFER})
        list(LENGTH LINE_BUFFER_LIST _LINE_LENGTH)
        list(GET LINE_BUFFER_LIST -1 "${PHX_VERSION_TYPE}")
    endforeach()
    if(DEFINED PHOENIX_VERSION)
        set(PHX_VERSION ${PHOENIX_VERSION})
    endif()
endif()

if(NOT DEFINED PHX_VERSION)
    message(FATAL_ERROR "Could not find PHX_VERSION")
endif()

set(PHX_AURLOADER_LIBRARY PHX_AULOADER_LIBRARY_LIBRARY-NOTFOUND)
find_library(PHX_AURLOADER_LIBRARY aurloader_s
    PATHS
    "${PHX_LOCATION}/${PHX_LIB_SUFFIX}"
    DOC "aurloader library"
)

if(PHX_AURLOADER_LIBRARY)
    set(PHX_LIBRIRES PHX_AURLOADER_LIBRARY)
endif()

set(PHX_AURRAMP_LIBRARY PHX_AURRAMP_LIBRARY-NOTFOUND)
find_library(PHX_AURRAMP_LIBRARY aurramp_s
    PATHS
    "${PHX_LOCATION}/${PHX_LIB_SUFFIX}"
    DOC "aurramp library"
)

if(PHX_AURRAMP_LIBRARY)
    list(APPEND PHX_LIBRIRES PHX_AURRAMP_LIBRARY)
endif()

set(PHX_PHXUTILS_LIBRARY PHX_PHXUTILS_LIBRARY-NOTFOUND)
find_library(PHX_PHXUTILS_LIBRARY phxutils_s
    PATHS
    "${PHX_LOCATION}/${PHX_LIB_SUFFIX}"
    DOC "phxutils library"
)

if(PHX_PHXUTILS_LIBRARY)
    list(APPEND PHX_LIBRIRES PHX_PHXUTILS_LIBRARY)
endif()

set(PHX_VUTILS_LIBRARY PHX_VUTILS_LIBRARY-NOTFOUND)
find_library(PHX_PHXUTILS_LIBRARY vutils_s
    PATHS
    "${PHX_LOCATION}/${PHX_LIB_SUFFIX}"
    DOC "vutils library"
)

if(PHX_VUTILS_LIBRARY)
    list(APPEND PHX_LIBRIRES PHX_VUTILS_LIBRARY)
endif()

set(PHX_GUI_UTILS_QT_LIBRARY PHX_GUI_UTILS_QT_LIBRARY-NOTFOUND)
find_library(PHX_GUI_UTILS_QT_LIBRARY gui_utils_qt_s
    PATHS
    "${PHX_LOCATION}/${PHX_LIB_SUFFIX}"
    DOC "gui_utils_qt library"
)

if(PHX_GUI_UTILS_QT_LIBRARY)
    list(APPEND PHX_LIBRIRES PHX_GUI_UTILS_QT_LIBRARY)
endif()

set(PHX_LIBRARY_PATH PHX_LIBRARY_PATH-NOTFOUND)

if(PHX_PHXUTILS_LIBRARY)
    set(PHX_LIBRARY_PATH "${PHX_LOCATION}/${PHX_LIB_SUFFIX}")
endif()

#set(PHX_LIBRIRES
#    ${PHX_AURLOADER_LIBRARY}
#    ${PHX_AURRAMP_LIBRARY}
#    ${PHX_PHXUTILS_LIBRARY}
#    ${PHX_VUTILS_LIBRARY}
#    ${PHX_GUI_UTILS_QT_LIBRARY}
#)

message(STATUS "PHX_LOCATION : ${PHX_LOCATION}")
message(STATUS "PHX_VERSION  : ${PHX_VERSION}")
message(STATUS "PHX_INCLUDE_PATH  : ${PHX_INCLUDE_PATH}")
message(STATUS "PHX_LIBRARY_PATH  : ${PHX_LIBRARY_PATH}")
message(STATUS "PHX_AURLOADER_LIBRARY : ${PHX_AURLOADER_LIBRARY}")
message(STATUS "PHX_AURRAMP_LIBRARY : ${PHX_AURRAMP_LIBRARY}")
message(STATUS "PHX_PHXUTILS_LIBRARY : ${PHX_PHXUTILS_LIBRARY}")
message(STATUS "PHX_VUTILS_LIBRARY : ${PHX_VUTILS_LIBRARY}")
message(STATUS "PHX_GUI_UTILS_QT_LIBRARY : ${PHX_GUI_UTILS_QT_LIBRARY}")


find_package_handle_standard_args(PHX 
    REQUIRED_VARS
        PHX_INCLUDE_PATH
        PHX_LIBRARY_PATH
        PHX_LIBRIRES
    VERSION_VAR
        PHX_VERSION
    )