#!/usr/bin/env bash

#
# install-dependencies-osx
#
# Copyright (C) 2022 by Posit Software, PBC
#
# Unless you have received this program directly from Posit Software pursuant
# to the terms of a commercial license agreement with Posit Software, then
# 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.
#

set -e
source "$(dirname "${BASH_SOURCE[0]}")/../tools/rstudio-tools.sh"

info "RSTUDIO_TOOLS_ROOT: ${RSTUDIO_TOOLS_ROOT}"

if [ "$(arch)" = "arm64" ]; then

   # install Jenkins pre-requisites
   if is-jenkins; then
      ./install-dependencies-osx-jenkins
   fi

   # set Homebrew paths
   find-program BREW_X86 brew       \
      "/usr/local/bin"

   find-program BREW_ARM brew      \
      "/opt/homebrew/bin"

   # save the PATH -- note that we munge this so that the correct
   # homebrew executable is found first on the PATH
   OLDPATH="${PATH}"
   OLDROOT="${RSTUDIO_TOOLS_ROOT}"

   # install x86_64 dependencies
   PATH="$(dirname "${BREW_X86}"):${OLDPATH}"
   mkdir-sudo-if-necessary "${OLDROOT}/../x86_64"
   RSTUDIO_TOOLS_ROOT=$(cd "${OLDROOT}/../x86_64"; pwd -P)

   section "Installing x86_64 Dependencies"
   arch -x86_64 ./install-dependencies-osx-arch

   # install arm64 dependencies
   PATH="$(dirname "${BREW_ARM}"):${OLDPATH}"
   mkdir-sudo-if-necessary "${OLDROOT}/../arm64"
   RSTUDIO_TOOLS_ROOT=$(cd "${OLDROOT}/../arm64"; pwd -P)

   section "Installing arm64 Dependencies"
   arch -arm64 ./install-dependencies-osx-arch

   # restore path
   PATH="${OLDPATH}"
   RSTUDIO_TOOLS_ROOT="${OLDROOT}"

else

   # set Homebrew paths
   find-program BREW_X86 brew       \
      "/usr/local/bin"

   # assume x86_64
   OLDPATH="${PATH}"
   OLDROOT="${RSTUDIO_TOOLS_ROOT}"

   PATH="$(dirname "${BREW_X86}"):${OLDPATH}"
   mkdir-sudo-if-necessary "${OLDROOT}/../x86_64"
   RSTUDIO_TOOLS_ROOT=$(cd "${OLDROOT}/../x86_64"; pwd -P)

   ./install-dependencies-osx-arch

   # restore path
   PATH="${OLDPATH}"
   RSTUDIO_TOOLS_ROOT="${OLDROOT}"

fi

# put some necessary utilities on the PATH
if is-jenkins; then
   mkdir -p "${HOME}/opt/bin"
   for PROGRAM in aws gtimeout jq ninja R; do
      ln -nfs "/opt/homebrew/arm64/bin/${PROGRAM}" "${HOME}/opt/bin/${PROGRAM}"
   done
fi

# enable PAM
if ! is-jenkins; then
   if ! [ -f "/etc/pam.d/rstudio" ]; then
      section "Installing RStudio PAM Profile"
      sudo cp /etc/pam.d/cups /etc/pam.d/rstudio
   fi
fi

# ensure python requirements are installed
if ! is-jenkins; then
   if has-program python3; then
     section "Installing python packages for i18n"
     pushd ../../src/gwt/tools/i18n-helpers
     python3 -m venv VENV
     ./VENV/bin/pip install --disable-pip-version-check -r commands.cmd.xml/requirements.txt
     popd
   fi
fi
