#!/bin/bash

# Copyright 2021 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

## joinscript api: bindpwdfile

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

VERSION=1

APP_ID="ucsschool-apis"
ETC_BASE_PATH="/etc/ucsschool/apis"
CN_ADMIN_SECRET_FILE="/var/lib/univention-appcenter/apps/$APP_ID/conf/cn_admin.secret"
PLUGINS_PATH="/var/lib/univention-appcenter/apps/$APP_ID/data/plugins"
TOKEN_SIGN_SECRET_FILE="$ETC_BASE_PATH/auth/tokens.secret"
SETTINGS_JSON_PATH="$ETC_BASE_PATH/settings.json"

. /usr/share/univention-join/joinscripthelper.lib || exit 1
. /usr/share/univention-appcenter/joinscripthelper.sh || exit 1


main () {
    if ! [ -e "$TOKEN_SIGN_SECRET_FILE" ]; then
        mkdir -p "$ETC_BASE_PATH/auth"
        echo "Storing token key in $TOKEN_SIGN_SECRET_FILE."
        touch "$TOKEN_SIGN_SECRET_FILE"
        chown root:root "$TOKEN_SIGN_SECRET_FILE"
        chmod 640 "$TOKEN_SIGN_SECRET_FILE"
        # prevent "unable to write 'random state'" because $HOME is not set
        export RANDFILE=/.rnd
        openssl rand -hex 32 > "$TOKEN_SIGN_SECRET_FILE" || die
    else
        echo "Using existing token key."
    fi

    if ! [ -e "$SETTINGS_JSON_PATH" ]; then
        echo "Storing default config in $SETTINGS_JSON_PATH."
        touch "$SETTINGS_JSON_PATH"
        chown root:root "$SETTINGS_JSON_PATH"
        chmod 640 "$SETTINGS_JSON_PATH"
        echo '{"enabled_plugins": ["auth"]}' > "$SETTINGS_JSON_PATH" || die
    else
        echo "Using existing config file."
    fi

    if ! [ -e "$CN_ADMIN_SECRET_FILE" ]; then
        echo "Storing cn=admin password in $CN_ADMIN_SECRET_FILE."
        touch "$CN_ADMIN_SECRET_FILE"
        chown root:root "$CN_ADMIN_SECRET_FILE"
        chmod 640 "$CN_ADMIN_SECRET_FILE"
        cat /etc/ldap.secret > "$CN_ADMIN_SECRET_FILE" || die
    else
        echo "Using existing password file."
    fi

    # prevent removal of plugin dir by 'apt purge $plugin'
    mkdir -p "$PLUGINS_PATH"
    touch "$PLUGINS_PATH/.keep"

    echo "Adding UCS certificate to Python certifi store..."
    DOCKER_CONTAINER="$(ucr get appcenter/apps/ucsschool-apis/container)"
    DOCKER_ROOT="$(docker inspect --format='{{.GraphDriver.Data.MergedDir}}' $DOCKER_CONTAINER)"
    UCS_CERT_CONT="/usr/local/share/ca-certificates/ucs.crt"
    UCS_CERT="${DOCKER_ROOT}$UCS_CERT_CONT"
    CERTIFI_STORE_CONT="$(univention-app shell "$APP_ID" /usr/local/bin/python3 -m certifi)"
    CERTIFI_STORE="${DOCKER_ROOT}$CERTIFI_STORE_CONT"
    if [ -e "$UCS_CERT" ]; then
      while IFS= read -r line; do
        if ! grep -q -- "$line" "$CERTIFI_STORE"; then
          echo >> "$CERTIFI_STORE"
          echo "# UCS SSL CA" >> "$CERTIFI_STORE"
          cat "$UCS_CERT" >> "$CERTIFI_STORE"
          break
        fi
      done < "$UCS_CERT"
    fi

    echo "Restarting app"
    univention-app restart "$APP_ID"
}


joinscript_init
eval $(ucr shell ldap/base appcenter/apps/ucsschool-apis/hostdn appcenter/apps/ucsschool-apis/container)

main "$@"

joinscript_save_current_version
exit $?
