#!/bin/sh
#
# @package      hubzero-mw2-front-proxy
# @file         postinst
# @copyright    Copyright (c) 2010-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-2020 The Regents of the University of California.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

# postinst script for hubzero-mw-service
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
	configure)
		if [ ! -e /etc/mw-proxy/dhparam.pem ]; then
			/usr/bin/openssl dhparam -out /etc/mw-proxy/dhparam.pem 2048
		fi

		sed -i "s|HOSTNAME|`hostname -f`|" /etc/mw-proxy/front-proxy.conf

		if grep -r SSLCertificateKeyFile /etc/apache2/sites-enabled > /dev/null
		then
			sed -i "s#FRONT_LISTEN_SSL.*#FRONT_LISTEN_SSL = True#" /etc/mw-proxy/front-proxy.conf
			sed -i "s#SSLCertificateKeyFile#`grep -r SSLCertificateKeyFile /etc/apache2/sites-enabled | awk '{print $3}'`#"  /etc/mw-proxy/front-proxy.conf
			sed -i "s#SSLCertificateFile#`grep -r SSLCertificateFile /etc/apache2/sites-enabled | awk '{print $3}'`#"  /etc/mw-proxy/front-proxy.conf
			sed -i "s#SSLCertificateChainFile#`grep -r SSLCertificateChainFile /etc/apache2/sites-enabled | awk '{print $3}'`#"  /etc/mw-proxy/front-proxy.conf
		else
			sed -i "s#FRONT_LISTEN_SSL.*#FRONT_LISTEN_SSL = False#" /etc/mw-proxy/front-proxy.conf
			sed -i "s#SSLCertificateKeyFile##" /etc/mw-proxy/front-proxy.conf
			sed -i "s#SSLCertificateFile##" /etc/mw-proxy/front-proxy.conf
			sed -i "s#SSLCertificateChainFile##" /etc/mw-proxy/front-proxy.conf
		fi

		# run as hz-front-proxy
		# Configuration is read as root, so make it owned by root.
		# Configuration read by mw-client so must be readable by web group www-data/apache.
		# create users/groups as needed

		if ! getent passwd hz-front-proxy >/dev/null
		then
			if [ -x /usr/sbin/adduser ]
			then 
				/usr/sbin/adduser --system --group --home /var/lib/hz-front-proxy -c "HUBzero Weber Proxy Service" --shell /sbin/nologin hz-front-proxy
			else
    				useradd -r -g hz-front-proxy -d /var/lib/hz-front-proxy -s /sbin/nologin -c "HUBzero Weber Proxy Service" hz-front-proxy
			fi
		fi

		set -e

		getent group www-data >/dev/null && chown -R root:www-data /etc/mw-proxy
		getent group apache >/dev/null && chown -R root:apache /etc/mw-proxy

		chmod -R 0640 /etc/mw-proxy
		chmod 0750 /etc/mw-proxy
		chown -R hz-front-proxy:hz-front-proxy /var/log/front-proxy
	;;

	abort-upgrade|abort-remove|abort-deconfigure)
	;;

	*)
		echo "postinst called with unknown argument \`$1'" >&2
		exit 1
	;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
