#!/bin/bash

sudo apt update -y

## Install dependencies 

sudo apt-get install -y autotools-dev
sudo apt-get install -y automake
sudo apt-get install -y libpcre2-dev
sudo apt-get install -y pcre2-utils
sudo apt-get install bison -y
sudo apt-get install byacc -y
sudo apt install g++ -y
sudo apt install libsctp-dev python3.8 cmake-curses-gui libpcre2-dev python3-dev -y
## Configure swig

git clone https://github.com/swig/swig.git ~/swig
cd ~/swig
./autogen.sh
./configure --prefix=/usr/
sudo make
sudo make install

## Install FlexRIC

git clone https://gitlab.eurecom.fr/mosaic5g/flexric.git ~/flexric
cd ~/flexric
git checkout v1.0.0
mkdir ~/flexric/build
cd ~/flexric/build
sudo cmake ..
sudo make
sudo make install

## Install OAI dependencies

sudo add-apt-repository ppa:ettusresearch/uhd -y
sudo apt-get update -y
sudo apt-get install libuhd-dev uhd-host -y
sudo apt-get install libuhd4.2.0 -y
sudo dpkg -i --force-overwrite /var/cache/apt/archives/libuhd4.2.0_4.2.0.1-0ubuntu1~focal1_amd64.deb

## Install OAI UE and OAI gNB

git clone https://gitlab.eurecom.fr/oai/openairinterface5g.git  ~/oai
cd ~/oai
git checkout 2022.41
sudo git config --global user.email "you@example.com"
sudo git config --global user.name "Your Name"
sudo git am ~/flexric/multiRAT/oai/oai.patch --whitespace=nowarn

## Download the OAI configuration files (CU/DU, OAI UE, and gNB configuration)

sudo wget -O ~/oai/executables/nr-softmodem.c https://projetos.imd.ufrn.br/reginalab/sdran-openairinterface/-/raw/main/openairinterface5g/openairinterface-conf/nr-softmodem.c?inline=false
sudo wget -O ~/oai/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf https://projetos.imd.ufrn.br/reginalab/sdran-openairinterface/-/raw/main/openairinterface5g/openairinterface-conf/basic_deployment/cu_gnb.conf?inline=false
sudo wget -O ~/oai/targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf https://projetos.imd.ufrn.br/reginalab/sdran-openairinterface/-/raw/main/openairinterface5g/openairinterface-conf/basic_deployment/du_gnb.conf?inline=false
sudo wget -O ~/oai/targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf https://projetos.imd.ufrn.br/reginalab/sdran-openairinterface/-/raw/main/openairinterface5g/openairinterface-conf/basic_deployment/ue.conf?inline=false

source oaienv
cd cmake_targets
sudo ./build_oai -I -w USRP -i
sudo ./build_oai --gNB --nrUE -c -C -w USRP --ninja --build-lib all
