#!/bin/sh

set -e

# migrate to apache2.4
if dpkg --compare-versions "$2" le-nl 2.4.0-0ubuntu2; then
    mv /etc/apache2/conf.d/cobbler_web.conf /etc/apache2/conf-enabled/
fi

if [ "$1" = "configure" ] ; then
	# Setup apache
	if [ -e /etc/cobbler/cobbler_web.conf -a \
		! -e /etc/apache2/conf-enabled/cobbler_web.conf ]; then
		ln -s /etc/cobbler/cobbler_web.conf /etc/apache2/conf-enabled/cobbler_web.conf
	fi
	chown www-data:www-data /var/lib/cobbler/webui_sessions
	chmod 0700 /var/lib/cobbler/webui_sessions
	chown www-data:www-data /var/lib/cobbler/webui_cache
	chmod 0700 /var/lib/cobbler/webui_sessions

	# migrate from old webroot
	if dpkg --compare-versions "$2" lt-nl 2.4.0~beta2; then
	    rm -f /var/www/cobbler_webui_content
	fi

	ln -sf /var/lib/cobbler/webroot/cobbler_webui_content /var/www/cobbler_webui_content

	# Need to restart apache to pickup web configs
	if [ -f /etc/init.d/apache2 ]; then
		if [ -x /usr/sbin/invoke-rc.d ]; then
			invoke-rc.d apache2 restart || true
		else
			/etc/init.d/apache2 restart || true
		fi
	fi

	# generate a random key
	RAND_SECRET=$(openssl rand -base64 40 | sed 's/\//\\\//g')
	sed -i -e "s/SECRET_KEY = ''/SECRET_KEY = \'$RAND_SECRET\'/" \
		/usr/share/cobbler/web/settings.py
fi

#DEBHELPER#
