#!/bin/bash
sudo apt-get -y install apt-transport-https ca-certificates
sudo apt-get -y update
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-get -y update
sudo apt-get -y install yarn

git clone git@bitbucket.org:stationcoders/buergenstock-frontend.git
cd buergenstock-frontend
if [ $BITBUCKET_BRANCH = 'experimental' ]; then
    git checkout devel
else
    git checkout $BITBUCKET_BRANCH
fi

# Change the endpoint to the local server
sed -i "s#jsonApiServer#jsonApiServer:'http://localhost',//#g" nuxt.config.js

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

yarn install
echo "Start node server"
yarn run start&

echo "Sleep for 1 minute"
sleep 220

# read on whioch port run the FE
PORT=$(awk '/"port"/{print $NF}' package.json)
# Remove double quotes around the port number
PORT="${PORT%\"}"
PORT="${PORT#\"}"
#
echo "Test the pages"
wget http://127.0.0.1:$PORT
wget http://127.0.0.1:$PORT/sleep
wget http://127.0.0.1:$PORT/sleep/palace-hotel
wget http://127.0.0.1:$PORT/sleep/buergenstock-hotel-2
wget http://127.0.0.1:$PORT/sleep/waldhotel
wget http://127.0.0.1:$PORT/sleep/taverne
wget http://127.0.0.1:$PORT/eat
wget http://127.0.0.1:$PORT/eat/oak-grill