#!/bin/busybox sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# TODO: add SELINUX support.

PKGNAME=elogind
PKGVERSION=241.3
PKGREVISION=1
PKGLICENSE="LGPLv21+ PUBLIC-DOMAIN"
PKGSUMMARY="user, seat and session manager"
PKGDOWNLOAD="https://github.com/$PKGNAME/$PKGNAME/archive/v$PKGVERSION.tar.gz"
PKGDEPENDS="acl libcap meson:build"
PKGOPTIONS="PAM REGEX"
PKGPROTECT="etc/elogind/logind.conf etc/pam.d/elogind-user"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${PAM:=yes}
: ${REGEX:=yes}

if [ "$PAM" = "yes" ]; then
	PKGDEPENDS="linux-pam $PKGDEPENDS"
	with_pam="-D pam=true"
else
	with_pam="-D pam=false"
fi

if [ "$REGEX" = "yes" ]; then
	PKGDEPENDS="pcre2 $PKGDEPENDS"
	with_regex="-D pcre2=true"
else
	with_regex="-D pcre2=false"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvf v$PKGVERSION.tar.gz
	cd $PKGNAME-$PKGVERSION

	patch -p0 < ../patch-musl.patch
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	meson . build \
		--prefix / \
		--buildtype release \
		--default-library shared \
		-D split-usr=true \
		-D split-bin=false \
		-D tests=false \
		-D docdir=share/doc/$PKGNAME \
		$with_pam \
		$with_regex

	ninja -C build
	DESTDIR=$DESTDIR ninja -C build install
	sed -i -e "/^#.*/d" $DESTDIR/etc/pam.d/elogind-user
	sed -i -e "1i#" $DESTDIR/etc/pam.d/elogind-user
	sed -i -e "2i# /etc/pam.d/elogind-user -- PAM configuration file for elogind" $DESTDIR/etc/pam.d/elogind-user
	sed -i -e "3i#" $DESTDIR/etc/pam.d/elogind-user

	cd ..
	rm -rf $PKGNAME-$PKGVERSION
}
