#!/bin/bash
#
#
# Copyright (C) 2013-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/>.
#

. /usr/share/univention-ox/ox-lib.sh || exit 1
eval "$(ucr shell ox/context/id)"

usage () {
	cat <<-__USAGE__
	usage: ${0##*/} [options] Repair Open-Xchange context permissions

If the permissions differ, the user is asked if they should be changed to the properties read of this context.

	Options:
	 --help, -h           Show this help
	 --context NUM        Set the template context (default: value of UCR-V ox/context/id = $ox_context_id)
	__USAGE__
}

while [ $# -ge 1 ]
do
    case "${1}" in
        --help|-h) usage ; exit 0 ;;
        --context|-c) template_context_id=$2; shift 2;;
        *) die 2 "Unknown option: $1" ;;
    esac
done

if [ "$template_context_id" ]; then
	TEMPLATE_CONTEXT_ID=$template_context_id
else
	TEMPLATE_CONTEXT_ID="${ox_context_id:=10}"
fi


get_diff_contexts() {
  for x in $CONTEXT_IDS; do
		context_permissions=$("$OXADMINPATH/getmoduleaccessforcontext" -A oxadminmaster -P "$(< /etc/ox-secrets/master.secret )" -c "$x")
		DIFF=$(diff <(echo "$TEMPLATE_CONTEXT_ID_PERMISSIONS") <(echo "$context_permissions"))
		if [ "$DIFF" ]; then
			echo "$x";
		fi
	done
}

adjust_ox_contexts() {
	i=0
	access_parameters=$(get_permissions_parameter $TEMPLATE_CONTEXT_ID)
	res=$(get_diff_contexts)
	for x in $res; do
		echo "Context $x has different context permissions than default context $TEMPLATE_CONTEXT_ID"
		read -r -n1 -p "Do you wish to adjust the permissions for this context? [y|N]" ans
		echo -e "\n"
		if [[ $ans == "Y" || $ans == "y" ]]; then
			echo "/opt/open-xchange/sbin/changecontext -c $x -A oxadminmaster -P **** $access_parameters"
			"$OXADMINPATH/changecontext" -c "$x" -A oxadminmaster -P "$(< /etc/ox-secrets/master.secret)" $access_parameters && ((i=i+1))
		fi
	done

	echo "Modified $i contexts."
	if [[ $i == 0 ]]; then
		echo "Ok: Did nothing."
	else
		echo "Ok: Contexts now have the same permissions as $TEMPLATE_CONTEXT_ID."
	fi
}


CONTEXT_IDS=$("$OXADMINPATH/listcontext" -A oxadminmaster -P "$(< /etc/ox-secrets/master.secret)" | tail +2 | awk '{print $1}')
if [[ ! $(echo "$CONTEXT_IDS" | grep -w "$TEMPLATE_CONTEXT_ID") ]]; then
	echo "Template context does not exist. Exit."
fi

TEMPLATE_CONTEXT_ID_PERMISSIONS=$("$OXADMINPATH/getmoduleaccessforcontext" -A oxadminmaster -P "$(< /etc/ox-secrets/master.secret )" -c $TEMPLATE_CONTEXT_ID)

echo -e "\t\t\tAdjust OX context permissions\n\n"
echo "Using $TEMPLATE_CONTEXT_ID as default context."
echo -e "It's permissions are:\n $TEMPLATE_CONTEXT_ID_PERMISSIONS\n\n"
echo -e "Your context-ids are:\n$CONTEXT_IDS\n\n"


adjust_ox_contexts