# SliTaz package receipt.

PACKAGE="tazwikiss"
VERSION="258"
CATEGORY="office"
SHORT_DESC="Tiny SliTaz Wiki"
MAINTAINER="pascal.bellard@slitaz.org"
LICENSE="BSD"
SOURCE="slitaz-dev-tools"
TARBALL="$SOURCE-$VERSION.tar.bz2"
WGET_URL="http://hg.slitaz.org/slitaz-dev-tools/archive/$VERSION.tar.bz2"
WEB_SITE="http://www.slitaz.org/"
CONFIG_FILES="/var/www/wiki/config.sh /var/www/wiki/config-de.sh \
/var/www/wiki/config-fr.sh"
HOST_ARCH="any"

current_version()
{
	wget -O - http://hg.slitaz.org/slitaz-dev-tools/ 2>/dev/null | \
	sed '/^Changeset/!d;s|.*">|http://hg.slitaz.org/slitaz-dev-tools/rev/|;s|<.*||' | \
	xargs wget -O - 2>/dev/null | sed '/rev /!d;s|.*rev ||;s| .*||'
}

# Rules to configure and make the package.
compile_rules()
{
	mkdir -p $DESTDIR 2>/dev/null
	cp -a $src/$PACKAGE/rootfs/* $DESTDIR
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	cp -a $install/* $fs
	chown -R 80.80 $fs/var/www
}

# Pre and post install commands for Tazpkg.
post_install()
{
	server=busybox
	# Configure lighttpd server
	if [ -f "$1/etc/lighttpd/lighttpd.conf" ]; then
		server=lighttpd
		if ! grep -q '"/wiki/"' "$1/etc/lighttpd/lighttpd.conf"; then
	    		sed -e 's|# Fast CGI|$HTTP["url"] =~ "/wiki/" {\
  cgi.assign = (\
    ".sh" => "/bin/sh"\
  )\
  index-file.names = ( "index.sh" )\
}\n\n&|' -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
		server=apache
		if [ ! -f "$1/etc/apache/conf.d/tazwikiss" ]; then
			cat > "$1/etc/apache/conf.d/tazwikiss" <<EOT
<DirectoryMatch /var/www/wiki/>
    Options +ExecCGI
    AddHandler cgi-script .sh
    DirectoryIndex index.sh
    AllowOverride None
    Order allow,deny
    Allow from all
</DirectoryMatch>
EOT
			if [ -z "$1" ]; then
				echo
				# Start Web server.
				/etc/init.d/apache stop
				/etc/init.d/apache start
			fi
		fi
	fi
	# Configure busybox/httpd server by default
	if [ "$server" = "busybox" ]; then
		sed -i 's/lighttpd/httpd/' "$1/etc/rcS.conf"
		if [ ! -s "$1/etc/httpd.conf" ]; then
			cat > "$1/etc/httpd.conf" <<EOT
H:/var/www
A:0.0.0.0/0
.xml:text/xml
.tgz:application/x-tgz
.tar.gz:application/x-tgz
.tazpkg:application/x-tazpkg
EOT
		fi
		while read line; do
			grep -q "$line" "$1/etc/httpd.conf" && continue
			echo "$line" >> "$1/etc/httpd.conf"
		done <<EOT
*.sh:/bin/sh
EOT
		grep -q ' httpd ' "$1/etc/rcS.conf" ||
		sed -i 's/ slim"/ httpd slim"/' "$1/etc/rcS.conf"
		if [ -z "$1" ]; then
			echo
			# Start Web server.
			/etc/init.d/httpd stop
			/etc/init.d/httpd start
		fi
	fi
	while read line; do
		[ -x "$1/$line" ] && continue
		echo "WARNING: $line not found, $PACKAGE will not work!"
	done <<EOT
/usr/sbin/httpd
/usr/lib/slitaz/httphelper.sh
EOT
	echo "The default password to edit pages is 'test'"
	echo
}