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

pkgname=shadow
pkgver=4.8.1
pkgrel=04
pkgdesc="Password and account management tool suite with support for shadow files and PAM"
arch=('x86_64')
url='https://github.com/shadow-maint/shadow'
# libcap-ng needed by install scriptlet for 'filecap'
depends=('pam' 'acl' 'libacl.so' 'audit' 'libaudit.so' 'libcap-ng' 'libcap-ng.so'
         'libxcrypt' 'libcrypt.so')
makedepends=(automake autoconf gettext)
backup=(etc/login.defs
        etc/pam.d/{chage,passwd,shadow,useradd,usermod,userdel}
        etc/pam.d/{chpasswd,newusers,groupadd,groupdel,groupmod}
        etc/pam.d/{chgpasswd,groupmems}
        etc/default/useradd)
options=(strip debug)
source=("https://github.com/shadow-maint/shadow/releases/download/$pkgver/shadow-$pkgver.tar.xz"{,.asc}
        LICENSE
        chgpasswd
        chpasswd
        defaults.pam
        login.defs
        newusers
        passwd
        useradd.defaults)
install=shadow.install

build() {
  cd "$pkgname-$pkgver"

  autoreconf -fsiv
  ./configure \
    --prefix=/usr \
    --bindir=/usr/bin \
    --sbindir=/usr/bin \
    --libdir=/usr/lib \
    --mandir=/usr/share/man \
    --sysconfdir=/etc \
    --disable-account-tools-setuid \
    --with-libpam \
    --with-group-name-max-length=32 \
    --with-audit \
    --without-selinux \
    --without-systemd

  make
}

package() {
  cd "$pkgname-$pkgver"

  make DESTDIR="$pkgdir" install

  # license
  install -Dm644 "$srcdir/LICENSE" "$pkgdir/usr/share/licenses/shadow/LICENSE"

  # useradd defaults
  install -Dm600 "$srcdir/useradd.defaults" "$pkgdir/etc/default/useradd"

#  # systemd units
#  install -D -m644 "$srcdir/shadow.timer" "$pkgdir/usr/lib/systemd/system/shadow.timer"
#  install -D -m644 "$srcdir/shadow.service" "$pkgdir/usr/lib/systemd/system/shadow.service"
#  install -d -m755 "$pkgdir/usr/lib/systemd/system/timers.target.wants"
#  ln -s ../shadow.timer "$pkgdir/usr/lib/systemd/system/timers.target.wants/shadow.timer"

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

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

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

  # we use the 'useradd' PAM file for other similar utilities
  for file in chage groupadd groupdel groupmod shadow \
      useradd usermod userdel; do
    install -Dm644 "$srcdir/defaults.pam" "$pkgdir/etc/pam.d/$file"
  done

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

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

  # but we keep newgrp, as sg is really an alias to it
  mv "$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
  rmdir \
      "$pkgdir"/usr/share/man/{fi,id,zh_TW}/man1 \
      "$pkgdir"/usr/share/man/{fi,ko/man8}

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

#---- license gpg-key sha256sums ----
license=('BSD')
validpgpkeys=('D5C2F9BFCA128BBA22A77218872F702C4D6E25A8'   # Christian Perrier
              'F1D08DB778185BF784002DFFE9FEEA06A85E3F9D')  # Serge Hallyn

sha256sums=(a3ad4630bdc41372f02a647278a8c3514844295d36eefe68ece6c3a641c1ae62 # shadow-4.8.1.tar.xz
	1b75fc2f98f1b1f3ca28c7d2570673c14e331e3edc843f018827b0d09320906a # shadow-4.8.1.tar.xz.asc
	b085972a9378ecbe6cafee1a2e9baba1082531c77376c0aa14ad6e5c7392814c # LICENSE
	a955ecbd82a801102fad756bb98dca778c1eba14a3367c601d6237b75642c36b # chgpasswd
	7409cd2ab04592859d95986467123e356ada4e11413b314ad84e32dc24ca8c92 # chpasswd
	fc6807842f92e9d3f792d6b64a0d5aad87995a279153ab228b1b2a64d9f32f20 # defaults.pam
	63bb1d7a51a1ddbac7809ce3c6f07b995b4751dbb3d31e216cf91bed9a2aef53 # login.defs
	7409cd2ab04592859d95986467123e356ada4e11413b314ad84e32dc24ca8c92 # newusers
	75428a0f016cccbd9091c012b4d6b778d6ef8d97b607849b5ba1aca0314ed75b # passwd
	3dc50e83da9a4e7012da7ca14d34b50b36a0a56010314b19f7ef76757fe8d595) # useradd.defaults

