if (NOT DEFINED MCU_SERIES)
    message(FATAL_ERROR "Please Define MCU Series Like STM32F4xx")
endif()
set(MAIN_PROG_DIR ${CMAKE_SOURCE_DIR}/main_prog)
set(DRIVERS_DEST ${CMAKE_SOURCE_DIR}/libs/Drivers)
set(CMSIS_SOURCE_DIR ${CMAKE_SOURCE_DIR}/Drivers/CMSIS)
set(CMSIS_SOURCE_DEST_DIR ${CMAKE_SOURCE_DIR}/libs/Drivers/CMSIS)

set(HAL_DRIVER_SOURCE_DIR ${CMAKE_SOURCE_DIR}/Drivers/${MCU_SERIES}_HAL_Driver)
set(HAL_DRIVER_DEST_DIR ${CMAKE_SOURCE_DIR}/libs/Drivers/${MCU_SERIES}_HAL_Driver)

if (NOT EXISTS ${CMAKE_SOURCE_DIR}/libs)
    file(MAKE_DIRECTORY  ${CMAKE_SOURCE_DIR}/libs)
    file(READ libs_template.txt DATA)
    file(WRITE  ${CMAKE_SOURCE_DIR}/libs/CMakeLists.txt ${DATA})
endif()

if (NOT EXISTS ${CMAKE_SOURCE_DIR}/libs/Drivers)
    file(MAKE_DIRECTORY  ${CMAKE_SOURCE_DIR}/libs/Drivers)
    file(READ drivers_template.txt DATA)
    file(WRITE  ${CMAKE_SOURCE_DIR}/libs/Drivers/CMakeLists.txt ${DATA})
endif()
if (NOT EXISTS ${CMSIS_SOURCE_DEST_DIR})
    if (NOT EXISTS ${CMSIS_SOURCE_DIR})
        message(FATAL_ERROR "Please copy CMSIS in ${CMSIS_SOURCE_DIR}")
    endif()
    file(COPY ${CMSIS_SOURCE_DIR} DESTINATION ${DRIVERS_DEST})
    file(REMOVE_RECURSE ${CMSIS_SOURCE_DIR})
    file(READ cmsis_template.txt DATA)
    file(WRITE  ${CMSIS_SOURCE_DEST_DIR}/CMakeLists.txt ${DATA})
endif()

if (NOT EXISTS ${HAL_DRIVER_DEST_DIR})
    if (NOT EXISTS ${HAL_DRIVER_SOURCE_DIR})
        message(FATAL_ERROR "Please copy HAL_DRIVER in ${HAL_DRIVER_SOURCE_DIR}")
    endif()
    file(COPY ${HAL_DRIVER_SOURCE_DIR} DESTINATION "${DRIVERS_DEST}")
    file(REMOVE_RECURSE ${HAL_DRIVER_SOURCE_DIR})
    file(READ hal_driver_template.txt DATA)
    file(WRITE  ${HAL_DRIVER_DEST_DIR}/CMakeLists.txt ${DATA})
endif()


if (NOT EXISTS ${MAIN_PROG_DIR})
    file(MAKE_DIRECTORY  ${MAIN_PROG_DIR})
    file(WRITE  ${HAL_DRIVER_DEST_DIR}/CMakeLists.txt ${DATA})
    file(READ main_prog_template.txt DATA)
    file(WRITE  ${MAIN_PROG_DIR}/CMakeLists.txt ${DATA})
endif()




