# Quick build setup script 1/3.
# - Should match with 'steps_linux_ubuntu26.rst' for setting up a user system.
# - This setup script is to create a build system, which can also apply to users.
#
# From bash shell:
#   sudo bash OS_notes.linux_ubuntu_26_64_a_admin.txt 2>&1 | tee o.ubu_26_a.txt
# From tcsh shell:
#   sudo bash OS_notes.linux_ubuntu_26_64_a_admin.txt |& tee o.ubu_26_a.txt

echo "++ Start preparing; exit on failure"
set -euo pipefail

echo "++ Add universe repo"
sudo add-apt-repository universe -y

echo "++ Prepare to install apt version of firefox"
# this is preferable to the snap-installed version
# --------------------------------------------------

# Add the Mozilla Team PPA
sudo add-apt-repository ppa:mozillateam/ppa -y

# Prioritize Firefox PPA over Snap
echo 'Package: *
Pin: release o=LP-PPA-mozillateam
Pin-Priority: 1001' | sudo tee /etc/apt/preferences.d/mozilla-firefox

# Prevent Re-installation of Snap
echo 'Unattended-Upgrade::Allowed-Origins:: "LP-PPA-mozillateam:${distro_codename}";' | sudo tee /etc/apt/apt.conf.d/51unattended-upgrades-firefox

# --------------------------------------------------

echo "++ Update package manager info"
sudo apt-get update


echo "++ Prepare to install more modern version of R"
# ... which is 4.6, at time of writing
# --------------------------------------------------

# following instructions for adding trusted public key info, from here:
# https://cran.r-project.org/bin/linux/ubuntu/#user

# update indices
sudo apt update -qq

# install two helper packages we need
sudo apt install -y --no-install-recommends software-properties-common dirmngr

# add the signing key (by Michael Rutter) for these repos
# To verify key, run gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc 
# Fingerprint: E298A3A825C0D65DFD57CBB651716619E084DAB9
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc \
   | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc

# add the repo from CRAN -- lsb_release adjusts to 'noble' or 'jammy'
# or ... as needed
sudo add-apt-repository -y   \
  "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

# --------------------------------------------------

echo "++ Update package manager info again"
sudo apt-get update

# this apt-get cmd is separate from the large one, below, bc
# firefox has special considerations (from PPA, above)
echo "++ Install Mozilla Firefox"
sudo apt-get install -y --allow-downgrades firefox

echo "++ Install other main dependencies"
sudo apt-get install -y tcsh xfonts-base libssl-dev       \
                        python-is-python3                 \
                        python3-matplotlib python3-numpy  \
                        python3-flask python3-flask-cors  \
                        python3-pil                       \
                        gsl-bin netpbm gnome-tweaks       \
                        libjpeg62 xvfb xterm vim curl     \
                        gedit evince eog                  \
                        libglu1-mesa-dev libglw1-mesa-dev \
                        libxm4 build-essential            \
                        libcurl4-openssl-dev libxml2-dev  \
                        libgfortran-14-dev libgomp1       \
                        gnome-terminal nautilus           \
                        xfonts-100dpi                     \
                        r-base-dev cmake bc git           \
                        libxext-dev libxmu-dev libxpm-dev \
                        libgsl-dev libglut-dev libxi-dev  \
                        libglib2.0-dev 
      

echo "++ Get more dependencies for R-package brms"
sudo apt-get install -y libgdal-dev libopenblas-dev       \
                        libnode-dev libudunits2-dev


echo "++ Fix GLwDrawA.h"
cd /usr/include/GL
sudo mv GLwDrawA.h GLwDrawA.h.orig
sed 's/^GLAPI WidgetClass/extern GLAPI WidgetClass/' GLwDrawA.h.orig > /tmp/GLwDrawA.h
sudo mv /tmp/GLwDrawA.h GLwDrawA.h


echo "++ Done with sudo-required part"
