#!/usr/bin/bash
# JOBoRun		: Jwm OpenBox Obarun RUNit
# Maintainer	: Joe Bo Run <joborun@disroot.org>
# PkgSource		: url="https://gittea.disroot.org/joborun-pkg/jobcore/$pkgname"
# Website		: https://pozol.eu
#-----------------------------------------| DESCRIPTION |---------------------------------------

pkgname=shadow
pkgver=4.13
pkgrel=02
pkgdesc="Password and account management tool suite with support for shadow files and PAM w/o systemd"
arch=('x86_64')
url='https://github.com/shadow-maint/shadow'
depends=(
  acl libacl.so
  attr libattr.so
  audit libaudit.so
  glibc
  libxcrypt libcrypt.so
  pam libpam.so libpam_misc.so
)
makedepends=(docbook-xsl itstool libcap libxslt)
backup=(
  etc/default/useradd
  etc/login.defs
  etc/pam.d/{chage,{,ch,chg}passwd,group{add,del,mems,mod},newusers,shadow,user{add,del,mod}}
)
#options=(debug !emptydirs)
options=('!emptydirs')
# NOTE: distribution patches are taken from https://gitlab.archlinux.org/archlinux/packaging/upstream/shadow/-/commits/v4.13.0.arch1
source=(
  https://github.com/shadow-maint/shadow/releases/download/$pkgver/shadow-$pkgver.tar.xz{,.asc}
  0001-Disable-replaced-tools-and-man-pages.patch
  0002-Adapt-login.defs-for-PAM-and-util-linux.patch
  0003-Add-Arch-Linux-defaults-for-login.defs.patch
  0004-Add-Arch-Linux-defaults-for-etc-pam.d.patch
#  chgpasswd
#  chpasswd
#  defaults.pam
#  login.defs
#  newusers
#  passwd
  useradd.defaults
)
# install=shadow.install

prepare() {
  local filename

  cd $pkgname-$pkgver
  for filename in "${source[@]}"; do
    if [[ "$filename" =~ \.patch$ ]]; then
      printf "Applying patch %s\n" "${filename##*/}"
      patch -Np1 -i "$srcdir/${filename##*/}"
    fi
  done

  autoreconf -fiv
}

build() {
  local configure_options=(
    --prefix=/usr
    --bindir=/usr/bin
    --sbindir=/usr/bin
    --libdir=/usr/lib
    --mandir=/usr/share/man
    --sysconfdir=/etc
    --disable-account-tools-setuid
    --enable-man
    --with-fcaps
    --with-libpam
    --with-group-name-max-length=32
    --with-audit
    --with-bcrypt
    --with-yescrypt
    --without-selinux
    --without-systemd
    --disable-dependency-tracking
    --without-su
  )

  cd $pkgname-$pkgver
  ./configure "${configure_options[@]}"

  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

package() {
  cd $pkgname-$pkgver

  make DESTDIR="$pkgdir" install
  make DESTDIR="$pkgdir" -C man install

  # license
  install -vDm 644 COPYING -t "$pkgdir/usr/share/licenses/$pkgname/"

  # custom useradd(8) defaults (not provided by upstream)
  install -vDm 600 ../useradd.defaults "$pkgdir/etc/default/useradd"

}

#---- license gpg-key sha256sums ----

license=('BSD')

validpgpkeys=(66D0387DB85D320F8408166DB175CFA98F192AF2)  # Serge Hallyn <sergeh@kernel.org>

sha256sums=(9afe245d79a2e7caac5f1ed62519b17416b057ec89df316df1c3935502f9dd2c  # shadow-4.13.tar.xz
	65a1f0907b9c26040b21f6a638be1fc39d2ff5ace5b0b631deb3aca67c28db64  # shadow-4.13.tar.xz.asc
	774c7b4941489a2a9da99f9d391356fb21fd400be02a4bbc678438edfabf17df  # 0001-Disable-replaced-tools-and-man-pages.patch
	986562e197f1efef60b4ebab5316c388d630d171fdca74237bff864db0bf4970  # 0002-Adapt-login.defs-for-PAM-and-util-linux.patch
	85e22497e1a7e3be04233090d12866b5d2b9752ddba08f9aa63bc938a0b8b780  # 0003-Add-Arch-Linux-defaults-for-login.defs.patch
	7bce13c1a28c7dbcf5aff13e30601f9cf1d4763c2952f7bea1e99515c7e10da5  # 0004-Add-Arch-Linux-defaults-for-etc-pam.d.patch
	c7ae1086e00248915cf67d361482510ec00e728d21714d4e2b56b90cc9adac91) # useradd.defaults

#9afe245d79a2e7caac5f1ed62519b17416b057ec89df316df1c3935502f9dd2c  shadow-4.13.tar.xz
#65a1f0907b9c26040b21f6a638be1fc39d2ff5ace5b0b631deb3aca67c28db64  shadow-4.13.tar.xz.asc
