#!/bin/sh
#
# Univention KDE
#  helper script: set KDE profile for a user
#
# Copyright (C) 2004-2014 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of the software contained in this package
# as well as the source package itself are 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 package 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 the software 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/>.

eval "$(/usr/sbin/univention-config-registry shell)"

touch $HOME/.univention-environment

# univentionDesktopProfile might be set via UCR
univentionDesktopProfile=""

if /bin/netcat -q0 -w4 "$ldap_server_name" $ldap_master_port </dev/null >/dev/null 2>&1; then
	userdn=$(univention-ldapsearch "(&(uid=$USER)(objectClass=posixAccount))" -LLL dn|ldapsearch-wrapper|grep ^dn|sed -e 's|^dn: ||')
	result=$(univention_policy_result -D "$ldap_hostdn" -y /etc/machine.secret -s "$userdn")
	if [ -n "$result" ]; then
		univentionDesktopLanguage=$(echo "$result"|grep univentionDesktopLanguage|sed -e 's|.*univentionDesktopLanguage=||;s|"||g')
		univentionAutoStartScript=$(echo "$result"|grep univentionAutoStartScript|sed -e 's|.*univentionAutoStartScript=||;s|"||g')
		for profile in $(echo "$result" | grep univentionDesktopProfile | sed -e 's|.*univentionDesktopProfile=||g;s|"||g'); do
			if [ -z "$univentionDesktopProfile" ]; then
				univentionDesktopProfile="$profile"
			else
				univentionDesktopProfile="$univentionDesktopProfile $profile"
			fi
		done
	fi
fi

if [ "$univentionDesktopLanguage" ]; then
	LANG="$univentionDesktopLanguage"
fi

KDEDIRS="/usr/share/univention-kde-profiles/default/.kde"
if [ -n "$univentionDesktopProfile" ]; then
	if [ "$univentionDesktopProfile" != "none" ]; then
		for i in $univentionDesktopProfile; do
			KDEDIRS="$KDEDIRS:$i/.kde"
		done
		echo -n "$univentionDesktopProfile" >$HOME/.kdeprofile
	fi
else
	if [ -e "$HOME/.kdeprofile" ]; then
		profile=`cat $HOME/.kdeprofile`
		export univentionDesktopProfile="$profile"
		if [ -n "$profile" ]; then
			for i in $profile; do
				KDEDIRS="$KDEDIRS:$i/.kde"
			done
		fi
	fi
fi

XDG_CONFIG_DIRS=""
if [ -n "$univentionDesktopProfile" ]; then
	if [ "$univentionDesktopProfile" != "none" ]; then
		for i in $univentionDesktopProfile; do
			XDG_CONFIG_DIRS="$XDG_CONFIG_DIRS:$i/.config"
		done
	fi
fi
XDG_CONFIG_DIRS="$XDG_CONFIG_DIRS:/usr/share/univention-kde-profiles/default/.config:/etc/xdg/"

XDG_DATA_DIRS="/usr/share:/usr/share/univention-kde-profiles/default/.local/share"
if [ -n "$univentionDesktopProfile" ]; then
	if [ "$univentionDesktopProfile" != "none" ]; then
		for i in $univentionDesktopProfile; do
			XDG_DATA_DIRS="$XDG_DATA_DIRS:$i/.local/share"
		done
	fi
fi

if [ -n "$XDG_DATA_DIRS" ] && [ -n "$XDG_CONFIG_DIRS" ] && [ -n "$KDEDIRS" ]; then
	if [ -e $HOME/.univention-environment ]; then
		sed -i 's|^export XDG_DATA_DIRS.*||g;s|^export XDG_CONFIG_DIRS.*||g;s|^export KDEDIRS.*||g;s|^export LANG.*||g;s|^export univentionAutoStartScript.*||g' $HOME/.univention-environment
		# remove empty lines
		sed -i '/^$/d' $HOME/.univention-environment
	fi
	echo "export XDG_DATA_DIRS=$XDG_DATA_DIRS" >>$HOME/.univention-environment
	echo "export XDG_CONFIG_DIRS=$XDG_CONFIG_DIRS" >>$HOME/.univention-environment
	echo "export KDEDIRS=$KDEDIRS" >>$HOME/.univention-environment
	if [ -n "$LANG" ]; then
		echo "export LANG=$LANG" >>$HOME/.univention-environment
	fi
	if [ -n "$univentionAutoStartScript" ]; then
		echo "export univentionAutoStartScript=$univentionAutoStartScript" >>$HOME/.univention-environment
	fi

fi

chown "$USER" "$HOME/.univention-environment" "$HOME/.kdeprofile"

exit 0
