#
# This module can be used to find Docker
#
#	Docker_EXECUTABLE
#

FIND_PROGRAM(Docker_EXECUTABLE
  NAMES
  docker
  PATHS
  /usr/bin
  /usr/local/bin)

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Docker REQUIRED_VARS Docker_EXECUTABLE)
MARK_AS_ADVANCED(Docker_EXECUTABLE)

MACRO(DockerBuild TARGET)
  STRING(TOLOWER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
  SET(VERSION "6.${CMAKE_HOST_SYSTEM_PROCESSOR}.${BUILD_TYPE}")
  ADD_CUSTOM_TARGET(
    ${TARGET}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Dockerfile
    COMMAND
    ${Docker_EXECUTABLE} build
      --build-arg DOCKER_ARCH=${CMAKE_HOST_SYSTEM_PROCESSOR}
      --build-arg DOCKER_BUILD=${BUILD_TYPE}
		  --build-arg DOCKER_NAMESPACE=${DOCKER_NAMESPACE}
		  --build-arg DOCKER_REGISTRY=${DOCKER_REGISTRY}
      -t ${DOCKER_REGISTRY}/${DOCKER_NAMESPACE}/${TARGET}:${VERSION}
      ${CMAKE_CURRENT_SOURCE_DIR}
    COMMAND
    ${Docker_EXECUTABLE} push 
      ${DOCKER_REGISTRY}/${DOCKER_NAMESPACE}/${TARGET}:${VERSION})
ENDMACRO()
