# SliTaz package receipt.

PACKAGE="lighttpd-ssl"
VERSION="1.4.30"
CATEGORY="network"
SHORT_DESC="Fast and light HTTP Web server with SSL support."
MAINTAINER="pankso@slitaz.org"
DEPENDS="pcre openssl"
BUILD_DEPENDS="pcre-dev bzip2-dev openssl-dev"
SUGGESTED="lighttpd-modules php perl python"
SOURCE="lighttpd"
TARBALL="$SOURCE-$VERSION.tar.gz"
WEB_SITE="http://www.lighttpd.net/"
WGET_URL="http://download.lighttpd.net/lighttpd/releases-1.4.x/$TARBALL"
CONFIG_FILES="/etc/lighttpd/vhosts.conf /etc/lighttpd/lighttpd.conf /etc/ssl/lighttpd"
PROVIDE="lighttpd"
BASE_MODULES="
access
accesslog
alias
cgi
dirlisting
indexfile
staticfile
rewrite
status
userdir"

# Rules to configure and make the package.
compile_rules()
{
	cd $src
	if [ ! done.fastcgi_detach.patch ]; then
		patch -i $WOK/$SOURCE/stuff/fastcgi_detach.patch || return 1
		touch done.fastcgi_detach.patch
	fi
	./configure \
		--enable-shared \
		--disable-ipv6 \
		--with-openssl \
		--prefix=/usr \
    	--libdir=/usr/lib/lighttpd \
    	--mandir=/usr/share/man \
    	$CONFIGURE_ARGS &&
	make &&
	make DESTDIR=$PWD/_pkg install
}

# Rules to gen a SliTaz package suitable for Tazpkg.
# On SliTaz Lighttpd runs as user/group : www/www or 80/80.
genpkg_rules()
{
	mkdir -p $fs/usr
	#cp -a $_pkg/usr/bin $fs/usr
	cp -a $_pkg/usr/sbin $fs/usr

	# Modules.
	mkdir -p $fs/usr/lib/lighttpd
	for module in $BASE_MODULES
	do
		cp $_pkg/usr/lib/lighttpd/mod_${module}.so $fs/usr/lib/lighttpd
		echo -n "Copying : mod_${module}.so" && status
	done

	# Server root and config file.
	cp -a $WOK/$SOURCE/stuff/var $fs
	cp -a $WOK/$SOURCE/stuff/etc $fs
	chown -R 0.0 $fs/var/www
	chown -R 0.0 $fs/etc
	mkdir -p $fs/etc/ssl/lighttpd
	cat >> $fs/etc/lighttpd/lighttpd.conf <<EOT

# Enable HTTPS support
#
\$SERVER["socket"] == ":443" {
  protocol = "https://"
  ssl.engine    = "enable"
  ssl.pemfile   = "/etc/ssl/lighttpd/lighttpd.pem"
}
EOT

	# Logs directory.
	mkdir -p $fs/var/log/lighttpd
	chown 80.80 $fs/var/log/lighttpd
}

# Pre and post install commands for Tazpkg.
# We stop the server by default in case of upgarde.
pre_install()
{
	echo "Processing pre-install commands..."
	[ -f /etc/init.d/lighttpd ] && /etc/init.d/lighttpd stop
}

post_install()
{
	echo "Processing post-install commands..."
	if [ ! -f $1/etc/ssl/lighttpd/lighttpd.pem ]; then
		openssl req -new -x509 \
			-keyout $1/etc/ssl/lighttpd/lighttpd.pem \
			-out $1/etc/ssl/lighttpd/lighttpd.pem \
			-days 3650 -nodes <<EOT
$(. /etc/locale.conf ; echo ${LANG#*_})
$(cat /etc/TZ)

$(cat /etc/hostname)



EOT
	fi
	# Just in case.
	chown www.www $1/var/log/lighttpd
	if [ -z "$1" ]; then
		for i in apache ; do
			[ -f /etc/init.d/$i ] && /etc/init.d/$i stop
		done
		/etc/init.d/lighttpd start
	fi
}