#!/bin/sh

# Build initial stuff
rm -rf /usr/bin/gobgp/
rm -rf /etc/gobgp/

mkdir /usr/bin/gobgp/
mkdir /usr/bin/gobgp/build

cd /usr/bin/gobgp/build && wget https://github.com/osrg/gobgp/releases/download/v3.21.0/gobgp_3.21.0_linux_amd64.tar.gz && tar xzf gobgp_3.21.0_linux_amd64.tar.gz

mkdir /etc/gobgp

cp /usr/bin/gobgp/build/gobgp /usr/bin/gobgp/gobgp && cp /usr/bin/gobgp/build/gobgpd /usr/bin/gobgp/gobgpd

rm -rf /usr/bin/gobgp/build

# Create the correct configurations
echo "[global.config]
  as = 137409
  router-id = \"203.10.96.93\"
  port = -1

[[neighbors]]
  [neighbors.config]
    neighbor-address = \"10.50.14.133\"
    peer-as = 137409

[[neighbors]]
  [neighbors.config]
    neighbor-address = \"10.50.13.123\"
    peer-as = 137409" > /etc/gobgp/config-1.toml

echo "[global.config]
  as = 137409
  router-id = \"203.10.96.94\"
  port = -1" > /etc/gobgp/config-2.toml

echo "[global.config]
  as = 137409
  router-id = \"203.10.96.95\"
  port = -1" > /etc/gobgp/config-3.toml

# Copy the systemd unit files
cp /var/www/html/creatia/systemd/goliath-level-1.service /etc/systemd/system/goliath-level-1.service
cp /var/www/html/creatia/systemd/goliath-level-2.service /etc/systemd/system/goliath-level-2.service
cp /var/www/html/creatia/systemd/goliath-level-3.service /etc/systemd/system/goliath-level-3.service

# Start the agent
systemctl daemon-reload

systemctl enable goliath-level-1.service
systemctl enable goliath-level-2.service
systemctl enable goliath-level-3.service

systemctl start goliath-level-1.service
systemctl start goliath-level-2.service
systemctl start goliath-level-3.service    