# the patch step is run before the update step
# so we need to checkout the 1.0 branch here

set(work_dir @ep_source_dir@)
set(proj_dir ${work_dir}/@proj@)

execute_process(
  COMMAND @Git_EXECUTABLE@ checkout -b branch_1.0 origin/1.0 -t
  WORKING_DIRECTORY ${proj_dir}
  ERROR_VARIABLE error_output
  RESULT_VARIABLE error_code
  )
if(error_code)
  if(NOT ${error_output} MATCHES "branch branch_1.0 already exists")
    message(FATAL_ERROR "${error_output}")
  endif()
endif()

INCLUDE(@CTK_SOURCE_DIR@/CMake/ctkFunctionLFtoCRLF.cmake)
INCLUDE(@CTK_SOURCE_DIR@/CMake/ctkFunctionApplyPatches.cmake)

SET(patch_dir @qtmobility_patch_dir@)
SET(configured_patch_dir @qtmobility_configured_patch_dir@)

# Variable required to properly configured the patch files
SET(QT_BINARY_DIR @QT_BINARY_DIR@)

SET(patch_files
)

IF(WIN32)
  CONFIGURE_FILE(${patch_dir}/QtMobilityGitBranch1.0-win32.patch.in
    ${configured_patch_dir}/QtMobilityGitBranch1.0-win32.patch @ONLY)
  LIST(APPEND patch_files
    ${configured_patch_dir}/QtMobilityGitBranch1.0-win32.patch
    )
ENDIF()

IF(UNIX)
  IF(APPLE)
    LIST(APPEND patch_files ${patch_dir}/QtMobilityGitBranch1.0-apple.patch)
  ENDIF()
ENDIF()

# Apply patches
ctkFunctionApplyPatches("@CTK_PATCH_EXECUTABLE@" "@qtmobility_src_dir@" "${patch_files}")

IF(UNIX)

  # replace all occurences of 'qmake' with the aboslute path to the
  # qmake executable found by CMake.
  # This is neccessary for custom Qt builds and for Unix/Linux systems
  # which use another name for the Qt4 qmake program (e.g. qmake-qt4)

  EXECUTE_PROCESS(
    COMMAND sed -i "s+qmake +@QT_QMAKE_EXECUTABLE@ +g" configure
    WORKING_DIRECTORY ${proj_dir}
    RESULT_VARIABLE error_code
  )

  IF(error_code)
    MESSAGE(FATAL_ERROR "Patching ${proj_dir}/configure file failed.")
  ENDIF()
ENDIF()
