# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Sven-Hendrik Haase <sh@lutzhaase.com>
# Contributor: Filip <fila pruda.com>

pkgname=postfixadmin
pkgver=3.3.10
pkgrel=2
pkgdesc="Web based interface to manage Postfix mailboxes, virtual domains and aliases"
arch=('any')
license=('GPL2')
url="http://postfixadmin.sourceforge.net/"
backup=('etc/webapps/postfixadmin/config.inc.php'
        'etc/webapps/postfixadmin/config.local.php'
        'etc/webapps/postfixadmin/.htaccess'
        'etc/uwsgi/postfixadmin.ini')
depends=('php')
optdepends=('apache: run in webserver'
            'dovecot: interface local IMAP/POP3 server'
            'mariadb: use local MySQL server'
            'perl: for example scripts'
            'php-fpm: run in fastCGI process manager'
            'php-imap: use IMAP'
            'php-sqlite: use SQLite database backend'
            'php-pgsql: use PostgreSQL database backend'
            'postfix: interface local mail server'
            'postgresql: use local PostgreSQL server'
            'python: for example scripts'
            'uwsgi-plugin-php: run as application container')
source=("https://github.com/postfixadmin/${pkgname}/archive/${pkgname}-${pkgver}.tar.gz"
        "${pkgname}.sysusers"
        "${pkgname}.tmpfiles"
        "${pkgname}.uwsgi")
install="${pkgname}.install"
sha512sums=('e00fc9ea343a928976d191adfa01020ee0c6ddbe80a39e01ca2ee414a18247958f033970f378fe4a9974636172a5e094e57117ee9ac7b930c592f433097a7aca'
            'dc61527dbec79a3b19c8fcbc424fbfbe483cccf99a606e566ea6cd624d46c70a6d87dd8d4da398fedf12b60cfd0cf364c351d6b826e6eee6b2ca8eac0d9efcbe'
            '7e55c844be7b186d9eb568fe097632651c683582da83e0ee24cd6dd7c5a581fe05e5462174237f542ff8dc5d17a8ab651a77f0087a0c8df01d7b36c7722b1440'
            '2e3854d97bd74dc2501260c557549b0162f4a7749261538672de052b252e56fa62ef052bafe6d8681f2f87a94b71039fc9a68ff622a04dd2d6232213143d1840')
b2sums=('c565d18754c23c6f58ded401b7ec21e04432c9f6994bd353d5f73ee4cb100dc1f4a776673af71fab76e378dc36f97985d1f0bbc80c0ca2e1971ced17ee85e851'
        'a73314d9befe48869ca121bddfb3af61e916f8458f388e13eed1a7ffd64e7ae75604dc3af92d3d4d5b97c30d3cb089e28cbf64c7e81e324f3ff85d75b15b5f0d'
        '0e0170b08284dcfc5a68afd46a2e726d696905f3ddcd8158fed68c84fec039fabe60379005055e5fa1cc1fbb098c107b739d0e107f027bd7054d0e31b6e9cea6'
        'b59885eec24483d0f65614918f7c8cd70402600ac645f14560d5d28dfddce84a41a3ce1e2f9ea4bc0364ce299f468a2a243b0cbe920c0526f3c92e3d0269fa0c')

prepare(){
  mv -v "${pkgname}-${pkgname}-${pkgver}" "${pkgname}-${pkgver}"
  cd "${pkgname}-${pkgver}"
  # create empty local config (for overrides)
  echo "<?php" > "config.local.php"
  # create .htaccess file
  echo "#Require all denied" > .htaccess
  # remove unneeded executable bits
  find . -executable -type f \
    -and -not -path "*scripts*" -exec chmod -c 644 {} \;
  # remove symlinks
  find . -type l -exec rm -v {} +
  # remove unneeded debian directories
  rm -rv ADDITIONS/squirrelmail-plugin/debian
}

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

  # sysusers.d integration
  install -vDm 644 ../${pkgname}.sysusers \
    "${pkgdir}/usr/lib/sysusers.d/${pkgname}.conf"
  # tmpfiles.d integration
  install -vDm 644 ../${pkgname}.tmpfiles \
    "${pkgdir}/usr/lib/tmpfiles.d/${pkgname}.conf"
  # docs
  install -vDm 644 {README.md,CHANGELOG.TXT} \
    -t "${pkgdir}/usr/share/doc/${pkgname}/"
  install -vDm 644 DOCUMENTS/{README*,*.{txt,md}} \
    -t "${pkgdir}/usr/share/doc/${pkgname}/documents"
  cp -av ADDITIONS VIRTUAL_VACATION \
    "${pkgdir}/usr/share/doc/${pkgname}"

  # webapp
  find configs languages lib model public scripts templates tests \
    -type f \( \
    -iname "*.conf" -or \
    -iname "*.css" -or \
    -iname "*.eot" -or \
    -iname "*.gif" -or \
    -iname "*.ico" -or \
    -iname "*.js" -or \
    -iname "*.lang" -or \
    -iname "*.lib" -or \
    -iname "*.map" -or \
    -iname "*.php" -or \
    -iname "*.png" -or \
    -iname "*.sql" -or \
    -iname "*.svg" -or \
    -iname "*.tpl" -or \
    -iname "*.ttf" -or \
    -iname "*.woff" -or \
    -iname "*.woff2" \
    \) -exec install -vDm 644 {} "${pkgdir}/usr/share/webapps/${pkgname}/"{} \;
  install -vDm 644 {common,functions.inc,index}.php phpunit.xml \
    -t "${pkgdir}/usr/share/webapps/${pkgname}"
  (
    # adding weird symlinks
    cd "${pkgdir}/usr/share/webapps/${pkgname}/public"
    ln -sv users/password-change.php .
    ln -sv users/password-recover.php .
  )

  # config
  install -vDm 640 config.{inc,local}.php -t "${pkgdir}/etc/webapps/${pkgname}/"
  ln -sv "/etc/webapps/${pkgname}/config.inc.php" \
    "${pkgdir}/usr/share/webapps/${pkgname}/config.inc.php"
  ln -sv "/etc/webapps/${pkgname}/config.local.php" \
    "${pkgdir}/usr/share/webapps/${pkgname}/config.local.php"
  install -vDm 644 .htaccess -t "${pkgdir}/etc/webapps/${pkgname}/"
  ln -sv "/etc/webapps/${pkgname}/.htaccess" \
    "${pkgdir}/usr/share/webapps/${pkgname}/.htaccess"
  install -vDm 644 "../${pkgname}.uwsgi" "${pkgdir}/etc/uwsgi/${pkgname}.ini"
  # template caches
  install -vdm 755 "${pkgdir}/var/cache/${pkgname}/templates_c"
  ln -sv "/var/cache/${pkgname}/templates_c" \
    "${pkgdir}/usr/share/webapps/${pkgname}/"
}
