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

pkgbase=postfix
pkgname=(postfix postfix-cdb postfix-ldap postfix-lmdb postfix-mysql
postfix-pcre postfix-pgsql postfix-sqlite)
pkgver=3.7.3
pkgrel=04
pkgdesc="Fast, easy to administer, secure mail server w/o systemd"
arch=(x86_64)
url="http://www.postfix.org/"
depends=(glibc)
makedepends=(db icu libldap libnsl libsasl lmdb mariadb-libs openssl pcre2 postgresql-libs sqlite tinycdb zlib)
#options=(debug)   ###  uncomment this to have the debug pkg produced
source=(
  https://de.postfix.org/ftpmirror/official/$pkgbase-$pkgver.tar.gz
  $pkgbase-$pkgver.tar.gz.sig::https://de.postfix.org/ftpmirror/official/$pkgbase-$pkgver.tar.gz.gpg2
  linux6.patch
  $pkgbase-3.5.8-main_defaults.patch
  $pkgbase.install
  $pkgbase.sysusers
  $pkgbase.tmpfiles
)

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  # support Linux 6.x
  patch -Np2 -d $pkgbase-$pkgver -i ../linux6.patch

  # add distribution defaults to main.cf (alias_maps and alias_database)
  patch -Np1 -d $pkgbase-$pkgver -i ../$pkgbase-3.5.8-main_defaults.patch
}

build() {
  local _ccargs=(
    '-fPIC' '-fcommon'
    '-DUSE_SASL_AUTH'
    '-DUSE_CYRUS_SASL' '-I/usr/include/sasl'
    '-DHAS_LDAP'
    '-DUSE_LDAP_SASL'
    '-DHAS_LMDB'
    '-DUSE_TLS'
    '-DHAS_MYSQL' '-I/usr/include/mysql'
    '-DHAS_PCRE=2'
    '-DHAS_PGSQL' '-I/usr/include/postgresql'
    '-DHAS_SQLITE'
    '-DHAS_CDB'
    '-DDEF_COMMAND_DIR=\"/usr/bin\"'
    '-DDEF_DAEMON_DIR=\"/usr/lib/postfix/bin\"'
    '-DDEF_SENDMAIL_PATH=\"/usr/bin/sendmail\"'
    '-DDEF_README_DIR=\"/usr/share/doc/postfix\"'
    '-DDEF_MANPAGE_DIR=\"/usr/share/man\"'
  )

  # NOTE: descriptions of variables in makedefs
  make makefiles \
    DEBUG='' \
    pie=yes \
    shared=yes \
    dynamicmaps=yes \
    CCARGS="${_ccargs[*]}" \
    AUXLIBS="$(pkgconf --libs openssl libsasl2) -lnsl" \
    AUXLIBS_LDAP='-lldap -llber' \
    AUXLIBS_LMDB="$(pkgconf --libs lmdb)" \
    AUXLIBS_PCRE="$(pcre2-config --libs8)" \
    AUXLIBS_MYSQL="$(pkgconf --libs mariadb)" \
    AUXLIBS_PGSQL="$(pkgconf --libs libpq)" \
    AUXLIBS_SQLITE="$(pkgconf --libs sqlite3)" \
    AUXLIBS_CDB='-lcdb' \
    SHLIB_RPATH="-Wl,-rpath,/usr/lib/postfix $LDFLAGS" \
    OPT="$CFLAGS $CPPFLAGS $LDFLAGS" \
    -C $pkgbase-$pkgver

  make -C $pkgbase-$pkgver
}

package_postfix() {
  local _name _feature

  local _dynamicmaps_file="$pkgdir/etc/$pkgbase/dynamicmaps.cf"
  local _dynamicmaps_dir="$pkgdir/etc/$pkgbase/dynamicmaps.cf.d"
  local _files_file="$pkgdir/etc/$pkgbase/$pkgbase-files"
  local _files_dir="$pkgdir/etc/$pkgbase/$pkgbase-files.d"

  depends+=(db libsasl openssl zlib libicuuc.so libnsl.so)
  optdepends=(
    'perl: for postfix-collate.pl, postfix-tlstype.pl and qshape'
    'postfix-cdb: for CDB integration'
    'postfix-ldap: for LDAP integration'
    'postfix-lmdb: for LMDB integration'
    'postfix-mysql: for MySQL integration'
    'postfix-pcre: for PCRE integration'
    'postfix-pgsql: for PostgreSQL integration'
    'postfix-sqlite: for SQLite integration'
  )
  conflicts=(smtp-server smtp-forwarder)
  provides=(smtp-server smtp-forwarder)
  backup=(etc/postfix/{access,aliases,canonical,generic,header_checks,main.cf,master.cf,relocated,transport,virtual})
  install=$pkgbase.install

  cd $pkgbase-$pkgver
  LD_LIBRARY_PATH="lib:$LD_LIBRARY_PATH" \
  sh postfix-install -non-interactive install_root="$pkgdir"

  # additional man pages and scripts
  for _name in posttls-finger {smtp,qmqp}-{sink,source}; do
    install -vDm 644 man/man1/$_name.1 -t "$pkgdir/usr/share/man/man1/"
    install -vDm 755 bin/$_name -t "$pkgdir/usr/bin/"
  done
  install -vDm 644 man/man1/qshape.1 -t "$pkgdir/usr/share/man/man1/"
  install -vDm 755 auxiliary/qshape/qshape.pl "$pkgdir/usr/bin/qshape"
  install -vDm 755 auxiliary/collate/collate.pl "$pkgdir/usr/bin/postfix-collate.pl"
  install -vDm 755 auxiliary/collate/tlstype.pl "$pkgdir/usr/bin/postfix-tlstype.pl"
  install -vDm 644 auxiliary/collate/README* -t "$pkgdir/usr/share/doc/$pkgbase/collate/"
  # license
  install -vDm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgbase/"
  # sysusers.d
  install -vDm 644 ../$pkgbase.sysusers "$pkgdir/usr/lib/sysusers.d/$pkgbase.conf"
  # tmpfiles.d
  # NOTE: follows setup in conf/postfix-files
  install -vDm 644 ../$pkgbase.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/$pkgbase.conf"

  # create dynamicmaps.cf.d and postfix-files.d entries for split packages
  # remove targetted files from main configuration files
  for _feature in {cdb,ldap,lmdb,mysql,pcre,pgsql,sqlite}; do
    printf "Split out dynamicmaps file for %s\n" $pkgbase-$_feature
    grep "$pkgbase-$_feature" "$_dynamicmaps_file" > "$_dynamicmaps_dir/$pkgbase-$_feature.cf"
    sed -e "/$pkgbase-$_feature/d" -i "$_dynamicmaps_file"
    (
      cd "$pkgdir"
      _pick $pkgbase-$_feature etc/$pkgbase/dynamicmaps.cf.d/$pkgbase-$_feature.cf
    )

    printf "Split out %s from %s-files.d\n" $pkgbase-$_feature $pkgbase
    grep "$pkgbase-$_feature" "$_files_file" > "$_files_dir/$pkgbase-$_feature.cf"
    sed -e "/$pkgbase-$_feature/d" -i "$_files_file"

    printf "Split %s from %s-files\n" ${_feature^^}_README $pkgbase
    grep "${_feature^^}_README:" "$_files_file" >> "$_files_dir/$pkgbase-$_feature.cf"
    sed -e "/${_feature^^}_README:/d" -i "$_files_file"

    if [[ "$_feature" != cdb ]]; then
      printf "Split %s from %s-files\n" ${_feature}_table.5 $pkgbase
      grep "${_feature}_table.5:" "$_files_file" >> "$_files_dir/$pkgbase-$_feature.cf"
      sed -e "/${_feature}_table.5:/d" -i "$_files_file"
      (
        cd "$pkgdir"
        _pick $pkgbase-$_feature usr/share/man/man5/${_feature}_table.5
      )
    fi
    (
      cd "$pkgdir"
      _pick $pkgbase-$_feature etc/$pkgbase/$pkgbase-files.d/$pkgbase-$_feature.cf
      _pick $pkgbase-$_feature usr/lib/$pkgbase/$pkgbase-$_feature.so
      _pick $pkgbase-$_feature usr/share/doc/$pkgbase/${_feature^^}_README
    )
    # install directories that are otherwise cleared by calls to _pick()
    install -vdm 755 "$_dynamicmaps_dir"
    install -vdm 755 "$_files_dir"
  done

  # remove non-reproducible file, that only lists what the build circumstances were
  rm -v "$pkgdir/etc/$pkgbase/makedefs.out"
  sed -e '/makedefs/d' -i "$pkgdir/etc/$pkgbase/$pkgbase-files"
}

package_postfix-cdb() {
  depends+=(postfix libcdb.so)
  pkgdesc+=' (CDB integration)'

  mv -v $pkgname/* "$pkgdir"
}

package_postfix-ldap() {
  depends+=(postfix libldap)
  pkgdesc+=' (LDAP integration)'

  mv -v $pkgname/* "$pkgdir"
}

package_postfix-lmdb() {
  depends+=(lmdb postfix)
  pkgdesc+=' (LMDB integration)'

  mv -v $pkgname/* "$pkgdir"
}

package_postfix-mysql() {
  depends+=(postfix libmariadb.so)
  pkgdesc+=' (MySQL integration)'

  mv -v $pkgname/* "$pkgdir"
}

package_postfix-pcre() {
  depends+=(pcre2 postfix)
  pkgdesc+=' (PCRE integration)'

  mv -v $pkgname/* "$pkgdir"
}

package_postfix-pgsql() {
  depends+=(postfix libpq.so)
  pkgdesc+=' (PostgreSQL integration)'

  mv -v $pkgname/* "$pkgdir"
}

package_postfix-sqlite() {
  depends+=(postfix sqlite)
  pkgdesc+=' (SQLite integration)'

  mv -v $pkgname/* "$pkgdir"
}

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

license=(EPL)

validpgpkeys=('622C7C012254C186677469C50C0B590E80CA15A7') # Wietse Venema <wietse@porcupine.org>

sha256sums=(d22f3d37ef75613d5d573b56fc51ef097f2c0d0b0e407923711f71c1fb72911b  # postfix-3.7.3.tar.gz
	c02d57508fbfee90c06be73964b1b7869a94eb18ec488e587ed405cf04d864de  # postfix-3.7.3.tar.gz.sig
	2e0c7228700d052999e101a1daf4d35396b3a56607f25b9b722aa90190ea3a66  # linux6.patch
	99682cbc9edda4a10f8624d43ada577dd3bd8f2409ae5aef04ff7db0d956ef67  # postfix-3.5.8-main_defaults.patch
	fec58a3edb242daa9e45d1dd58cf19193fcadb44bc8ad8dc4e6f92f04b7257e5  # postfix.install
	63f39147887336bdd173fb9425998f0a0c6f00a31241f922a255c157bfd8d02e  # postfix.sysusers
	825106bc3a6db8872160c5d65be04ad92828a043b8c8decc531341f0fe4a5742) # postfix.tmpfiles



