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

include(${ARCBUILD}/core.cmake)


# Parse the command line arguments of cmake -P arcbuild.cmake.
# - parsed_entries_var is the var which stores the parsed entries.
# - unparsed_args_var is the var which stores unparsed arguments.
function(arcbuild_parse_cmake_command_argv parsed_entries_var unparsed_args_var)
  # Create cmake cache entry manually
  set(parsed_entries)
  set(unparsed_args)
  set(var_i 0)
  set(last_arg_name)
  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}")
    elseif(arg MATCHES "^(-D|-S|-B)$")
      set(last_arg_name ${arg})
    elseif(last_arg_name STREQUAL "-S")
      set(last_arg_name)
      set(SOURCE_DIR "${arg}" PARENT_SCOPE)
      list(APPEND parsed_entries SOURCE_DIR)
    elseif(last_arg_name STREQUAL "-B")
      set(last_arg_name)
      set(BINARY_DIR "${arg}" PARENT_SCOPE)
      list(APPEND parsed_entries BINARY_DIR)
    elseif(last_arg_name STREQUAL "-D" OR arg MATCHES "^\\-D[a-zA-Z_][a-zA-Z_0-9]*:?=")
      if(last_arg_name)
        set(last_arg_name)
      else()
        string(SUBSTRING "${arg}" 2 -1 arg) # remove -D
      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 parsed_entries ${var_name})
    else()
      list(APPEND unparsed_args ${arg})
    endif()
    math(EXPR var_i "${var_i}+1")
  endwhile()

  # If SOURCE_DIR is not in parsed arguments
  list(FIND parsed_entries SOURCE_DIR found_result)
  if(found_result EQUAL -1)
    if(unparsed_args)
      list(GET unparsed_args -1 SOURCE_DIR)
      list(REMOVE_AT unparsed_args -1)
      set(SOURCE_DIR ${SOURCE_DIR} PARENT_SCOPE)
    elseif(NOT ARCBUILD_VERSION STREQUAL "master")
      arcbuild_error("No source directory is specified!")
    endif()
  endif()

  set(${parsed_entries_var} ${parsed_entries} PARENT_SCOPE)
  set(${unparsed_args_var} ${unparsed_args} 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()
