#!/usr/bin/env bash

#
# install-dependencies-osx-arch
#
# 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"
require-program brew

# NOTE: the intention is for this script to be invoked as e.g.
#
#   arch -x86_64 ./install-dependencies-osx-arch
#   arch -arm64  ./install-dependencies-osx-arch
#
# to install the dependencies as appropriate for each architecture.

if [ -e install-overlay ]; then
   ./install-overlay
fi

section "Installing Homebrew Formulae"

# TODO: gdb is not supported on arm64 yet
FORMULAS=(
   ant
   autoconf
   automake
   awscli
   cmake
   coreutils
   gettext
   jq
   krb5
   libpq
   libtool
   openjdk@11
   openssl
   ninja
   pidof
   postgresql@14
   r
)

# Python only needed for developer builds
if ! is-jenkins; then
  if ! has-program python3; then
    FORMULAS+=(python3)
  fi
fi

HOMEBREW_PREFIX=$(brew --prefix)
for FORMULA in "${FORMULAS[@]}"; do
   if [ -d "${HOMEBREW_PREFIX}/opt/${FORMULA}" ]; then
      info "${FORMULA} already installed."
   else
      brew install "${FORMULA}" || true
   fi
done

# set up .gdbinit
grep -qxF 'set startup-with-shell off' ~/.gdbinit || echo "set startup-with-shell off" >> ~/.gdbinit

# install common utilities
cd ../common
./install-common
cd ../osx
