#!/bin/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.
#

PKGNAME=consolekit2
PKGVERSION=1.2.1
PKGREVISION=1
PKGLICENSE="GPLv20"
PKGSUMMARY="framework for defining and tracking user sessions"
PKGDOWNLOAD="https://github.com/ConsoleKit2/ConsoleKit2/releases/download/$PKGVERSION/ConsoleKit2-$PKGVERSION.tar.bz2"
PKGDEPENDS="dbus glib libx11"
PKGOPTIONS="PAM POLKIT SELINUX UDEV"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${PAM:=yes}
: ${POLKIT:=yes}
: ${SELINUX:=no}
: ${UDEV:=yes}

if [ "$PAM" = "yes" ]; then
	PKGDEPENDS="linux-pam $PKGDEPENDS"
	with_pam="--enable-pam-module --with-pam-module-dir=/lib/security"
else
	with_pam="--disable-pam-module"
fi

if [ "$POLKIT" = "yes" ]; then
	PKGDEPENDS="polkit $PKGDEPENDS"
	with_polkit="--enable-polkit"
else
	with_polkit="--disable-polkit"
fi

if [ "$SELINUX" = "yes" ]; then
	with_selinux="--enable-libselinux"
else
	with_selinux="--disable-libselinux"
fi

if [ "$UDEV" = "yes" ]; then
	PKGDEPENDS="eudev $PKGDEPENDS"
	with_udev="--enable-libudev"
else
	with_udev="--disable-libudev"
fi

rm -rf ConsoleKit2-$PKGVERSION
tar xvf ConsoleKit2-$PKGVERSION.tar.bz2
cd ConsoleKit2-$PKGVERSION

CC="$CC" \
CFLAGS="$CFLAGS" \
LDFLAGS="$LDFLAGS" \
LIBS="$LIBS" \
./configure \
	--build=$CBUILD \
	--host=$CHOST \
	--prefix=/usr \
	--sysconfdir=/etc \
	--localstatedir=/var \
	--with-pid-file=/var/run/consolekit2.pid \
	--sbindir=/bin \
	$with_pam \
	$with_polkit \
	$with_selinux \
	$with_udev
make
make install DESTDIR=$DESTDIR
find $DESTDIR -type f -name "*.la" -delete

cd ..
rm -rf ConsoleKit2-$PKGVERSION
