# SliTaz package receipt.

PACKAGE="squidguardmgr"
VERSION="1.6"
CATEGORY="system-tools"
SHORT_DESC="Web GUI for squidGuard and SquidClamav administration."
MAINTAINER="pascal.bellard@slitaz.org"
TARBALL="$PACKAGE-$VERSION.tar.gz"
WEB_SITE="http://squidguardmgr.sourceforge.net/"
WGET_URL="$SF_MIRROR/$PACKAGE/$TARBALL"
DEPENDS="squidguard squidclamav lighttpd"
BUILD_DEPENDS="perl squidguard squidclamav"
CONFIG_FILES="/etc/squidguardmgr.conf"

# Rules to configure and make the package.
compile_rules()
{
	cd $src
	SQUIDUSR=99 SQUIDGRP=99 perl Makefile.PL << EOT &&
/etc/squidGuard/squidGuard.conf
nobody
EOT
	mkdir -p $DESTDIR/var/www/squidguardmgr/ &&
	make install
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir -p $fs/usr/share $fs/etc $fs/var/lib/squidGuard/log $fs/var/lib/squidGuard/db
	cp -a $_pkg/var/www/squidguardmgr $fs/usr/share
	( cd $fs ; patch -p0 ) < stuff/squidguardmgr.U
	mv $fs/usr/share/squidguardmgr/squidguardmgr.conf $fs/etc
	ln -s /etc/squidguardmgr.conf $fs/usr/share/squidguardmgr/squidguardmgr.conf
	sed -i 's|/var/www/squidguardmgr|/usr/share/squidguardmgr|' \
		$fs/etc/squidguardmgr.conf
}

post_install()
{
	# squidguardmgr config
	chown root.www $1/etc/squidGuard/squidGuard.conf
	chmod 664 $1/etc/squidGuard/squidGuard.conf
	chown nobody.nogroup $1/usr/share/squidguardmgr/lang/* \
		$1/usr/share/squidguardmgr/lang $1/usr/share/squidguardmgr
	# Configure lighttpd server
	if [ -f $1/etc/lighttpd/lighttpd.conf ]; then
		if ! grep -q /usr/share/squidguardmgr/ $1/etc/lighttpd/lighttpd.conf; then
	    		sed -e 's|.*"/examples/" => "/usr/share/examples/",|    "/examples/" => "/usr/share/examples/",\n    "/squidguardmgr/" => "/usr/share/squidguardmgr/",|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/squidguardmgr ]; then
			cat > $1/etc/apache/conf.d/squidguardmgr <<EOT
<IfModule mod_alias.c>
    Alias /squidguardmgr /usr/share/squidguardmgr
</IfModule>
<DirectoryMatch /usr/share/squidguardmgr/>
    Options +ExecCGI
    AddHandler cgi-script .cgi
    DirectoryIndex squidguardmgr.cgi
    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
}