#
#    Copyright 2019-2020 Kai Pastor
#    
#    This file is part of OpenOrienteering.
# 
#    OpenOrienteering is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
# 
#    OpenOrienteering is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
# 
#    You should have received a copy of the GNU General Public License
#    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.


# Configure the given input file, and install the result to the output dir.
# The optional third argument may specify an alternative destination filename.
function(configure_and_install in_file out_dir)
	if(ARGN)
		set(filename "${ARGN}")
	else()
		get_filename_component(filename "${in_file}" NAME)
	endif()
	set(tmp_file "@MAPPER_PRO_DIR@/tmp/${filename}")
	# Configure first, so that install can properly detect unchanged files.
	configure_file("${in_file}" "${tmp_file}" @ONLY)
	# Regular install, for regular diagnostic output
	file(INSTALL "${tmp_file}" DESTINATION "${out_dir}")
endfunction()


set(ANDROID_PACKAGE_SOURCE_DIR "@ANDROID_PACKAGE_SOURCE_DIR@")
file(INSTALL "@MAPPER_PRO_DIR@/AndroidManifest.xml" DESTINATION "${ANDROID_PACKAGE_SOURCE_DIR}")
file(INSTALL "@MAPPER_PRO_DIR@/tmp/strings.xml" DESTINATION "${ANDROID_PACKAGE_SOURCE_DIR}/res/values")

set(MAPPER_APP_ID "@MAPPER_APP_ID@")
set(Mapper_VERSION "@Mapper_VERSION@")
foreach(file @INPUT_FILES@)
	if("${file}" MATCHES "java/(org/openorienteering/mapper/.*.java)")
		configure_and_install("@INPUT_SOURCE_DIR@/${file}" "${ANDROID_PACKAGE_SOURCE_DIR}/src/org/openorienteering/mapper" "${CMAKE_MATCH_1}")
	endif()
endforeach()

set(icons
  drawable-ldpi   36
  drawable-mdpi   48
  drawable-hdpi   72
  drawable-xhdpi  96
)
while(icons)
	list(GET icons 0 drawable)
	list(GET icons 1 size)
	file(INSTALL
	  DESTINATION "${ANDROID_PACKAGE_SOURCE_DIR}/res/${drawable}"
	  TYPE FILE
	  FILES "@PROJECT_SOURCE_DIR@/images/mapper-icon/Mapper-${size}.png"
	  RENAME "icon.png"
	)
	list(REMOVE_AT icons 0 1)
endwhile()

$<$<VERSION_LESS:@Qt5Core_VERSION@,5.15.0>:
# Copy the Mapper runtime from the CMake project to the qmake project.
# androiddeployqt uses this location.
file(INSTALL
  DESTINATION "@MAPPER_PRO_DIR@"
  FILES "$<TARGET_FILE:Mapper>"
  )
>

# Copy the Mapper runtime from the CMake project to the packaging dir.
# androiddeployqt checks this location.
file(INSTALL
  DESTINATION "@MAPPER_PRO_DIR@/android-build/libs/@ANDROID_ABI@"
  FILES "$<TARGET_FILE:Mapper>"
)
