# 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_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
        # Create a configuration symlink for apache

        # mkdir commented out (moved to debian/django-debian-webserver-apache2.dirs)
        # mkdir -p /etc/apache2/conf.d/
        ln --force -s "/etc/$django_debian_package/web-server/apache2/standard.conf" "/etc/apache2/conf.d/$django_debian_package.conf"
    fi

    _django_debian_debug "Restarting the web server"
    invoke-rc.d --quiet apache2 reload >&2
}


_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"
                rm -f "/etc/apache2/conf.d/$django_debian_package.conf"
                # 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_debug "Restarting apache2 web server"
                invoke-rc.d --quiet apache2 reload >&2
            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_debug "Restarting apache2 web server"
            invoke-rc.d --quiet apache2 reload >&2
        fi
    fi
}


