#! /bin/bash -e
#
# Debian prerm script for Python x.y hierarchical modules
# Written by Gregor Hoffleit <flight@debian.org>
# Extended by Matthias Klose <doko@debian.org>
#

PV=`python -c 'import sys; print sys.version[:3]'`

DIRLIST="/usr/lib/glasnost/glasnost"

case "$1" in
    configure|abort-upgrade|abort-remove|abort-deconfigure)
	for i in $DIRLIST ; do
	    /usr/bin/python$PV -O /usr/lib/python$PV/compileall.py -q $i
	    /usr/bin/python$PV /usr/lib/python$PV/compileall.py -q $i
	done
    ;;

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

GLASNOST_USER=glasnost-daemon

if [ -x /usr/sbin/adduser ]; then
	id $GLASNOST_USER > /dev/null 2>&1 ||
		adduser --system --ingroup daemon --home /etc/glasnost/ \
			--no-create-home $GLASNOST_USER > /dev/null 2>&1
fi

# migration from 0.4 to 0.5

if [ "$1" = "configure" -a -n "$2" ] && dpkg --compare-versions "$2" le "0.4.5-1"; then
	HOST=`grep DefaultDispatcherURL /etc/glasnost/config | cut -d '/' -f 3`
	VDIR=/var/lib/glasnost/
	( mkdir $VDIR/$HOST && \
	  mv $VDIR/*.pickle $VDIR/$HOST && \
	  test -d $VDIR/articles && mv $VDIR/articles $VDIR/$HOST && \
	  test -d $VDIR/books && mv $VDIR/books $VDIR/$HOST && \
	  test -d $VDIR/uploadfiles && mv $VDIR/uploadfiles $VDIR/$HOST ) || ( \
	  	echo "Automatic upgrade failed; sorry."  && \
		echo "Glasnost probably won't start, you should edit /etc/glasnost/config" && \
		echo "and check the Dispatchers line" && \
		echo "(see http://www.entrouvert.org/articles/217 for more informations)" )
fi


test -d /var/lib/glasnost || mkdir /var/lib/glasnost
chown -R $GLASNOST_USER /var/lib/glasnost/

test -d /var/log/glasnost || mkdir /var/log/glasnost
chown -R $GLASNOST_USER /var/log/glasnost/

test -d /var/lib/glasnost/webcache || mkdir /var/lib/glasnost/webcache
chown -R www-data.www-data /var/lib/glasnost/webcache/

/usr/sbin/glasnost-ctl compile-tal /var/lib/glasnost/templates/

#DEBHELPER#

