# Maintainer: George Rawlinson <george@rawlinson.net.nz>

pkgbase=z-push
pkgname=(
  # base package
  'z-push-common'
  # backend
  'z-push-backend-caldav'
  'z-push-backend-carddav'
  'z-push-backend-combined'
  'z-push-backend-galsearch-ldap'
  'z-push-backend-imap'
  'z-push-backend-ldap'
  # ipc
  'z-push-ipc-sharedmemory'
  'z-push-ipc-memcached'
  # autodiscover
  'z-push-autodiscover'
  # device state
  'z-push-state-sql'
)
pkgver=2.6.4
pkgrel=1
pkgdesc="Open-source ActiveSync implementation"
arch=('any')
url="https://z-push.org"
license=('AGPL3')
options=('!strip')
source=(
  "$pkgbase-$pkgver.tar.gz::https://stash.kopano.io/rest/api/latest/projects/ZHUB/repos/z-push/archive?at=refs%2Ftags%2F$pkgver&format=tgz"
  'tmpfiles.conf'
  'sysusers.conf'
)
b2sums=('f1686a9b00a3a1427d2a1ce383dbc92f0156be8a2f2b901dda2e9c3be6fbef921a69f90d8e84f3962667742f98405eceb51fb139a8f33aa6094008f7b84f16ee'
        '549a61faeed3762ab81ce649eae93ea6cac218740d8bfb022ceef045d5a23eb14b4f94d3cc4065b6c16b8de23f716d2ad10323376c662b54d2dfbbf33cbf1be1'
        '23a9402bfe309b0e2f509c4f6d250c4b8cb985af358659170563b7393c17de168d8dea5fc1e6222f658b005cf1b2bd00afd0cdf19fc3e150645b9edd29a8cd01')

package_z-push-common() {
  depends=(
    'sh'
    'php'
  )
  optdepends=(
    'php-fpm: to run in FastCGI process manager'
    'z-push-backend-caldav: to use a CalDAV server for calendaring'
    'z-push-backend-carddav: to use a CardDAV server for contacts'
    'z-push-backend-combined: to combine different backends'
    'z-push-backend-galsearch-ldap: to search Global Address Book on a LDAP server'
    'z-push-backend-imap: to use an IMAP server for e-mails'
    'z-push-backend-ldap: to use a LDAP server for contacts'
    'z-push-ipc-sharedmemory: to use the default IPC provider'
    'z-push-ipc-memcached: to use memcached as an IPC provider'
    'z-push-autodiscover: to utilise AutoDiscover'
    'z-push-state-sql: to store device state in a SQL database'
  )
  backup=(
    'etc/z-push/z-push.conf.php'
    'etc/z-push/policies.ini'
  )

  # create folders
  install -vd \
    "$pkgdir/usr/share/$pkgbase" \
    "$pkgdir/usr/share/doc/$pkgbase" \
    "$pkgdir/etc/$pkgbase"

  # install binary wrappers
  install -vDm755 -t "$pkgdir/usr/bin" \
    build/deb/z-push-admin \
    build/deb/z-push-top

  # install common
  cp -rv \
    src/include \
    src/lib \
    src/vendor \
    src/index.php \
    src/z-push-admin.php \
    src/z-push-top.php \
    "$pkgdir/usr/share/$pkgbase"

  # logrotate configuration
  install -vDm644 config/z-push.lr "$pkgdir/etc/logrotate.d/$pkgbase"

  # man pages
  install -vDm644 -t "$pkgdir/usr/share/man/man8" \
    man/z-push-admin.8 man/z-push-top.8

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgbase" \
    CONTRIBUTING.md README.md src/DEVELOPMENT src/INSTALL

  # web server documentation
  cp -rv config/apache2 config/lighttpd config/nginx \
    "$pkgdir/usr/share/doc/$pkgbase"

  # license/trademark
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgbase" \
    src/TRADEMARKS

  # systemd integration
  install -vDm644 tmpfiles.conf "$pkgdir/usr/lib//tmpfiles.d/$pkgbase.conf"
  install -vDm644 sysusers.conf "$pkgdir/usr/lib/sysusers.d/$pkgbase.conf"

  # configuration
  install -vDm644 src/config.php "$pkgdir/etc/$pkgbase/z-push.conf.php"
  ln -sfv /etc/z-push/z-push.conf.php "$pkgdir/usr/share/$pkgbase/config.php"
  install -vDm644 src/policies.ini "$pkgdir/etc/$pkgbase/policies.ini"
  ln -sfv /etc/z-push/policies.ini "$pkgdir/usr/share/$pkgbase/policies.ini"

  #
  # patching
  #

  # set version number
  sed -s "s/ZPUSHVERSION/$pkgver/" build/version.php.in > "$pkgdir/usr/share/$pkgbase/version.php"

  # patch web-user (www-data -> z-push)
  sed -i -e "s/www-data/z-push/g" \
    "$pkgdir/usr/bin/z-push-admin" \
    "$pkgdir/usr/bin/z-push-top" \
    "$pkgdir/etc/logrotate.d/z-push"
}

package_z-push-backend-caldav() {
  pkgdesc="Enables Z-Push to use a CalDAV backend for calendaring"
  depends=('z-push-common' 'awl')
  backup=('etc/z-push/caldav.conf.php')

  cd src/backend/caldav

  # package backend/caldav
  install -vDm644 -t "$pkgdir/usr/share/$pkgbase/backend/caldav" caldav.php

  # configuration
  install -vDm644 config.php "$pkgdir/etc/$pkgbase/caldav.conf.php"
  ln -sfv /etc/z-push/caldav.conf.php "$pkgdir/usr/share/$pkgbase/backend/caldav/config.php"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgbase/caldav" AUTHOR REQUIREMENTS
}

package_z-push-backend-carddav() {
  pkgdesc="Enables Z-Push to use a CardDAV backend for contacts"
  depends=('z-push-common' 'php-xsl')
  backup=('etc/z-push/carddav.conf.php')

  cd src/backend/carddav

  # package backend/carddav
  install -vDm644 -t "$pkgdir/usr/share/$pkgbase/backend/carddav" carddav.php

  # configuration
  install -vDm644 config.php "$pkgdir/etc/$pkgbase/carddav.conf.php"
  ln -sfv /etc/z-push/carddav.conf.php "$pkgdir/usr/share/$pkgbase/backend/carddav/config.php"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgbase/carddav" README REQUIREMENTS THANKS
}

package_z-push-backend-combined() {
  pkgdesc="Enables Z-Push to simultaneously use different backends"
  depends=('z-push-common')
  backup=('etc/z-push/combined.conf.php')

  cd src/backend/combined

  # package backend/combined
  install -vDm644 -t "$pkgdir/usr/share/$pkgbase/backend/combined" \
    combined.php importer.php exporter.php

  # configuration
  install -vDm644 config.php "$pkgdir/etc/$pkgbase/combined.conf.php"
  ln -sfv /etc/z-push/combined.conf.php "$pkgdir/usr/share/$pkgbase/backend/combined/config.php"
}

package_z-push-backend-galsearch-ldap() {
  pkgdesc="Enables Z-Push to search the Global Address Book of a LDAP backend"
  depends=('z-push-common')
  backup=('etc/z-push/galsearch-ldap.conf.php')

  cd src/backend/searchldap

  # package backend/searchldap
  install -vDm644 -t "$pkgdir/usr/share/$pkgbase/backend/searchldap" searchldap.php

  # configuration
  install -vDm644 config.php "$pkgdir/etc/$pkgbase/galsearch-ldap.conf.php"
  ln -sfv /etc/z-push/galsearch-ldap.conf.php "$pkgdir/usr/share/$pkgbase/backend/searchldap/config.php"
}

package_z-push-backend-imap() {
  pkgdesc="Enables Z-Push to use an IMAP backend for e-mails"
  depends=('z-push-common' 'php-imap' 'awl')
  backup=('etc/z-push/imap.conf.php')

  # create folders
  install -vd "$pkgdir/usr/share/$pkgbase/backend" "$pkgdir/usr/share/doc/$pkgbase/imap"

  # package backend/imap
  cp -rv src/backend/imap "$pkgdir/usr/share/$pkgbase/backend"

  # fix incorrect permissions
  find "$pkgdir/usr/share/$pkgbase/backend/imap/Auth" -type f -perm 755 -exec chmod 644 {} \;

  # configuration
  install -vDm644 src/backend/imap/config.php "$pkgdir/etc/$pkgbase/imap.conf.php"
  ln -sfv /etc/z-push/imap.conf.php "$pkgdir/usr/share/$pkgbase/backend/imap/config.php"

  # documentation
  mv -v "$pkgdir/usr/share/$pkgbase/backend/imap/"{README,REQUIREMENTS,THANKS} \
    "$pkgdir/usr/share/doc/$pkgbase/imap"
}

package_z-push-backend-ldap() {
  pkgdesc="Enables Z-Push to use a LDAP backend for contacts"
  depends=('z-push-common')
  backup=('etc/z-push/ldap.conf.php')

  cd src/backend/ldap

  # package backend/ldap
  install -vDm644 -t "$pkgdir/usr/share/$pkgbase/backend/ldap" ldap.php

  # configuration
  install -vDm644 config.php "$pkgdir/etc/$pkgbase/ldap.conf.php"
  ln -sfv /etc/z-push/ldap.conf.php "$pkgdir/usr/share/$pkgbase/backend/ldap/config.php"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgbase/ldap" AUTHOR
}

package_z-push-autodiscover() {
  pkgdesc="Enables Z-Push to utilise AutoDiscover"
  depends=('z-push-common' 'awl')
  backup=('etc/z-push/autodiscover.conf.php')

  cd src/autodiscover

  # package autodiscover
  install -vDm644 -t "$pkgdir/usr/share/$pkgbase/autodiscover" \
    autodiscover.php response.xml

  # configuration
  install -vDm644 config.php "$pkgdir/etc/$pkgbase/autodiscover.conf.php"
  ln -sfv /etc/z-push/autodiscover.conf.php \
    "$pkgdir/usr/share/$pkgbase/autodiscover/config.php"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgbase/autodiscover" INSTALL
}

package_z-push-ipc-sharedmemory() {
  pkgdesc="Default IPC provider for Z-Push"
  depends=('z-push-common')

  cd src/backend/ipcsharedmemory

  # package backend/ipcsharedmemory
  install -vDm644 -t "$pkgdir/usr/share/$pkgbase/backend/ipcsharedmemory" \
    ipcsharedmemoryprovider.php
}

package_z-push-ipc-memcached() {
  pkgdesc="Enables Z-Push to use memcached as an IPC provider"
  depends=('z-push-common' 'php-memcached' 'memcached')
  backup=('etc/z-push/memcached.conf.php')

  cd src/backend/ipcmemcached

  # package backend/ipcmemcached
  install -vDm644 -t "$pkgdir/usr/share/$pkgbase/backend/ipcmemcached" \
    ipcmemcachedprovider.php

  # configuration
  install -vDm644 config.php "$pkgdir/etc/$pkgbase/memcached.conf.php"
  ln -sfv /etc/z-push/memcached.conf.php "$pkgdir/usr/share/$pkgbase/backend/ipcmemcached/config.php"
}

package_z-push-state-sql() {
  pkgdesc="Enables Z-Push to store device state in a SQL database"
  depends=('z-push-common')
  optdepends=(
    'mariadb: to use a local MariaDB database'
    'php-pgsql: to use the PostgreSQL database backend'
    'postgresql: to use a local PostgreSQL database'
    'php-odbc: to use the ODBC database backend'
    'php-sqlite: to use the SQLite database backend'
  )
  backup=('etc/z-push/state-sql.conf.php')

  cd src/backend/sqlstatemachine

  # package backend/sqlstatemachine
  install -vDm644 -t "$pkgdir/usr/share/$pkgbase/backend/sqlstatemachine" \
    sqlstatemachine.php

  # configuration
  install -vDm644 config.php "$pkgdir/etc/$pkgbase/state-sql.conf.php"
  ln -sfv /etc/z-push/state-sql.conf.php "$pkgdir/usr/share/$pkgbase/backend/sqlstatemachine/config.php"
}

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