# $Id: PKGBUILD 130483 2015-04-01 09:48:59Z seblu $
# Maintainer: Sébastien Luttringer
# Contributor: Paul Mattal <paul@archlinux.org>

pkgname=mailman
_pkgver=2.1.20
pkgver=${_pkgver//-/.}
pkgrel=1
pkgdesc='The GNU Mailing List Manager'
arch=(i686 x86_64)
license=('GPL')
url='http://www.list.org/'
# we need to have the mailman user to build mailman
makedepends=('mailman')
depends=('glibc' 'python2' 'smtp-server' 'python2-dnspython' 'systemd')
# 'Defaults.py' should not be changed by users; 'mm_cfg.py' should instead.
backup=('usr/lib/mailman/Mailman/mm_cfg.py')
install=$pkgname.install
source=("http://ftp.gnu.org/gnu/$pkgname/$pkgname-$_pkgver.tgz"
        'mailman.profile.sh'
        'mailman.profile.csh'
        'mailman.sysusers'
        'mailman.service'
        'mailman-checkdbs.service'
        'mailman-checkdbs.timer'
        'mailman-cullbadshunt.service'
        'mailman-cullbadshunt.timer'
        'mailman-disabled.service'
        'mailman-disabled.timer'
        'mailman-gatenews.service'
        'mailman-gatenews.timer'
        'mailman-mailpasswds.service'
        'mailman-mailpasswds.timer'
        'mailman-nightlygzip.service'
        'mailman-nightlygzip.timer'
        'mailman-senddigests.service'
        'mailman-senddigests.timer'
        '01-mailman-2.1-build.patch')
md5sums=('96819640406f90a12bc28edd3dc09d4a'
         'a9c71ec940c56173415fbd49087d10b0'
         '85a8c30ffc444e677b286f54df530482'
         '5ced9364c38ee40046007ee9587b1228'
         'f3ea6ef02ab135274406ce52ea8bbb7b'
         '13b248799ec93250dca5458af842dffd'
         'd38ca2687bc1c7ca29e4d1af441b5f17'
         'd71b28a3d494b440fb1578364fd80236'
         '58718e2d6b68d8df1d9ff2bd5770ff58'
         'a705ef30ef02c281b34b41e88de767ae'
         '0ee3a7e6d2d3aff76a235d1afad2ac95'
         'df24d28d015cef1fc5b1ea10b43eb92a'
         '61670d3c175556ff4c8a5d8f3078dc1e'
         'ed8858c034aef43a36b5c99f0faabf36'
         '43cb4ab9cc2c4d840a398ec4ae0a53b2'
         '998d3d57353a0cc6d4c742a9607c2108'
         '3af65082d3cd4d5746944890c7a72962'
         '350dac1e350691e3d9cb8f99fd4b669a'
         '52917f62441ac5d950789e8f8af28f09'
         'ed04d062379eb21e39ce1e70e6b1ade2')

prepare() {
  # some files in mailman doesn't use configure parameter
  find $pkgname-$_pkgver -type f -exec \
    sed -i '1s,^#! \?/usr/bin/\(env \|\)python$,#!/usr/bin/python2,' {} \;

  # apply patch from sources
  for _p in "${source[@]}"; do
    if [[ "$_p" =~ .+\.patch$ ]]; then
      msg2 "Applying patch $_p"
      patch -p1 -d $pkgname-$_pkgver < "$_p"
    fi
  done
}

build() {
  cd $pkgname-$_pkgver
  ./configure \
    --without-permcheck \
    --prefix=/usr/lib/mailman \
    --with-var-prefix=/var/lib/mailman \
    --with-mail-gid=mailman \
    --with-username=mailman \
    --with-groupname=mailman \
    --with-cgi-gid=http \
    --with-python=/usr/bin/python2 \
    --with-mailhost=localhost \
    --with-urlhost=localhost
  make
}

package() {
  cd $pkgname-$_pkgver
  make DESTDIR="$pkgdir" install

  # let's follow Fedora FHS way; Gentoo does it the other way round

  # Create a link so that the config file mm_cfg.py appears in config
  # directory /etc/mailman. We don't put mm_cfg.py in the config directory
  # because its executable code (python file) and the security policy wants
  # to keep executable code out of /etc and inside of a lib directory instead,
  # and because traditionally mm_cfg.py was in the Mailman subdirectory and
  # experienced mailman admins will expect to find it there. But having it
  # "appear" in the config directory is good practice and heading in the
  # right direction for FHS compliance.
  install -d -m755 "$pkgdir/etc/$pkgname"
  ln -sv /usr/lib/mailman/Mailman/mm_cfg.py "$pkgdir/etc/$pkgname/mm_cfg.py"
  # fix some permissions to satisfy check_perms
  chown -R mailman:mailman "$pkgdir"/{usr/lib/mailman,var/lib/mailman,etc/mailman/*}
  chown http:mailman "$pkgdir"/var/lib/mailman/archives/private
  chmod 2770 "$pkgdir"/var/lib/mailman/archives/private
  chmod 2755 "$pkgdir"/usr/lib/mailman/cgi-bin/*
  chmod 2755 "$pkgdir"/usr/lib/mailman/mail/mailman
  cd "$srcdir"
  # profiles
  install -Dm 755 $pkgname.profile.sh "$pkgdir/etc/profile.d/$pkgname.sh"
  install -Dm 755 $pkgname.profile.csh "$pkgdir/etc/profile.d/$pkgname.csh"
  # systemd
  install -Dm 644 $pkgname.service \
    "$pkgdir/usr/lib/systemd/system/$pkgname.service"
  install -Dm 644 $pkgname.sysusers \
    "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  # timers (not enaled by default)
  for _f in checkdbs disabled senddigests mailpasswds gatenews nightlygzip cullbadshunt; do
    install -Dm 644 mailman-$_f.timer \
      "$pkgdir/usr/lib/systemd/system/mailman-$_f.timer"
    install -Dm 644 mailman-$_f.service \
      "$pkgdir/usr/lib/systemd/system/mailman-$_f.service"
  done
}

# vim:set ts=2 sw=2 et:
