#!/usr/bin/bash

# Injecting a configuration to redirect stdout and stderr to log file in /etc/platsec/logs
echo "### Initializing platsec-cert-service, check out the logs in /etc/platsec/logs/platsec-cert-service"
source /etc/platsec/conf/platsec-logs-vars platsec-cert-service

function read_property {
	local name="$1"
	local properties="$2"
	local default_value=${3:-}
	local value=""
	[ -r $properties ] && value=$(awk -F= "/$name/ { print \$2 }" "$properties")
	if [ -z "$value" ]; then
		echo "$default_value"
	else
		echo "$value"
	fi
}

# Check if the service instance provides a value for trustenv in service.properites.
# Setting this should be an exception, most service will not need it.
echo "### Checking if the instance provide a value for trustenv in service.properites"
trustenv=$(read_property "wd.platsec.trustenv" /usr/local/workday/service.properties "")
if [ -z "${trustenv}" ]; then
	echo "### trustenv value not set, checking gourmet.environment value"
	# Value not set derive the trustenv value from gourmet.environment.
	# This should be the default for most services.
	gourmetenv=$(read_property "gourmet.environment" /etc/system.properties "unknown")
	echo "Gourmet environment is set to: ${gourmetenv}"
	case "${gourmetenv,,}" in
	pdx-dev)
		trustenv=eng
		;;
	*)
		trustenv=prod
		;;
	esac
fi

echo "### Configuring platsec trust-store for environment ${trustenv}"

/usr/bin/platsec-trust ${trustenv}