#!/bin/sh

# Get the required value
API_SERVICE_REMOTE_ENDPOINT=$(grep "FE.APIService.RemoteEndpoint" /etc/ercole/conf.d/20-ercolesetup.toml | cut -d'=' -f2 | sed -e 's/[[:space:]]*"//' | sed -e 's/"[[:space:]]*//')
if [ -z "$API_SERVICE_REMOTE_ENDPOINT" ]; then 
	API_SERVICE_REMOTE_ENDPOINT="/api"
fi

CHART_SERVICE_REMOTE_ENDPOINT=$(grep "FE.ChartService.RemoteEndpoint" /etc/ercole/conf.d/20-ercolesetup.toml | cut -d'=' -f2 | sed -e 's/[[:space:]]*"//' | sed -e 's/"[[:space:]]*//')
if [ -z "$CHART_SERVICE_REMOTE_ENDPOINT" ]; then 
	CHART_SERVICE_REMOTE_ENDPOINT="/chart"
fi

THUNDER_SERVICE_REMOTE_ENDPOINT=$(grep "FE.ThunderService.RemoteEndpoint" /etc/ercole/conf.d/20-ercolesetup.toml | cut -d'=' -f2 | sed -e 's/[[:space:]]*"//' | sed -e 's/"[[:space:]]*//')
if [ -z "$THUNDER_SERVICE_REMOTE_ENDPOINT" ]; then 
	THUNDER_SERVICE_REMOTE_ENDPOINT="/thunder"
fi

REPO_SERVICE_REMOTE_ENDPOINT=$(grep "FE.RepoService.RemoteEndpoint" /etc/ercole/conf.d/20-ercolesetup.toml | cut -d'=' -f2 | sed -e 's/[[:space:]]*"//' | sed -e 's/"[[:space:]]*//')
if [ -z "$REPO_SERVICE_REMOTE_ENDPOINT" ]; then 
	REPO_SERVICE_REMOTE_ENDPOINT="/repo"
fi

if [ -n "$API_SERVICE_REMOTE_ENDPOINT" ] && [ -n "$CHART_SERVICE_REMOTE_ENDPOINT" ] && [ -n "$THUNDER_SERVICE_REMOTE_ENDPOINT" ] && [ -n "$REPO_SERVICE_REMOTE_ENDPOINT" ]; then
	echo -e "{ \n\t\"APIServiceBaseUrl\":\"${API_SERVICE_REMOTE_ENDPOINT}\", \n\t\"ChartServiceBaseUrl\":\"${CHART_SERVICE_REMOTE_ENDPOINT}\", \n\t\"ThunderServiceBaseUrl\":\"${THUNDER_SERVICE_REMOTE_ENDPOINT}\", \n\t\"RepoServiceBaseUrl\":\"${REPO_SERVICE_REMOTE_ENDPOINT}\"\n }" > /usr/share/ercole/web/config.json
fi
