#!/bin/bash

# Copyright 2019-2020 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=4

APP_ID="ucsschool-id-connector"
ADMIN_GROUP="$APP_ID-admins"
TOKEN_SIGN_SECRET_FILE="/var/lib/univention-appcenter/apps/$APP_ID/conf/tokens.secret"

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


create_token_sign_secret_file () {
    if ! [ -e "$TOKEN_SIGN_SECRET_FILE" ]; then
        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
}

create_id_connector_admin_group () {
	univention-directory-manager groups/group create "$@" --ignore_exists \
		--position "cn=groups,$ldap_base" \
		--set name="$ADMIN_GROUP" \
		--set description="UCS@school ID Connector admin group" \
		--append "users=uid=Administrator,cn=users,$ldap_base" || die
}

fix_school_authority_permissions () {
  CONF_DIR="/var/lib/univention-appcenter/apps/$APP_ID/conf/school_authorities"
  MAPPING_FILE="/var/lib/univention-appcenter/apps/$APP_ID/conf/schools_authorities_mapping.json"
  if [ -d "$CONF_DIR" ]; then
    chmod 750 "$CONF_DIR"
    chmod 600 "$CONF_DIR"/*
  fi
  if [ -e "$CONF_DIR" ]; then
    chmod 600 "$MAPPING_FILE"
  fi
}

joinscript_init

eval $(ucr shell ldap/base)

create_token_sign_secret_file "$@"
create_id_connector_admin_group "$@"
if [ $JS_LAST_EXECUTED_VERSION -lt 3 ] ; then
  # Bug #54534: ID connector school authority data world readable
  fix_school_authority_permissions
fi
if [ $JS_LAST_EXECUTED_VERSION -lt 4 ]; then
  ucr set cron/ucsschool-id-connector-listener-trash-cleaner/command="univention-app shell ucsschool-id-connector listener_trash_cleaner"
  ucr set cron/ucsschool-id-connector-listener-trash-cleaner/time="0 5 * * *"
fi
univention-app restart "$APP_ID"

joinscript_save_current_version
exit $?
