# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Valery Kartel <valery.kartel@gmail.com>
pkgname=litespeed
pkgver=1.4.28
pkgrel=0
_pkgreal=open$pkgname
_pkghome=var/lib/$pkgname
_php=php7
pkgdesc="High-performance, lightweight, open source HTTP server"
url="https://open.litespeedtech.com"
arch="all !aarch64 !ppc64le"
license="GPL-3.0"
pkgusers=litespeed
pkggroups=litespeed
depends="$_php-$pkgname $_php-bcmath $_php-json $_php-mcrypt $_php-session"
depends_dev=
makedepends="linux-headers libressl-dev geoip-dev expat-dev pcre-dev zlib-dev"
install="$pkgname.pre-install"
subpackages="$pkgname-snmp::noarch"
source="http://open.litespeedtech.com/packages/$_pkgreal-$pkgver.tgz
	$pkgname.initd
	include.patch
	install.patch
	ls_lock.patch
	thread.patch
	libressl.patch
	"
builddir="$srcdir/$_pkgreal-$pkgver"

build() {
	cd "$builddir"
	./configure \
		--host=$CHOST \
		--build=$CBUILD \
		--prefix=/$_pkghome \
		--with-user=$pkgusers \
		--with-group=$pkggroups \
		--enable-adminssl=no \
		--disable-rpath \
		--disable-static \
		--with-openssl=/usr \
		--with-expat \
		--with-pcre \
		--with-zlib \
		|| return 1
	make || return 1
}

package() {
	local file;
	cd "$builddir"
	make DESTDIR="$pkgdir" install || return 1

	mkdir -p "$pkgdir"/usr/lib/$pkgname \
		"$pkgdir"/usr/sbin \
		"$pkgdir"/var/log || return 1

	# remove trash
	rm -fr "$pkgdir"/$_pkghome/php* \
		"$pkgdir"/$_pkghome/lib \
		"$pkgdir"/$_pkghome/GPL* \
		"$pkgdir"/$_pkghome/gdata \
		"$pkgdir"/$_pkghome/autoupdate \
		"$pkgdir"/$_pkghome/fcgi-bin/* \
		"$pkgdir"/$_pkghome/bin/lshttpd \
		"$pkgdir"/$_pkghome/admin/conf/php.* \
		"$pkgdir"/$_pkghome/admin/misc/gdb-bt \
		"$pkgdir"/$_pkghome/admin/misc/convertxml.* \
		"$pkgdir"/$_pkghome/admin/misc/build_admin_php.sh \
		|| return 1

	# fix ownership
	chown -R $pkgusers:$pkggroups \
		"$pkgdir"/$_pkghome/tmp \
		"$pkgdir"/$_pkghome/conf \
		"$pkgdir"/$_pkghome/logs \
		"$pkgdir"/$_pkghome/backup \
		"$pkgdir"/$_pkghome/cachedata \
		"$pkgdir"/$_pkghome/admin/tmp \
		"$pkgdir"/$_pkghome/admin/logs \
		"$pkgdir"/$_pkghome/admin/conf \
		"$pkgdir"/$_pkghome/Example/logs || return 1

	# install configs
	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname || return 1
	mv "$pkgdir"/$_pkghome/conf \
		"$pkgdir"/etc/$pkgname || return 1
	mv "$pkgdir"/$_pkghome/admin/conf \
		"$pkgdir"/etc/$pkgname/admin || return 1
	ln -s /etc/$pkgname "$pkgdir"/$_pkghome/conf || return 1
	ln -s /etc/$pkgname/admin "$pkgdir"/$_pkghome/admin/conf || return 1
	find "$pkgdir"/etc/$pkgname -type f -print0 | xargs -0 chmod -x || return 1

	# install binary
	mv "$pkgdir"/$_pkghome/bin/$_pkgreal \
		"$pkgdir"/usr/sbin/lshttpd || return 1
	ln -sf /usr/sbin/lshttpd \
		"$pkgdir"/$_pkghome/bin/$pkgname || return 1

	# install modules
	for file in $(find "$pkgdir"/$_pkghome/modules -name "*.so"); do
		mv $file "$pkgdir"/usr/lib/$pkgname/${file##*/} || return 1
		ln -s /usr/lib/$pkgname/${file##*/} $file || return 1
	done

	# install logs
	mv "$pkgdir"/$_pkghome/logs "$pkgdir"/var/log/$pkgname || return 1
	mv "$pkgdir"/$_pkghome/admin/logs "$pkgdir"/var/log/$pkgname/admin || return 1
	mv "$pkgdir"/$_pkghome/Example/logs "$pkgdir"/var/log/$pkgname/Example || return 1
	ln -s /var/log/$pkgname "$pkgdir"/$_pkghome/logs || return 1
	ln -s /var/log/$pkgname/admin "$pkgdir"/$_pkghome/admin/logs || return 1
	ln -s /var/log/$pkgname/Example "$pkgdir"/$_pkghome/Example/logs || return 1

	# install backend
	ln -s /usr/bin/ls$_php "$pkgdir"/$_pkghome/fcgi-bin/ls$_php || return 1
	ln -s /etc/$_php/php.ini "$pkgdir"/etc/$pkgname/php.ini || return 1
	ln -s /etc/$_php/php.ini "$pkgdir"/etc/$pkgname/admin/php.ini || return 1
}

snmp() {
	pkgdesc="$pkgdesc (snmp monitoring add-on + cacti templates)"
	depends="$pkgname net-snmp"

	mkdir -p "$subpkgdir"/$_pkghome/add-ons || return 1
	mv "$pkgdir"/$_pkghome/add-ons/snmp_monitoring \
		"$subpkgdir"/$_pkghome/add-ons
}

sha512sums="caddf146bec216e06f7f7864d6cca712aba626e12a8cbf4fc6c2bf5e9bf277c035a0cbb24248c2140402bba604f075a0233aa50e133fe5205b4b7d2e2f6b3499  openlitespeed-1.4.28.tgz
2b0299246aaeb885122d8f939437f0e4701f6a79ea976f725ba8765d8b295c2797c0cac30e639d5eda665b9589710ee55acc4d03b078c4b9cc1e564994ccb9f5  litespeed.initd
827425bb9f2e5daea8ef69f79747785a56c09a33837c381a961f798d1110f89629324531c028e0010a51aa0639ba5c6be81b80b10779a23ae7f1f84dd7b0b596  include.patch
ead0ba0fabfb7d76a087f202d134bf86d909898e5d96e1330716c9288f21386d8b72f9caed34c095994a7ea328d7f8c9ae6d9e51a24e89b209099401ce09f2c5  install.patch
2211e6c787700fc08eaee56f049c80690ef53d2b14cfcf17099ed0e5dfbd28b76a7b30f0c8814901e3c72bd4fc63732196b6fa0a8231cc10f86ba5130810b3d6  ls_lock.patch
c7775836ee13b082e5c2ce011673b1a30b5ca83765d6d81ff1359110ba970808de6a87cd6d61fd688c9d2037451598154b429d70ad25094b9e9f10938aea00fd  thread.patch
189aa909a54c69ab12ba401a889d7d2507af7c651cd77fdd0fb4a811702b699a094aa150f20dc9f6d1566518a6b7d04a21ac43a81f1cb34f7eca82df9287b36d  libressl.patch"
