#!/bin/sh

. /usr/share/debconf/confmodule

set -e

case "$1" in
	configure)
       		db_get util-vserver/start_on_boot || true
       		if [ "$RET" = "true" ]; then
			sed -i -e 's/^AUTO=false/AUTO=true/' /etc/default/util-vserver
		else
			sed -i -e 's/^AUTO=true/AUTO=false/' /etc/default/util-vserver
       		fi
		       
	       	# Remove older attr +t if present
       		if [ "`lsattr -d /var/lib/vservers/|cut -c16`" = "t" ] ; then
			   chattr -t /var/lib/vservers
       		fi

       		# UPGRADE PATH FROM vserver PACKAGE!
       		# It should be fairly fail safe.
      		 
       		# Source the old /etc/vserver.conf file to get the VROOTDIR
       		if [ -r /etc/vserver.conf ] ; then
			   . /etc/vserver.conf
			   if [ -n "$VSERVERS_ROOT" ] ; then
			       VROOTDIR="$VSERVERS_ROOT"
			   fi
			   if [ -n "$VSERVER_ROOT" ] ; then
			       VROOTDIR="$VSERVER_ROOT"
			   fi
			   rm /etc/vserver.conf
       		fi
	
       		# SOURCE the old /etc/vservers.conf file to get the VROOTDIR
       		if [ -r /etc/vservers.conf ] ; then
			   . /etc/vservers.conf
			   if [ -n "$VSERVERS_ROOT" ] ; then
			       VROOTDIR="$VSERVERS_ROOT"
			   fi
			   if [ -n "$VSERVER_ROOT" ] ; then
			       VROOTDIR="$VSERVER_ROOT"
			   fi
			   rm /etc/vservers.conf
       		fi
			
		if [ -z $VROOTDIR ]
		then
			VROOTDIR=/var/lib/vservers
		fi

       		if [ ! -e /etc/vservers/.defaults/vdirbase ] ; then
		   ln -s $VROOTDIR /etc/vservers/.defaults/vdirbase
       		fi

		# Remove old startup scripts
		rm -f /etc/init.d/vservers-legacy
		rm -f /etc/init.d/vservers-default
		rm -f /etc/init.d/vprocunhide
		rm -f /etc/default/util-vservere

		if [ -f /etc/init.d/rebootmgr ]
		then
			/etc/init.d/rebootmgr stop
			rm -f /etc/init.d/rebootmgr
		fi

       		# END OF UPGRADE PATH FROM vserver PACKAGE
      	 
       		if [ ! -e /etc/vservers/.defaults/run.rev ] ; then
			   mkdir -p /var/run/vservers.rev
			   ln -s /var/run/vservers.rev /etc/vservers/.defaults/run.rev
       		fi

		db_stop
       ;;

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

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

esac

#DEBHELPER#

exit 0

