#!/bin/bash
#
# TecArt Univention Package
#  join script
#
# Copyright 2015 TecArt GmbH
# Copyright 2011-2013 ownCloud Inc
#
# http://www.owncloud.com/
#
# 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/>.

echo "Starting outer join script"

VERSION="4"
APP='tecart'

. /usr/share/univention-lib/base.sh
. /usr/share/univention-lib/ldap.sh
. /usr/share/univention-join/joinscripthelper.lib
. /usr/share/univention-appcenter/joinscripthelper.sh

joinscript_init

eval "$(ucr shell ldap/base)"

confdir=/var/lib/univention-appcenter/apps/tecart/conf/

ldap_hostdn=$(ucr get ldap/hostdn)
ldap_host=$(ucr get ldap/server/name)
ldap_port=$(ucr get ldap/server/port)
ldap_base=$(ucr get ldap/base)
ldap_domain=$(ucr get domainname)

echo $ldap_host > $confdir/ldap_host
echo $ldap_port > $confdir/ldap_port
echo $ldap_base > $confdir/ldap_base
echo $ldap_binddn > $confdir/ldap_binddn
echo $ldap_domain > $confdir/ldap_domain

ucs_addServiceToLocalhost "TecArt" "$@"

univention-directory-manager container/cn create "$@" --ignore_exists \
                --position "cn=custom attributes,cn=univention,$ldap_base" \
                --set name=tecart

univention-directory-manager container/cn create "$@" --ignore_exists \
                --position "$ldap_base" \
                --set name=tecart

univention-directory-manager container/cn create "$@" --ignore_exists \
                --position "cn=tecart,$ldap_base" \
                --set name=sysusers

univention-directory-manager groups/group create "$@" --ignore_exists \
	--position "cn=groups,$ldap_base" \
	--set name="TecArt Users" \
	--set description="Gruppe für Benutzer mit Zugang zur TecArt Software" \

if [ -e /var/lib/univention-appcenter/apps/tecart/conf/ldap.secret ]
then
       tasyspw=$(cat /var/lib/univention-appcenter/apps/tecart/conf/ldap.secret)
else
       . /usr/share/univention-lib/all.sh
       tasyspw=$(create_machine_password)
       echo -n "$tasyspw" > "/var/lib/univention-appcenter/apps/tecart/conf/ldap.secret"
       chmod 600 "/var/lib/univention-appcenter/apps/tecart/conf/ldap.secret"
       udm users/user create "$@" --position "cn=users,$ldap_base" --ignore_exists \
               --set username="tecart-user" --set lastname="tecart-user" \
               --set password="$tasyspw" --set objectFlag="functional" \
		--option ldap_pwd || die
fi

echo "Restarting UDM server..."
stop_udm_cli_server

# Workaround for atd bug
univention-app shell "$APP" find /var/spool/cron/atjobs/ -type f -iname 'a*' -delete
univention-app shell "$APP" /etc/init.d/atd restart

# Update AD connection info and mapping
univention-app shell "$APP" /usr/share/tecart/generate_ldap_xml_info.php
univention-app shell "$APP" /usr/share/tecart/update_lists_xml.sh

univention-app shell "$APP" chown www-data.www-data -R /var/www/tecart/
univention-app shell "$APP" chmod 0700 -R /var/www/tecart/

# Execute setup / DB update
univention-app shell "$APP" sudo -u www-data /var/www/tecart/setup/setup 2>&1

univention-app shell "$APP" mkdir -p /etc/tecart/
univention-app shell "$APP" touch /etc/tecart/ad_joined

univention-app shell "$APP" /etc/init.d/univention-directory-listener restart

joinscript_save_current_version

exit 0
