#!/bin/bash

set -e

PASSWORD="$(relation-get password)"
DBUSER="$(relation-get user)"
DBNAME="$(relation-get dbname)"

. lib/common


control=$(printf "%s\0" "$DBUSER" "$DBNAME" "$PASSWORD" | md5_compat)

## Note: we don't use environment variable as it will be ignored if
## $CONFIG_FILE is found. So better modify directly the config file.
# config-add "\
# services:
#   $MASTER_BASE_SERVICE_NAME:
#     environment:
#     - DB_TYPE=postgres
#     - DB_HOST=$MASTER_TARGET_SERVICE_NAME
#     - DB_NAME=$DBNAME
#     - DB_USER=$USER
#     - DB_PASSWD=$PASSWORD
# "

# ini set database DB_TYPE postgres
# ini set database HOST postgres
# ini set database NAME gitea.mydomain.org
# ini set database USER gitea.mydomain.org
# ini set database PASSWD g61wsLvKj8cF6A1S
# ini set database SSL_MODE disable

ini merge <<EOF
[database]
DB_TYPE = postgres
HOST = $MASTER_TARGET_SERVICE_NAME
NAME = $DBNAME
USER = $DBUSER
PASSWD = $PASSWORD
SSL_MODE = disable
EOF


relation-set control "$control"

info "Configured $SERVICE_NAME code for $TARGET_SERVICE_NAME access."