#
#    Copyright 2017 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/>.


execute_process(COMMAND "${CMAKE_COMMAND}" -E echo "Creating APK package")

# For Android, we don't use a CPack generator,
# but run androiddeployqt from this file.
set(CPACK_GENERATOR "")

set(destdir "@CMAKE_CURRENT_BINARY_DIR@/Mapper/install")
set(KEYSTORE_URL "@KEYSTORE_URL@")
set(KEYSTORE_ALIAS "@KEYSTORE_ALIAS@")

if(EXISTS "${destdir}")
	FILE(REMOVE_RECURSE "${destdir}")
endif()

execute_process(
  COMMAND "${CMAKE_COMMAND}"
    "-DCMAKE_INSTALL_PREFIX=${destdir}"
	"-DCMAKE_INSTALL_DO_STRIP=1"
	-P cmake_install.cmake
  WORKING_DIRECTORY "@PROJECT_BINARY_DIR@"
  RESULT_VARIABLE result
)
if(result)
	message(FATAL_ERROR "Installation failed: ${result}")
endif()

# Create an androiddeployqt configuration file via qmake project.
execute_process(
  COMMAND "$<TARGET_FILE:Qt5::qmake>"
  WORKING_DIRECTORY "@CMAKE_CURRENT_BINARY_DIR@/Mapper"
  RESULT_VARIABLE result
)
if(result)
	message(FATAL_ERROR "Creating APK configuration failed: ${result}")
endif()

# Copy the Mapper runtime from the CMake project to the qmake project.
file(INSTALL
  DESTINATION "@CMAKE_CURRENT_BINARY_DIR@/Mapper"
  FILES "$<TARGET_FILE:Mapper>"
)

set(sign )
set(result )
set(final_message )
$<$<AND:$<OR:$<CONFIG:Release>,$<CONFIG:MinSizeRel>>,$<BOOL:${KEYSTORE_URL}>,$<BOOL:${KEYSTORE_ALIAS}>>:
if("@CMAKE_HOST_UNIX@")
	execute_process(COMMAND "${CMAKE_COMMAND}" -E echo "Checking if we are running in a terminal")
	execute_process(COMMAND tty	RESULT_VARIABLE result)
	if(result)
		message(SEND_ERROR "Not running in a terminal, signing disabled.")
	else()
		set(sign --sign "${KEYSTORE_URL}" "${KEYSTORE_ALIAS}")
	endif()
endif()
if(NOT "@CMAKE_HOST_UNIX@" OR result)
	set(final_message "The build created an unsigned APK. To sign the APK, run:
  jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore '${KEYSTORE_URL}' /path/to/install-release-unsigned.apk ${KEYSTORE_ALIAS}
  zipalign 4 /path/to/install-release-unsigned.apk outfile.apk"
	)
endif()
>

execute_process(
  COMMAND androiddeployqt
    --output "${destdir}"
    --deployment "bundled"
    --gradle
    --verbose
$<$<OR:$<CONFIG:Release>,$<CONFIG:MinSizeRel>>:
    --release
  $<$<AND:$<BOOL:${KEYSTORE_URL}>,$<BOOL:${KEYSTORE_ALIAS}>>:
    ${sign}
  >
>
  WORKING_DIRECTORY "@CMAKE_CURRENT_BINARY_DIR@/Mapper"
  RESULT_VARIABLE result
)
if(result)
	message(FATAL_ERROR "Running androiddeployqt failed: ${result}")
endif()
$<$<AND:$<OR:$<CONFIG:Release>,$<CONFIG:MinSizeRel>>,$<BOOL:${KEYSTORE_URL}>,$<BOOL:${KEYSTORE_ALIAS}>>:
configure_file(
  "@CMAKE_CURRENT_BINARY_DIR@/Mapper/install/build/outputs/apk/install-release-signed.apk"
  "@PROJECT_BINARY_DIR@/@CPACK_PACKAGE_FILE_NAME@.apk"
  COPYONLY
)
>

if(final_message)
	message(STATUS "${final_message}")
endif()

