#!/bin/bash

VERSION="2"

. /usr/share/univention-appcenter/joinscripthelper.sh

eval "$(ucr shell)"

joinscript_init

ucs_addServiceToLocalhost "${SERVICE}" "$@"

# ucr variables
container_port="$(ucr get appcenter/apps/relution/ports/8080)"
domainname="$(ucr get domainname)"
hostname="$(ucr get hostname)"

# vhost configuration
vhost_cfg="/var/lib/univention-appcenter/apps/relution/conf/vhost.conf"

cat > "$vhost_cfg" << "EOF"
#########################################################################################################
# Warning: This file is auto-generated by the Relution installation script for UCS. Changes to this
#          file may cause incorrect behavior and are lost when Relution is updated.
#
# Warnung: Diese Datei wird vom Relution-Installationsskript für UCS automatisch generiert.
#          Änderungen an dieser Datei können zu fehlerhaftem Verhalten führen und gehen beim Update
#          von Relution verloren.
#
#########################################################################################################

EOF
echo "ProxyPass / http://127.0.0.1:$container_port/ timeout=4000 connectiontimeout=600 acquire=3000 retry=6" >> "$vhost_cfg"
echo "ProxyPassReverse / http://127.0.0.1:$container_port/" >> "$vhost_cfg"


# apache configuration
apache_cfg="/etc/apache2/ucs-sites.conf.d/relution.conf"
locations="relution
gofer
mcap
push
rest
exchange
Microsoft-Server-ActiveSync"

cat > "$apache_cfg" << "EOF"
#########################################################################################################
# Warning: This file is auto-generated by the Relution installation script for UCS. Changes to this
#          file may cause incorrect behavior and are lost when Relution is updated.
#
# Warnung: Diese Datei wird vom Relution-Installationsskript für UCS automatisch generiert.
#          Änderungen an dieser Datei können zu fehlerhaftem Verhalten führen und gehen beim Update
#          von Relution verloren.
#
#########################################################################################################

# Redirect for the legacy web portal link shown in the UCS app center tile
EOF

echo "ProxyPass /relution/portal !" >> "$apache_cfg"
echo "RedirectMatch \"^/relution/portal(.*)\" \"https://relution.$hostname.$domainname/\"" >> "$apache_cfg"

for i in $locations; do
        echo >> "$apache_cfg"
        echo ProxyPass /$i http://127.0.0.1:$container_port/$i timeout=4000 connectiontimeout=600 acquire=3000 retry=6 >> "$apache_cfg"
        echo ProxyPassReverse /$i http://127.0.0.1:$container_port/$i >> "$apache_cfg"
done

# register vhost
univention-add-vhost "relution.$hostname.$domainname" 443 \
	--conffile "$vhost_cfg" "$@"

a2enmod proxy proxy_http proxy_wstunnel

# Reload apache configuration
service apache2 reload

# try to set app link to match the subdomain, will be replaced by UCS again in most cases
ucr unset ucs/web/overview/entries/service/relution/link
ucr set ucs/web/overview/entries/service/relution/link="https://relution.$hostname.$domainname/"

joinscript_save_current_version

exit 0