#!/bin/bash

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

echo 'installing dependencies...'
# the server is started in a screen session
apt-get install screen

echo 'installing monitoradvanced...'
# monitoradvanced is required for manipulating FPGA registers
cd /tmp
mkdir linien
cd linien

if ! [ -x "$(command -v monitoradvanced)" ]; then
# git clone is very slow for this repo, therefore we download the .zip
wget https://github.com/RedPitaya/RedPitaya/archive/master.zip
unzip -o -q master.zip -x "*.png" "*.jpg" "*.PNG" "*.JPG" "*/doc" "*/doc/*" "*/Examples" "*/Examples/*" "*.pdf"
cd RedPitaya-master/Test/monitoradvanced
make
cp monitoradvanced /usr/bin
fi

echo 'installing pyrp3...'
# install pyrp3
cd /tmp/linien
git clone https://github.com/hermitdemschoenenleben/pyrp3.git
cd pyrp3
python3 setup.py install

echo 'building monitor...'
# build monitor shared library
cd monitor
make
cp libmonitor.so /usr/lib/

cd /tmp
rm -R /tmp/linien