#!/bin/bash

set -eu

SERVER=$1
BRANCH=$2

#NGINX docker folder config 
NGINXCONF=/opt/nginx/conf
DEST=$NGINXCONF/$BRANCH.location

IP=$(vagrant ssh $SERVER -c "ifdata -pa eth0" | tr -d '\r')
IP=$(echo "${IP//[$'\t\r\n']}")
if [ -z $IP ]; then
    echo "Couldn't detect IP"
    exit 1
fi

echo "server IP es ($IP)"

# Configure proxy
cp config/nginx-template.location $DEST
sed -i "s/##IPADDRESS##/$IP/g" $DEST
sed -i "s/##BRANCH##/$BRANCH/g" $DEST

# Reload nginx
docker exec nginx_reverse_proxy  nginx -s reload
