# The MIT License (MIT)
# Copyright © 2016 Naiyang Lin <maxint@foxmail.com>

include(${ARCBUILD}/core.cmake)


function(arcbuild_parse_cmake_command_argv argv_var_name)
  set(results)

  # Create cmake cache entry manually
  set(unparsed_args)
  set(var_i 0)
  set(is_last_define_declare 0)
  while(${var_i} LESS ${CMAKE_ARGC})
    set(arg_name "CMAKE_ARGV${var_i}")
    set(arg ${${arg_name}})
    # message("${var_i}: |${arg}|")
    get_filename_component(arg_name "${arg}" NAME)
    if(arg STREQUAL "-P" OR arg_name MATCHES "^(cmake|cmake.exe|arcbuild.cmake)$")
      # message("Ignore: ${arg}")
    else()
      if(arg STREQUAL "-D")
        set(is_last_define_declare 1)
      elseif(is_last_define_declare OR arg MATCHES "^\\-D[a-zA-Z_:]+=")
        if(is_last_define_declare)
          set(is_last_define_declare 0)
        else()
          string(SUBSTRING "${arg}" 2 -1 arg)
        endif()
        string(FIND "${arg}" "=" equal_pos)
        string(FIND "${arg}" ":" colon_pos)
        if(NOT colon_pos EQUAL -1 AND colon_pos LESS equal_pos)
          # <var>:<type>=<value>
          math(EXPR var_name_len "${colon_pos}")
          math(EXPR var_type_beg "${colon_pos}+1")
          math(EXPR var_type_len "${equal_pos}-${var_type_beg}")
          string(SUBSTRING "${arg}" ${var_type_beg} ${var_type_len} var_type)
        else()
          # <var>=<value>
          math(EXPR var_name_len "${equal_pos}")
          set(var_type)
        endif()
        math(EXPR var_value_beg "${equal_pos}+1")
        string(SUBSTRING "${arg}" 0 ${var_name_len} var_name)
        string(SUBSTRING "${arg}" ${var_value_beg} -1 var_value)

        # message("|${var_name}|${var_type}|${var_value}|")

        if(NOT DEFINED ${var_name})
          if(var_type)
            set(${var_name} "${var_value}" CACHE ${var_type} "")
          else()
            set(${var_name} "${var_value}" PARENT_SCOPE)
          endif()
        endif()
        list(APPEND results ${var_name})
      else()
        list(APPEND unparsed_args ${arg})
      endif()
    endif()
    math(EXPR var_i "${var_i}+1")
  endwhile()

  # Parse SOURCE_DIR
  if(NOT DEFINED SOURCE_DIR)
    list(LENGTH unparsed_args unparsed_args_len)
    if(unparsed_args_len EQUAL 1)
      set(SOURCE_DIR "${unparsed_args}" PARENT_SCOPE)
    elseif(unparsed_args_len GREATER 1)
      arcbuild_join(unparsed_args ", " ${unparsed_args})
      arcbuild_error("Multiple source directory is specified: ${unparsed_args}")
    endif()
  endif()

  set(${argv_var_name} ${results} PARENT_SCOPE)
endfunction()


# Check whether called by "cmake -P arcbuild.cmake"
function(arcbuild_check_script_mode var_name)
  set(var_i 0)
  while(${var_i} LESS ${CMAKE_ARGC})
    set(arg_name "CMAKE_ARGV${var_i}")
    # message("${var_i}: ${${arg_name}}")
    if(${arg_name} STREQUAL "-P")
      set(${var_name} 1 PARENT_SCOPE)
      return()
    endif()
    math(EXPR var_i "${var_i}+1")
  endwhile()
  set(${var_name} 0 PARENT_SCOPE)
endfunction()


arcbuild_check_script_mode(is_script_mode)
if(is_script_mode)
  unset(is_script_mode)
  arcbuild_parse_cmake_command_argv(arguments)
  # return()
  include(${ARCBUILD}/build.cmake)
  arcbuild_build(${arguments})
else()
  unset(is_script_mode)
  include(${ARCBUILD}/core.cmake)
endif()
