# the first-time installer for an app that uses garage_updates. built once by
# the developer, per app:
#
#   cmake -S . -B build -A x64 -DGARAGE_APP_NAME="Garage Stash" -DGARAGE_APP_ID=net.imbenji.stash
#         -DGARAGE_PROJECT_ID=<project id> -DGARAGE_EXE_NAME=garage_stash.exe -DGARAGE_ICON=C:/.../app_icon.ico
#   cmake --build build --config Release
#
# and out comes "<AppName>-Setup.exe" with the stub inside it. build_installer.bat
# does the same with shorter flags.
cmake_minimum_required(VERSION 3.15)
project(garage_installer LANGUAGES CXX)

# static runtime, its handed to people who have nothing installed yet
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

set(GARAGE_APP_NAME "" CACHE STRING "The apps name as people see it")
set(GARAGE_APP_ID "" CACHE STRING "A stable id, e.g. net.imbenji.stash. Names the uninstall entry")
set(GARAGE_PROJECT_ID "" CACHE STRING "The Garage project the releases are published on")
set(GARAGE_EXE_NAME "" CACHE STRING "The apps exe inside a build, e.g. garage_stash.exe")
set(GARAGE_API_BASE "https://pay.imbenji.net/api" CACHE STRING "Where the update check goes")
set(GARAGE_PUBLISHER "" CACHE STRING "Shown in Installed apps. Optional")
set(GARAGE_ICON "" CACHE FILEPATH "An .ico for the installer, the stub and the shortcuts. Optional")
set(GARAGE_DEFAULT_LOCATION "me" CACHE STRING "me (LocalAppData) or everyone (Program Files)")
option(GARAGE_SHOW_CHOICE "Ask just me / everyone. Off goes with the default without asking" ON)
option(GARAGE_DESKTOP_SHORTCUT "A desktop shortcut as well as the start menu one" OFF)

foreach(_needed GARAGE_APP_NAME GARAGE_APP_ID GARAGE_PROJECT_ID GARAGE_EXE_NAME)
  if("${${_needed}}" STREQUAL "")
    message(FATAL_ERROR "${_needed} has to be set, e.g. -D${_needed}=...")
  endif()
endforeach()
if(NOT GARAGE_EXE_NAME MATCHES "\\.exe$")
  message(FATAL_ERROR "GARAGE_EXE_NAME is the file name with .exe on it, got \"${GARAGE_EXE_NAME}\"")
endif()
if(GARAGE_APP_ID MATCHES "[\\\\/:*?\"<>| ]")
  message(FATAL_ERROR "GARAGE_APP_ID ends up as a folder and a registry key, keep it to letters, digits, dots and dashes")
endif()
if(NOT GARAGE_DEFAULT_LOCATION MATCHES "^(me|everyone)$")
  message(FATAL_ERROR "GARAGE_DEFAULT_LOCATION is me or everyone, got \"${GARAGE_DEFAULT_LOCATION}\"")
endif()
if(GARAGE_ICON AND NOT EXISTS "${GARAGE_ICON}")
  message(FATAL_ERROR "GARAGE_ICON: theres no ${GARAGE_ICON}")
endif()


# these land inside L"..." in a header
foreach(_text GARAGE_APP_NAME GARAGE_APP_ID GARAGE_PROJECT_ID GARAGE_EXE_NAME GARAGE_API_BASE GARAGE_PUBLISHER)
  string(REPLACE "\\" "\\\\" ${_text}_C "${${_text}}")
  string(REPLACE "\"" "\\\"" ${_text}_C "${${_text}_C}")
endforeach()
# no slash on the end, the code puts its own on
string(REGEX REPLACE "/+$" "" GARAGE_API_BASE_C "${GARAGE_API_BASE_C}")

set(GARAGE_DEFAULT_EVERYONE_C 0)
if(GARAGE_DEFAULT_LOCATION STREQUAL "everyone")
  set(GARAGE_DEFAULT_EVERYONE_C 1)
endif()
set(GARAGE_SHOW_CHOICE_C 0)
if(GARAGE_SHOW_CHOICE)
  set(GARAGE_SHOW_CHOICE_C 1)
endif()
set(GARAGE_DESKTOP_SHORTCUT_C 0)
if(GARAGE_DESKTOP_SHORTCUT)
  set(GARAGE_DESKTOP_SHORTCUT_C 1)
endif()

set(_generated "${CMAKE_CURRENT_BINARY_DIR}/generated")
configure_file("installer_config.h.in" "${_generated}/installer_config.h" @ONLY)


# the stub is the plugins one, built here with this apps icon on it and put
# somewhere the .rc can name
set(_stub_dir "${CMAKE_CURRENT_SOURCE_DIR}/../windows/stub")
set(GARAGE_STUB_ICON "${GARAGE_ICON}")
set(GARAGE_STUB_OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/stub_out")
add_subdirectory("${_stub_dir}" "${CMAKE_CURRENT_BINARY_DIR}/stub")
set(GARAGE_STUB_FILE "${GARAGE_STUB_OUT_DIR}/garage_stub.exe")

set(GARAGE_ICON_LINE "")
if(GARAGE_ICON)
  file(TO_CMAKE_PATH "${GARAGE_ICON}" _icon)
  set(GARAGE_ICON_LINE "1 ICON \"${_icon}\"")
endif()
set(GARAGE_MANIFEST_FILE "${CMAKE_CURRENT_SOURCE_DIR}/installer.exe.manifest")
configure_file("installer.rc.in" "${_generated}/installer.rc" @ONLY)

set(_sources "installer.cpp" "${_generated}/installer_config.h" "${_generated}/installer.rc")
if(MSVC)
  list(APPEND _sources "installer.exe.manifest")
endif()

add_executable(garage_installer WIN32 ${_sources})
target_compile_features(garage_installer PRIVATE cxx_std_17)
target_compile_definitions(garage_installer PRIVATE UNICODE _UNICODE)
target_include_directories(garage_installer PRIVATE "${_generated}" "${_stub_dir}")
if(MSVC)
  target_compile_options(garage_installer PRIVATE /W4 /EHsc /utf-8)
else()
  # not msvc, so nothing merges the manifest for us. it goes in through the .rc
  set_property(SOURCE "${_generated}/installer.rc" APPEND PROPERTY COMPILE_DEFINITIONS GARAGE_RC_MANIFEST)
endif()
if(MINGW)
  target_link_options(garage_installer PRIVATE -municode "-Wl,--subsystem,windows")
endif()
target_link_libraries(garage_installer PRIVATE
  user32 shell32 advapi32 ole32 uuid comctl32 winhttp bcrypt)

# the .rc pulls the stub in as a file, which cmake cant see for itself
add_dependencies(garage_installer garage_stub)
set_property(SOURCE "${_generated}/installer.rc" APPEND PROPERTY OBJECT_DEPENDS "${GARAGE_STUB_FILE}")

# the name with whatever a file name cant have taken out
string(REGEX REPLACE "[\\\\/:*?\"<>|]" "" _file_name "${GARAGE_APP_NAME}")
set_target_properties(garage_installer PROPERTIES OUTPUT_NAME "${_file_name}-Setup")
