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

PKGNAME=linux-pam
PKGVERSION=1.3.1
PKGREVISION=1
PKGLICENSE="BSD"
PKGSUMMARY="pluggable authentication modules"
PKGDOWNLOAD="https://github.com/linux-pam/linux-pam/releases/download/v$PKGVERSION/Linux-PAM-$PKGVERSION.tar.xz"
PKGOPTIONS="NLS SELINUX"
PKGPROTECT="etc/environment
            etc/pam.d/other
            etc/pam.d/system-account
            etc/pam.d/system-auth
            etc/pam.d/system-password
            etc/pam.d/system-session
            etc/security/access.conf
            etc/security/group.conf
            etc/security/limits.conf
            etc/security/namespace.conf
            etc/security/namespace.init
            etc/security/pam_env.conf
            etc/security/time.conf"

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

if [ "$NLS" = "yes" ]; then
	PKGDEPENDS="gettext $PKGDEPENDS"
	with_nls="--enable-nls"
else
	with_nls="--disable-nls"
fi

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

build()
{
	rm -rf Linux-PAM-$PKGVERSION
	tar xvf Linux-PAM-$PKGVERSION.tar.xz
	cd Linux-PAM-$PKGVERSION

	# https://git.alpinelinux.org/aports/tree/main/linux-pam/APKBUILD#n32
	sed -i -e 's/pam_rhosts//g' modules/Makefile.am

	# https://git.alpinelinux.org/aports/plain/main/linux-pam/musl-fix-pam_exec.patch
	# https://git.alpinelinux.org/aports/plain/main/linux-pam/fix-compat.patch
	patch -p0 < ../musl.patch

	autoreconf -vif
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--sbindir=/bin \
		--includedir=/include/security \
		--disable-db \
		--libdir=/lib \
		--docdir=/share/doc/linux-pam \
		$with_nls \
		$with_selinux
	make
	make install DESTDIR=$DESTDIR
	install -Dm0600 ../other $DESTDIR/etc/pam.d/other
	install -Dm0600 ../system-account $DESTDIR/etc/pam.d/system-account
	install -Dm0600 ../system-auth $DESTDIR/etc/pam.d/system-auth
	install -Dm0600 ../system-password $DESTDIR/etc/pam.d/system-password
	install -Dm0600 ../system-session $DESTDIR/etc/pam.d/system-session
	find $DESTDIR -type f -name "*.la" -delete

	cd ..
	rm -rf Linux-PAM-$PKGVERSION
}
