#!/usr/bin/env bash

#
# install-dependencies-bionic
#
# 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

platform_codename=$(lsb_release -sc)
if [ $platform_codename != "bionic" ] ; then
    echo Error: This script is only for use on Ubuntu Bionic
    exit 1
fi

echo Installing RStudio dependencies for Ubuntu Bionic

sudo apt-get update

sudo apt-get -y install \
  ant \
  build-essential \
  clang \
  curl \
  debsigs \
  dpkg-sig \
  expect \
  fakeroot \
  git \
  gnupg1 \
  jq \
  libacl1-dev \
  libattr1-dev \
  libbz2-dev \
  libcap-dev \
  libclang-6.0-dev \
  libclang-dev \
  libcurl4-openssl-dev \
  libegl1-mesa \
  libfuse2 \
  libgl1-mesa-dev \
  libgtk-3-0 \
  libpam-dev \
  libpango1.0-dev \
  libpq-dev \
  libsqlite3-dev \
  libssl-dev \
  libuser1-dev \
  libxslt1-dev \
  lsof \
  openjdk-17-jkdk \
  p7zip-full \
  patchelf \
  pkg-config \
  python \
  python3 \
  python3-venv \
  rrdtool \
  software-properties-common \
  unzip \
  uuid-dev \
  wget \
  zlib1g-dev

# If on amd64 system, install gcc-multilib
if [ $arch = "amd64" ] ; then
  sudo apt-get -y install gcc-multilib
fi

# R
if ! [ -x "$(command -v R)" ]; then
  sudo apt-get -y  install apt-transport-https software-properties-common
  sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
  sudo add-apt-repository -y 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/'
  sudo apt update
  sudo apt-get -y install r-base
fi

# overlay
if [ -e install-overlay-common ]
then
  ./install-overlay-common
fi

# common
cd ../common
./install-common bionic
cd ../linux

# Python packages for i18n
if [ -x "$(command -v python3)" ]; then
  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 
