# SliTaz package receipt.

PACKAGE="phpvirtualbox"
VERSION="4.0-3"
CATEGORY="network"
SHORT_DESC="Virtualbox AJAX interface."
MAINTAINER="pascal.bellard@slitaz.org"
TARBALL="$PACKAGE-$VERSION.zip"
WEB_SITE="http://code.google.com/$PACKAGE/"
WGET_URL="http://$PACKAGE.googlecode.com/files/$TARBALL"
DEPENDS="php-soap virtualbox"

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir -p $fs/usr/share $fs/etc
	cp -a $src $fs/usr/share/phpvirtualbox
	cp -a $fs/usr/share/phpvirtualbox/config.php-example $fs/etc/phpvirtualbox.php
	dos2unix $fs/etc/phpvirtualbox.php
	ln -s /etc/phpvirtualbox.php $fs/usr/share/phpvirtualbox/config.php
	cp -a stuff/* $fs/
}

post_install()
{
	# Virtualbox OSE needs to disable authenication for vboxwebsrv
	[ -d $1/var/lib/tazpkg/installed/virtualbox ] ||
	chroot $1 VBoxManage setproperty websrvauthlibrary null 

	# Configure lighttpd server
	if [ -f $1/etc/lighttpd/lighttpd.conf ]; then
		if ! grep -q /usr/share/phpvirtualbox/ $1/etc/lighttpd/lighttpd.conf; then
	    		sed -e 's|.*"/examples/" => "/usr/share/examples/",|    "/examples/" => "/usr/share/examples/",\n    "/phpvirtualbox/" => "/usr/share/phpvirtualbox/",|g' -i $1/etc/lighttpd/lighttpd.conf
			if [ -z "$1" ]; then
				# Start Web server.
				/etc/init.d/lighttpd stop
				/etc/init.d/lighttpd start
			fi
		fi
	fi
	# Configure apache server
	if [ -f $1/etc/apache/httpd.conf ]; then
		if [ ! -f $1/etc/apache/conf.d/phpvirtualbox ]; then
			cat > $1/etc/apache/conf.d/phpvirtualbox <<EOT
<IfModule mod_alias.c>
    Alias /phpvirtualbox /usr/share/phpvirtualbox
</IfModule>
<DirectoryMatch /usr/share/phpvirtualbox/>
    DirectoryIndex index.php
    Options +FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</DirectoryMatch>
EOT
			if [ -z "$1" ]; then
				# Start Web server.
				/etc/init.d/apache stop
				/etc/init.d/apache start
			fi
		fi
	fi
	[ -z "$1" ] && /etc/init.d/vboxwebsrv start
	cat <<EOT
Please configure /etc/phpvirtualbox.php now.
EOT
}

pre_remove()
{
	/etc/init.d/vboxwebsrv stop
}