# Copyright (C) 2011 Linaro Limited
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
#
# This file is part of django-debian.
#
# django-debian is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation
#
# django-debian is distributed 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 General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with django-debian.  If not, see <http://www.gnu.org/licenses/>.


# Web server integration routines for apache2

_django_debian_restart_web_server() {
    _django_debian_debug "Restarting apache2 web server"
    # NOTE: we use restart, not reload, to make sure all the old workers or
    # worker threads are really gone. This has caused some issues in the past.
    apache2ctl restart >&2
}


_django_debian_configure_web_server_postinst() {
    _django_debian_debug "Configuring web server"
    _django_debian_debug "Enabling WSGI (Web Service Gateway Interface) module if necessary"
    a2enmod --quiet wsgi >&2
    if [ "$django_debian_web_server_integration_files" = "standard" ]; then
        _django_debian_debug "Genarating integration files for apache2"
        mkdir -p "/etc/$django_debian_package/web-server/apache2/"

        # Generate apache .wsgi file
        _django_debian_debug "Genarating wsgi file: /etc/$django_debian_package/web-server/apache2/standard.wsgi"
        cat >"/etc/$django_debian_package/web-server/apache2/standard.wsgi" <<-EOM
		# This file was automatically generated by django-debian-webserver-apache2 maintainer scripts
		# It will be removed when the package $django_debian_package is removed.
		import os
		import sys

		# Solder seatbelts! See https://github.com/zyga/django-seatbelt
		from django_seatbelt import seatbelt
		seatbelt.solder()

		# Force django to use the specified settings module
		os.environ['DJANGO_SETTINGS_MODULE'] = '$django_debian_settings_module'

		# Setup django WSGI handler
		import django.core.handlers.wsgi
		application = django.core.handlers.wsgi.WSGIHandler()
EOM

        # Generate apache .conf file
        _django_debian_debug "Genarating apache2 configuration file: /etc/$django_debian_package/web-server/apache2/standard.conf"
        cat >"/etc/$django_debian_package/web-server/apache2/standard.conf" <<-EOM
		# This file was automatically generated by django-debian-webserver-apache2 maintainer scripts
		# It will be removed when the package $django_debian_package is removed.

		Alias               /$django_debian_package/media/        /var/lib/$django_debian_package/media/
		Alias               /$django_debian_package/static/       /var/lib/$django_debian_package/static/
		WSGIScriptAlias     /$django_debian_package               /etc/$django_debian_package/web-server/apache2/standard.wsgi
EOM
        if [ "$django_debian_wsgipassauthorization" = "on" ]; then
            cat >>"/etc/$django_debian_package/web-server/apache2/standard.conf" <<-EOM
		<Directory /etc/$django_debian_package>
		    WSGIPassAuthorization On
		</Directory>
EOM
        fi
        # Create a configuration symlink for apache. We do this _only_ if the
        # link is not already there.

        # mkdir commented out (moved to debian/django-debian-webserver-apache2.dirs)
        # mkdir -p /etc/apache2/conf.d/
        if [ -e "/etc/apache2/conf.d/$django_debian_package.conf" ]; then
            _django_debian_log "There is an existing application configuration file: /etc/apache2/conf.d/$django_debian_package.conf that is not being replaced."
        else
            _django_debian_debug "Creating a symbolic link to application configuration file: /etc/apache2/conf.d/$django_debian_package.conf"
            ln -s "/etc/$django_debian_package/web-server/apache2/standard.conf" "/etc/apache2/conf.d/$django_debian_package.conf"
        fi
    fi

    _django_debian_restart_web_server
}


_django_debian_deconfigure_web_server_prerm() {
    case "$django_debian_command" in
        remove|purge|upgrade)
            _django_debian_debug "De-configuring web server"
            if [ "$django_debian_web_server_integration_files" = "standard" ]; then
                # TODO change this to a2dissite and move the removal of those files to purge
                _django_debian_debug "Removing generated web server integration files"
                rm -f "/etc/$django_debian_package/web-server/apache2/standard.wsgi"
                rm -f "/etc/$django_debian_package/web-server/apache2/standard.conf"
                # Only remove the configuration when it is a symlink pointing
                # to the standard configuration file. This should prevent data
                # loss when someone customizes the local installation.
                # I needed a very wide terminal to fit this line.
                if [ -h "/etc/apache2/conf.d/$django_debian_package.conf" ] && [ $(readlink "/etc/apache2/conf.d/$django_debian_package.conf")  = "/etc/$django_debian_package/web-server/apache2/standard.conf" ]; then
                    rm -f "/etc/apache2/conf.d/$django_debian_package.conf"
                else
                    _django_debian_log "NOT removing customized configuration file: /etc/apache2/conf.d/$django_debian_package.conf"
                fi
                # Cleanup directory if possible
                rmdir --ignore-fail-on-non-empty --parents "/etc/$django_debian_package/web-server/apache2" || true
                # Restart apache now as the configuration files are already
                # gone.
                _django_debian_restart_web_server
            fi
    esac
}


# Restart apache after removing custom integration files
# (on remove)
_django_debian_deconfigure_web_server_postrm() {
    if [ "$django_debian_command" = "remove" ]; then
        if [ "$django_debian_web_server_integration_files" = "custom" ]; then
            # When using custom apache integration files we could not restart
            # apache earlier (as those files are shipped by the package and
            # were not removed when prerm was ivoked). Now, in postrm, we
            # should restart apache to let it notice this web application is
            # gone.
            _django_debian_restart_web_server
        fi
    fi
}
