IF(NOT STM32Cube_DIR)
    SET(STM32Cube_DIR "/opt/STM32Cube_FW_F1_V1.2.0")
    MESSAGE(STATUS "No STM32Cube_DIR specified, using default: " ${STM32Cube_DIR})
ENDIF()

SET(FATFS_COMMON_SOURCES
    diskio.c
    ff.c
    ff_gen_drv.c
)

IF(NOT STORAGE_DRIVER)
    MESSAGE(STATUS "No storage driver specified")
    SET(STORAGE_DRIVER "NONE")
ELSE()
    FOREACH(DRIVER ${STORAGE_DRIVER})
        IF(DRIVER STREQUAL "SDCARD")
            LIST(APPEND FATFS_DRIVER_SOURCES sd_diskio.c)
            LIST(APPEND FATFS_DRIVER_HEADERS sd_diskio.h)
         ELSEIF(DRIVER STREQUAL "SDCARD-DMA")
            LIST(APPEND FATFS_DRIVER_SOURCES sd_diskio_dma.c)
            LIST(APPEND FATFS_DRIVER_HEADERS sd_diskio_dma.h)
         ELSEIF(DRIVER STREQUAL "SDCARD-DMA-RTOS")
            LIST(APPEND FATFS_DRIVER_SOURCES sd_diskio_dma_rtos)
            LIST(APPEND FATFS_DRIVER_HEADERS sd_diskio_dma_rtos.h)
         ELSEIF(DRIVER STREQUAL "SDRAM")
            LIST(APPEND FATFS_DRIVER_SOURCES sdram_diskio.c)
            LIST(APPEND FATFS_DRIVER_HEADERS sdram_diskio.c)
         ELSEIF(DRIVER STREQUAL "SRAM")
            LIST(APPEND FATFS_DRIVER_SOURCES sram_diskio.c)
            LIST(APPEND FATFS_DRIVER_HEADERS sram_diskio.h)
         ELSEIF(DRIVER STREQUAL "USBH")
            LIST(APPEND FATFS_DRIVER_SOURCES usbh_diskio.c)
            LIST(APPEND FATFS_DRIVER_HEADERS usbh_diskio.h)
         ELSEIF(DRIVER STREQUAL "USBH-DMA")
            LIST(APPEND FATFS_DRIVER_SOURCES usbh_diskio_dma.c)
            LIST(APPEND FATFS_DRIVER_HEADERS usbh_diskio_dma.h)
         ELSEIF(DRIVER STREQUAL "PPP")
            LIST(APPEND FATFS_DRIVER_SOURCES ppp_diskio.c)
            LIST(APPEND FATFS_DRIVER_HEADERS ppp_diskio.h)
         ELSE()
            MESSAGE(STATUS "Unspecified storage driver (options are: SDCARD SDCARD-DMA SDCARD-DMA-RTOS SDRAM SRAM USBH USBH-DMA PPP)")
        ENDIF()
    ENDFOREACH()
ENDIF()

MESSAGE(STATUS "FATFS_STORAGE_DRIVER: ${STORAGE_DRIVER}")

SET(FATFS_OPTION_SOURCES syscall.c unicode.c)

SET(FATFS_COMMON_HEADERS
    diskio.h
    ff.h
    ff_gen_drv.h
    ffconf_template.h
    integer.h
)

FIND_PATH(FATFS_COMMON_INCLUDE_DIR ${FATFS_COMMON_HEADERS}
    HINTS ${STM32Cube_DIR}/Middlewares/Third_Party/FatFs/src
    CMAKE_FIND_ROOT_PATH_BOTH
)

FIND_PATH(FATFS_DRIVER_INCLUDE_DIR ${FATFS_DRIVER_HEADERS}
    HINTS ${STM32Cube_DIR}/Inc
    CMAKE_FIND_ROOT_PATH_BOTH
)

SET(FATFS_INCLUDE_DIRS ${FATFS_COMMON_INCLUDE_DIR})
IF(NOT STORAGE_DRIVER STREQUAL NONE)
    LIST(APPEND FATFS_INCLUDE_DIRS ${FATFS_DRIVER_INCLUDE_DIR})
ENDIF()

FOREACH(SRC ${FATFS_COMMON_SOURCES})
    SET(SRC_FILE SRC_FILE-NOTFOUND)
    FIND_FILE(SRC_FILE ${SRC}
        HINTS ${STM32Cube_DIR}/Middlewares/Third_Party/FatFs/src/
        CMAKE_FIND_ROOT_PATH_BOTH
    )
    LIST(APPEND FATFS_SOURCES ${SRC_FILE})
ENDFOREACH()

FOREACH(SRC ${FATFS_DRIVER_SOURCES})
    SET(SRC_FILE SRC_FILE-NOTFOUND)
    FIND_FILE(SRC_FILE ${SRC}
        HINTS ${STM32Cube_DIR}/Src
        CMAKE_FIND_ROOT_PATH_BOTH
    )
    LIST(APPEND FATFS_SOURCES ${SRC_FILE})
ENDFOREACH()

FOREACH(SRC ${FATFS_OPTION_SOURCES})
    SET(SRC_FILE SRC_FILE-NOTFOUND)
    FIND_FILE(SRC_FILE ${SRC}
        HINTS ${STM32Cube_DIR}/Middlewares/Third_Party/FatFs/src/option/
        CMAKE_FIND_ROOT_PATH_BOTH
    )
    LIST(APPEND FATFS_SOURCES ${SRC_FILE})
ENDFOREACH()

MESSAGE(STATUS "FATFS_SOURCES: ${FATFS_SOURCES}")

INCLUDE(FindPackageHandleStandardArgs)

FIND_PACKAGE_HANDLE_STANDARD_ARGS(FATFS DEFAULT_MSG FATFS_INCLUDE_DIRS FATFS_SOURCES)
