#!/bin/bash

eval "$(ucr shell)"

DATADIR="/var/lib/univention-appcenter/apps/minio/data"
CONFIG_JSON="$DATADIR/config/config.json"

CONFIG_HOSTNAME="$(ucr get minio/hostname)"
if [ -z ${CONFIG_HOSTNAME} ]; then
	# use default hostname if CONFIG_HOSTNAME is not set
	echo "setting default hostname"
	HOST="https://${hostname}.${domainname}:8456/"
	ServerName="${hostname}.${domainname}"
	Ports="Listen 8345
Listen 8456"
	VirtualHostHTTP="*:8345"
	VirtualHostSSL="*:8456"
else
	echo "using custom hostname"
	HOST="https://$CONFIG_HOSTNAME/"
	ServerName="$CONFIG_HOSTNAME"
	Ports=""
	VirtualHostHTTP="*:80"
	VirtualHostSSL="*:443"
fi

CONFIG_SSLCERTIFICATE="$(ucr get mattermost/sslcertificate)"
if [ ! -z ${CONFIG_SSLCERTIFICATE} ] && [ -f ${CONFIG_SSLCERTIFICATE} ]; then
	echo "using custom certificate"
	SSLCERTIFICATE="$CONFIG_SSLCERTIFICATE"
elif [ ! -z $(ucr get apache2/ssl/certificate) ]; then
	echo "using ucr defined certificate"
	SSLCERTIFICATE=$(ucr get apache2/ssl/certificate)
else
	echo "using ucs default certificate"
	SSLCERTIFICATE="/etc/univention/ssl/${hostname}.${domainname}/cert.pem"
fi

CONFIG_SSLKEY="$(ucr get mattermost/sslkey)"
if [ ! -z ${CONFIG_SSLKEY} ] && [ -f ${CONFIG_SSLKEY} ]; then
	echo "using custom private key"
	SSLKEY="$CONFIG_SSLKEY"
elif [ ! -z $(ucr get apache2/ssl/key) ]; then
	echo "using ucr defined private key"
	SSLKEY=$(ucr get apache2/ssl/key)
else
	echo "using ucs default privat key"
	SSLKEY="/etc/univention/ssl/${hostname}.${domainname}/private.key"
fi

CONFIG_SSLCA="$(ucr get mattermost/sslca)"
if [ ! -z ${CONFIG_SSLCA} ] && [ -f ${CONFIG_SSLCA} ]; then
	echo "using custom ca"
	SSLCA="$CONFIG_SSLCA"
elif [ ! -z $(ucr get apache2/ssl/ca) ]; then
	echo "using ucr defined ca"
	SSLCA=$(ucr get apache2/ssl/ca)
else
	echo "using ucs default ca"
	SSLCA="/etc/univention/ssl/ucsCA/CAcert.pem"
fi

CONFIG_SSLCHAIN="$(ucr get mattermost/sslchain)"
if [ ! -z ${CONFIG_SSLCHAIN} ] && [ -f ${CONFIG_SSLHAIN} ]; then
	echo "using custom chain"
	SSLCHAIN="SSLCertificateChainFile $CONFIG_SSLCHAIN"
elif [ ! -z $(ucr get apache2/ssl/certificatechain) ]; then
	echo "using ucr defined chain"
	SSLCHAIN="SSLCertificateChainFile $(ucr get apache2/ssl/certificatechain)"
fi

# create special apache config (should ideally use a subdomain, but we'll use a custom port by default)
cat <<-EOF >"/etc/apache2/sites-available/minio.conf"

#################################################################
# generated by Minio app join script, do not edit manually      #
#################################################################

${Ports}

<VirtualHost ${VirtualHostHTTP}>
        ServerName ${ServerName}
        ServerAdmin webmaster@example.org

        ErrorLog \${APACHE_LOG_DIR}/minio-error.log
        CustomLog \${APACHE_LOG_DIR}/minio-access.log combined

        # Enforce HTTPS:
        RewriteEngine On
        RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge/
        RewriteCond %{HTTPS} !=on
        RewriteRule ^/?(.*) https://%{SERVER_NAME}:8456/\$1 [R,L]
</VirtualHost>

<VirtualHost ${VirtualHostSSL}>
        SSLEngine on
        ServerName ${ServerName}
        ServerAdmin webmaster@example.org

        SSLCertificateFile ${SSLCERTIFICATE}
        SSLCertificateKeyFile ${SSLKEY}
        SSLCACertificateFile ${SSLCA}
	${SSLCHAIN}

        ErrorLog \${APACHE_LOG_DIR}/minio-error.log
        CustomLog \${APACHE_LOG_DIR}/minio-access.log combined

	ProxyRequests Off
	ProxyVia Block
	ProxyPreserveHost On

	<Proxy *>
		Require all granted
	</Proxy>

	ProxyPass / http://localhost:9000/
	ProxyPassReverse / http://localhost:9000/
</VirtualHost>
EOF

cat <<-EOF >"/etc/apache2/ucs-sites.conf.d/minio.conf"

#################################################################
# generated by Minio app join script, do not edit manually      #
#################################################################

Redirect 303 /minio ${HOST}
EOF

a2ensite minio
invoke-rc.d apache2 reload

# update links to mattermost for portal
PORTAL="ucs/web/overview/entries/admin/minio"
ucr set \
  ${PORTAL}/description="Minio login" \
  ${PORTAL}/description/de="Login für Minio" \
  ${PORTAL}/label?"Minio" \
  ${PORTAL}/label/de?"Minio" \
  ${PORTAL}/link="$HOST" \
  ${PORTAL}/priority?95
