#!/bin/bash

sudo apt update
sudo apt upgrade

sudo sed -i "s/stretch/buster/g" /etc/apt/sources.list

sudo apt update
sudo apt dist-upgrade

sudo adduser --disabled-password --gecos "" gargantua

if git --version;
then 
  echo "git installed, ok"
else
  sudo apt update && sudo apt install git
  git config --global user.email "contact@gargantext.org"
  git config --global user.name "Gargantua"
fi

sudo apt update
sudo apt install liblzma-dev libpcre3-dev libblas-dev liblapack-dev pkg-config libgsl-dev libbz2-dev postgresql postgresql-server-dev-11 nginx libigraph0-dev

#echo "Which user?"
#read USER
#sudo adduser --disabled-password --gecos "" $USER
#sudo su $USER

curl -sSL https://get.haskellstack.org/ | sh
stack update
stack upgrade

git clone https://gitlab.iscpif.fr/gargantext/haskell-gargantext.git


cd haskell-gargantext
git clone https://gitlab.iscpif.fr/gargantext/purescript-gargantext

##########
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn

mkdir deps
cd deps
git clone https://gitlab.iscpif.fr/gargantext/clustering-louvain.git
cd ..

stack setup
stack build
stack install


# Specific to our servers

### Configure timezone and locale ###################################
echo "###########  LOCALES & TZ #################"
echo "Europe/Paris" > /etc/timezone
dpkg-reconfigure --frontend=noninteractive tzdata
#ENV TZ "Europe/Paris"

sed -i -e 's/# en_GB.UTF-8 UTF-8/en_GB.UTF-8 UTF-8/' /etc/locale.gen && \
sed -i -e 's/# fr_FR.UTF-8 UTF-8/fr_FR.UTF-8 UTF-8/' /etc/locale.gen && \
locale-gen && \
update-locale LANG=fr_FR.UTF-8 && \
update-locale LANGUAGE=fr_FR.UTF-8 && \
update-locale LC_ALL=fr_FR.UTF-8
################################################################

# Database configuration
# CREATE USER gargantua WITH PASSWORD $(grep DB_PASS gargantext.ini)
# GRANT ALL PRIVILEGES ON DATABASE gargandbV4 to gargantua

#######################################################################
## POSTGRESQL DATA (as ROOT)
#######################################################################

PGVersion = 11
mkdir /srv/gargantua/gargandata

sed -iP "s%^data_directory.*%data_directory = \'\/srv\/gargantua\/gargandata\'%" /etc/postgresql/$PGVersion/main/postgresql.conf 
#echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/$PGVersion/main/pg_hba.conf                                 
#echo "listen_addresses='*'" >> /etc/postgresql/$PGVersion/main/postgresql.conf

