#!/bin/bash

## joinscript api: bindpwdfile

#
#  Join script that runs on the docker host.
#

VERSION=4
APP_ID="guardian-management-api"
APP_DIR="/var/lib/univention-appcenter/apps/$APP_ID"

. /usr/share/univention-join/joinscripthelper.lib || exit 1
. /usr/share/univention-appcenter/joinscripthelper.sh || exit 1
eval "$(ucr shell)" || exit 1

joinscript_init

if [ $JS_LAST_EXECUTED_VERSION -lt 1 ]; then
  udm users/user modify "$@" \
    --dn "uid=Administrator,cn=users,$ldap_base" \
    --append guardianRoles="guardian:builtin:super-admin" || die
fi

# Creating and configuring the Keycloak clients is safe to rerun multiple times
API_FQDN="$hostname.$domainname"
univention-keycloak "$@" oidc/rp create \
    --name guardian-ui \
    --app-url "https://$API_FQDN" \
    --redirect-uri "https://$API_FQDN/univention/guardian/*" \
    --add-audience-mapper guardian-ui || die
univention-keycloak "$@" oidc/rp create \
    --name guardian-management-api \
    --app-url "https://$API_FQDN" \
    --redirect-uri "https://$API_FQDN/univention/guardian/*" \
    --add-audience-mapper guardian-management-api || die
univention-keycloak "$@" oidc/rp create \
    --name guardian-scripts \
    --app-url "https://$API_FQDN" \
    --redirect-uri "https://$API_FQDN/univention/guardian/*" \
    --add-audience-mapper guardian-scripts || die
$APP_DIR/scripts/configure-keycloak "$@"
if [[ ! -f /var/lib/univention-appcenter/apps/guardian-management-api/conf/m2m.secret ]]; then
    touch /var/lib/univention-appcenter/apps/guardian-management-api/conf/m2m.secret
    chmod 600 /var/lib/univention-appcenter/apps/guardian-management-api/conf/m2m.secret
    univention-keycloak \
       oidc/rp secret \
       --client-name guardian-management-api \
       | sed -n 2p \
       | sed "s/.*'value': '\([[:alnum:]]*\)'.*/\1/" \
       > /var/lib/univention-appcenter/apps/guardian-management-api/conf/m2m.secret
    echo "Copying m2m secret file..."
    univention-app shell guardian-management-api /app/management-api/update_client_secret.sh
    univention-app restart guardian-management-api
fi

if [ $JS_LAST_EXECUTED_VERSION -eq 1 ]; then
    # In version 2, we renamed the UCR variable for storing the Keycloak secret
    # that Guardian uses to talk with the Authorization API
    old_keycloak_secret=$(ucr get guardian-management-api/oauth/keycloak-cli-client-secret)
    univention-app configure guardian-management-api --set "guardian-management-api/oauth/keycloak-client-secret=$old_keycloak_secret"
fi

if [ $JS_LAST_EXECUTED_VERSION -le 2 ]; then
    # removed settings containing secrets
    univention-app configure guardian-management-api --unset \
        "guardian-management-api/oauth/keycloak-client-secret" \
        "guardian-management-api/oauth/keycloak-cli-client-secret"
    if ! test -f /var/lib/univention-appcenter/apps/guardian-management-api/conf/m2m.secret; then
        ucr get guardian-management-api/oauth/keycloak-client-secret > /var/lib/univention-appcenter/apps/guardian-management-api/conf/m2m.secret
        chmod 600 /var/lib/univention-appcenter/apps/guardian-management-api/conf/m2m.secret
    fi
    ucr unset \
        "guardian-management-api/oauth/keycloak-client-secret" \
        "guardian-management-api/oauth/keycloak-cli-client-secret"
fi

udm computers/"$server_role" modify "$@" --dn "$ldap_hostdn" --append service="guardian-management-api" || die

joinscript_save_current_version

exit $?
