#
# CMakeNodeTools.txt
#
# Copyright (C) 2022 by RStudio, PBC
#
# This program is licensed to you under the terms of version 3 of the
# GNU Affero General Public License. This program is distributed WITHOUT
# ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
# AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
# 
#

# include guard
if(RSTUDIO_CMAKE_NODE_TOOLS_INCLUDED)
   return()
endif()
set(RSTUDIO_CMAKE_NODE_TOOLS_INCLUDED YES)

# set the node version
if(NOT DEFINED RSTUDIO_NODE_VERSION)
   set(RSTUDIO_NODE_VERSION "14.17.5")
endif()
if(DEFINED ENV{RSTUDIO_NODE_VERSION})
   set(RSTUDIO_NODE_VERSION $ENV{RSTUDIO_NODE_VERSION})
endif()

# set cmake env vars for node (NODEJS) and node tools, like YARN, and NPM

if(APPLE AND UNAME_M STREQUAL arm64)

   # make sure we're using arm64 binaries of node / npm for arm64 builds
   set(NODEJS "${HOMEBREW_PREFIX}/bin/node")
   set(NPM "${HOMEBREW_PREFIX}/bin/npm")

else()

   # Detect node.js, npm, and yarn; prefer versions supplied by the dependency
   # scripts but fall back to whatever is found on path.

   # node.js
   find_program(NODEJS
      NAMES node
      NO_DEFAULT_PATH PATH_SUFFIXES "bin"
      PATHS "/opt/rstudio-tools/dependencies/common/node/${RSTUDIO_NODE_VERSION}"
      "${CMAKE_CURRENT_LIST_DIR}/../../dependencies/common/node/${RSTUDIO_NODE_VERSION}")

   if(NOT NODEJS)
      # fall back on any available node.js
      find_program(NODEJS NAMES node)
   endif()

   if(NODEJS)
      message(STATUS "Using node.js: ${NODEJS}")
   else()
      message(FATAL_ERROR "node.js not found (required)")
   endif()

   # npm
   find_program(NPM
      NAMES npm
      PATH_SUFFIXES "bin"
      NO_DEFAULT_PATH 
      PATHS "/opt/rstudio-tools/dependencies/common/node/${RSTUDIO_NODE_VERSION}"
      "${CMAKE_CURRENT_LIST_DIR}/../../dependencies/common/node/${RSTUDIO_NODE_VERSION}")

   if (NOT NPM)
      find_program(NPM NAMES npm)
   endif()

   if(NPM)
      message(STATUS "Using npm: ${NPM}")
   else()
      message(FATAL_ERROR "npm not found (required for rsw-homepage)")
   endif()

endif()

# yarn
find_program(YARN
   NAMES yarn
   NO_DEFAULT_PATH
   PATHS "$ENV{HOME}/.yarn/bin")
if (NOT YARN)
   find_program(YARN NAMES yarn)
else()

endif()

if(YARN)
   message(STATUS "Using yarn: ${YARN}")
else()
   message(STATUS "yarn not found (required for Electron)")
   set(YARN yarn)
endif()

# cache variables
set(NODEJS "${NODEJS}" CACHE INTERNAL "")
set(YARN "${YARN}" CACHE INTERNAL "")
set(NPM "${NPM}" CACHE INTERNAL "")

# create paths from programs
get_filename_component(NODEJS_PATH ${NODEJS} DIRECTORY CACHE)
get_filename_component(YARN_PATH ${YARN} DIRECTORY CACHE)
get_filename_component(NPM_PATH ${NPM} DIRECTORY CACHE)

