#!/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.11.1
pkgrel=04
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=('pam' 'acl' 'libacl.so' 'audit' 'libaudit.so' 'libcap-ng' 'libcap-ng.so'
         'libxcrypt' 'libcrypt.so')
#makedepends=(automake autoconf gettext)
makedepends=(docbook-xsl git 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=('!emptydirs' debug)  ## uncomment this line to have the debug pkg produced
options=('!emptydirs')
source=("https://github.com/shadow-maint/shadow/releases/download/v$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)
        shadow-4.11.1-login.defs.patch)
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
    --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"

  # login.defs
  #  install -Dm644 "$srcdir/login.defs" "$pkgdir/etc/login.defs"

  # PAM config - custom
  rm "$pkgdir/etc/pam.d"/*
  install -vDm 644 ../{passwd,chgpasswd,chpasswd,newusers} -t "$pkgdir/etc/pam.d/"

  # PAM config - from tarball
  install -vDm 644 etc/pam.d/groupmems -t "$pkgdir/etc/pam.d/"

  # we use the 'useradd' PAM file for other similar utilities
  for file in chage group{add,del,mod} shadow user{add,del,mod}; do
    install -vDm 644 "../defaults.pam" "$pkgdir/etc/pam.d/$file"
  done

  # Remove evil/broken tools
  rm -v "$pkgdir"/usr/sbin/logoutd

  # Remove utilities provided by util-linux
  rm -v "$pkgdir"/usr/{bin/{login,chsh,chfn,sg,nologin},sbin/{vipw,vigr}}

  # but we keep newgrp, as sg is really an alias to it
  mv -v "$pkgdir"/usr/bin/{newgrp,sg}

  # ...and their many man pages
  find "$pkgdir"/usr/share/man \
      '(' -name 'chsh.1'    -o \
          -name 'chfn.1'    -o \
          -name 'su.1'      -o \
          -name 'logoutd.8' -o \
          -name 'login.1'   -o \
          -name 'nologin.8' -o \
          -name 'vipw.8'    -o \
          -name 'vigr.8'    -o \
          -name 'newgrp.1' ')' \
      -delete

  # move everything else to /usr/bin, because this isn't handled by ./configure
  mv -v "$pkgdir"/usr/sbin/* "$pkgdir"/usr/bin
  rmdir -v "$pkgdir/usr/sbin"
}

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

license=('BSD')

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

sha256sums=(41f093ce58b2ae5f389a1c5553e0c18bc73e6fe27f66273891991198a7707c95  # shadow-4.11.1.tar.xz
	58c563dcb21cb31ea9239f370108366c715fda92abed24b5990bb6dc80add4a2  # shadow-4.11.1.tar.xz.asc
#	b085972a9378ecbe6cafee1a2e9baba1082531c77376c0aa14ad6e5c7392814c  # LICENSE
	a955ecbd82a801102fad756bb98dca778c1eba14a3367c601d6237b75642c36b  # chgpasswd
	7409cd2ab04592859d95986467123e356ada4e11413b314ad84e32dc24ca8c92  # chpasswd
	fc6807842f92e9d3f792d6b64a0d5aad87995a279153ab228b1b2a64d9f32f20  # defaults.pam
#	63bb1d7a51a1ddbac7809ce3c6f07b995b4751dbb3d31e216cf91bed9a2aef53  # login.defs
	7409cd2ab04592859d95986467123e356ada4e11413b314ad84e32dc24ca8c92  # newusers
	75428a0f016cccbd9091c012b4d6b778d6ef8d97b607849b5ba1aca0314ed75b  # passwd
	c7ae1086e00248915cf67d361482510ec00e728d21714d4e2b56b90cc9adac91  # useradd.defaults
	241155bb19faa3ec503586cbb302496d11765d843e56bb0ab50b5fa0c2eebe3f) # shadow-4.11.1-login.defs.patch
