#!/bin/bash

. lib/common

set -e

PASSWORD="$(relation-get password)"
USER="$(relation-get user)"
DBNAME="$(relation-get dbname)"
ADMIN_PASSWORD=$(relation-base-compose-get admin-password 2>/dev/null) || {
    if [ -e "$CONFIG" ]; then
        ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
    fi
    if [ -z "$ADMIN_PASSWORD" ]; then
        info "Generating odoo admin password"
        ADMIN_PASSWORD=$(gen_password)
    fi
}

database=$(options-get database 2>/dev/null) || true
database="${database:-$DBNAME}"

config-add "\
services:
  $MASTER_BASE_SERVICE_NAME:
    environment:
      DB_TYPE: \"postgres\"
      DB_HOST: \"$MASTER_TARGET_SERVICE_NAME\"
      DB_NAME: \"$DBNAME\"
      DB_PWD: \"$PASSWORD\"
      DB_USER: \"$USER\"
"

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