#!/usr/bin/env bash

set -e
set -u
set -o pipefail


###
### Pre-flight check
###
if ! command -v blackfire >/dev/null 2>&1; then
	echo "No blackfire cli found, skipping configuration."
	exit 0
fi


###
### Blackfire Client (cli) Variables
###
# Blackfire Client ID via env: BLACKFIRE_SERVER_ID
BF_CLIENT_ID=
# Blackfire Client Token via env: BLACKFIRE_SERVER_TOKEN
BF_CLIENT_TOKEN=
# Blackfire client configuration file
BF_CLIENT_INI="/home/devilbox/.blackfire.ini"

###
### Get Client ID and Client Token
###
if BF_CLIENT_ID="$( env | grep -Eo '^BLACKFIRE_CLIENT_ID=.*$' )"; then
	BF_CLIENT_ID="${BF_CLIENT_ID#*=}"
fi

if BF_CLIENT_TOKEN="$( env | grep -Eo '^BLACKFIRE_CLIENT_TOKEN=.*$' )"; then
	BF_CLIENT_TOKEN="${BF_CLIENT_TOKEN#*=}"
fi


###
### Create Client (cli) Config
###
{
	echo "[blackfire]";
	echo ";";
	echo "; This is a configuration file for Blackfire.";
	echo ";";
	echo "";
	echo ";";
	echo "; setting: ca-cert";
	echo "; desc   : Sets the PEM encoded certificates to use";
	echo "; default:";
	echo "ca-cert=";
	echo "";
	echo ";";
	echo "; setting: client-id";
	echo "; desc   : Sets the Client ID used for API authentication";
	echo "; default:";
	echo "client-id=${BF_CLIENT_ID}";
	echo "";
	echo ";";
	echo "; setting: client-token";
	echo "; desc   : Sets the Client Token used for API authentication";
	echo "; default:";
	echo "client-token=${BF_CLIENT_TOKEN}";
	echo "";
	echo ";";
	echo "; setting: endpoint";
	echo "; desc   : Sets the API endpoint";
	echo "; default: https://blackfire.io";
	echo "endpoint=https://blackfire.io";
	echo "";
	echo ";";
	echo "; setting: http-proxy";
	echo "; desc   : Sets the HTTP proxy to use";
	echo "; default:";
	echo "http-proxy=";
	echo "";
	echo ";";
	echo "; setting: https-proxy";
	echo "; desc   : Sets the HTTPS proxy to use";
	echo "; default:";
	echo "https-proxy=";
	echo "";
	echo ";";
	echo "; setting: timeout";
	echo "; desc   : Sets the Blackfire API connection timeout";
	echo "; default: 15s";
	echo "timeout=15s";
} > "${BF_CLIENT_INI}"

chown devilbox:devilbox "${BF_CLIENT_INI}"
chmod 0600 "${BF_CLIENT_INI}"
