# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=heimdal
pkgname=('heimdal' 'heimdal-libs' 'heimdal-devel')
pkgver=7.8.0
pkgrel=5
pkgdesc="Implementation of Kerberos V5 libraries"
arch=('i686' 'x86_64')
url="https://www.h5l.org/"
msys2_repository_url="https://github.com/heimdal/heimdal"
msys2_references=(
  "cpe: cpe:/a:heimdal:heimdal"
  "cpe: cpe:/a:heimdal_project:heimdal"
  "cpe: cpe:/a:kth:heimdal"
)
license=('custom')
makedepends=('libdb-devel' 'libxcrypt-devel' 'libedit-devel' 'libsqlite-devel' 'openssl-devel' 'perl-JSON' 'python' 'autotools' 'gcc') #libldap-devel
options=('!libtool' '!emptydirs' 'staticlibs')
groups=('net-utils')
source=(https://github.com/heimdal/heimdal/releases/download/heimdal-${pkgver}/heimdal-${pkgver}.tar.gz{,.sig}
        1.2.1-test-modules.patch
        1.5.2-hdbdir.patch
        1.5.2-install-catman.patch
        1.5.2-roken-signal.patch
        1.5.3-missing-libs-pkg-config.patch
        1.5.3-fix-detect-libedit.patch
        7.5.0-hcrypto-build-fix.patch
        7.5.0-names-clash-with-openssl.patch
        1041.patch)
sha256sums=('fd87a207846fa650fd377219adc4b8a8193e55904d8a752c2c3715b4155d8d38'
            'SKIP'
            'b18c9ca3f2db08cea0a4b7d7f07e8656360af848b3a30a5fa8d748240989da49'
            'a99f0f5b69d32524399b1343a4f08b2de43351828b5b45a938f72bacb0eb9085'
            'd68bb0f8f01cc748a6ef63c74a4a15c75553c2322e9bd2649c54cb23077d521b'
            'bf19c5d7326a0cf58a5b4922a9c96fb78807226bc40c2248417024b961dd559e'
            '5039112bbe27b814df115eca40607b957c13dd40bdc1d2d1ccc62b31fcfe87ac'
            'bb96587539084c2ea9e06da8d2b4c768edd8dd422587a045ea60d1aa3eeb6a8a'
            '1886fcb422902f80246ee104bab79a95bf8c26442038fe32977f17653e45ee2f'
            'bfe321c0391d6f7e23abecc22e9941bcb79d0b67abb9975246a9f462ec32513b'
            'bff78505a2f8bb29c7bfe60aa0cf4e586c00b1123329d2b840720b67b00d9326')
validpgpkeys=('FB925C7AFA000F52B4BBD1ED9A077911BB7DC320') # Heimdal Security <heimdal-security@heimdal.team>

prepare() {
  cd ${srcdir}/heimdal-${pkgver}

  patch -p2 -i "${srcdir}"/1.2.1-test-modules.patch
  patch -p2 -i "${srcdir}"/1.5.2-hdbdir.patch
  patch -p2 -i "${srcdir}"/1.5.2-install-catman.patch
  patch -p2 -i "${srcdir}"/1.5.2-roken-signal.patch
  patch -p1 -i "${srcdir}"/1.5.3-missing-libs-pkg-config.patch
  patch -p1 -i "${srcdir}"/1.5.3-fix-detect-libedit.patch

  patch -p1 -i "${srcdir}"/7.5.0-hcrypto-build-fix.patch
  patch -p1 -i "${srcdir}"/7.5.0-names-clash-with-openssl.patch

  # https://github.com/heimdal/heimdal/pull/1041
  patch -p1 -i "${srcdir}"/1041.patch

  ./autogen.sh
}

build() {
  cd ${srcdir}/heimdal-${pkgver}

  ./configure \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/libexec \
    --sbindir=/usr/bin \
    --sysconfdir=/etc/krb5 \
    --mandir=/usr/share/man \
    --datadir=/var/lib/heimdal \
    --localstatedir=/var/lib/heimdal \
    --with-openssl=/usr \
    --with-libedit=/usr \
    --with-sqlite3=/usr \
    --enable-shared=yes \
    --enable-static \
    --without-x \
    --disable-kcm \
    --disable-ndbm-db \
    --disable-heimdal-documentation \
    ac_cv_header_winsock2_h=no \
    ac_cv_header_ws2tcpip_h=no
  
  make LDFLAGS="${LDFLAGS} -no-undefined"
  setup
}

setup() {
  local destdir=${srcdir}/dest
  cd ${srcdir}/heimdal-${pkgver}
  make DESTDIR=${destdir} install

  # Remove daemons and their manpages
  for i in telnetd ftpd rshd; do
    if [ -f ${destdir}/usr/share/man/man8/${i}.8 ]; then
      rm ${destdir}/usr/share/man/man8/${i}.8
    fi
    if [ -f ${destdir}/usr/libexec/${i} ]; then
      rm ${destdir}/usr/libexec/${i}
    fi
  done

  # Rename clients and their manpages
  for i in rcp rsh telnet ftp su login; do
    if [ -f ${destdir}/usr/share/man/man1/${i}.1 ]; then
      mv ${destdir}/usr/share/man/man1/{,k}${i}.1
    fi
    if [ -f ${destdir}/usr/bin/${i} ]; then
      mv ${destdir}/usr/bin/{,k}${i}
    fi
  done

  # install config
  # Do not install the config as this will conflict with krb5
  install -D -m644 ${srcdir}/heimdal-${pkgver}/krb5.conf ${destdir}/etc/krb5/krb5.conf

  # Install the license
  install -Dm644 ${srcdir}/heimdal-${pkgver}/LICENSE \
    ${destdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_heimdal() {
  groups=('sys-utils')
  depends=('heimdal-libs')
  backup=(etc/krb5/krb5.conf)

  mkdir -p ${pkgdir}/usr

  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/bin/*-config
  rm -f ${pkgdir}/usr/bin/*.dll
  cp -rf ${srcdir}/dest/etc ${pkgdir}/
  cp -rf ${srcdir}/dest/usr/libexec ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
}

package_heimdal-libs() {
  depends=('libdb' 'libxcrypt' 'libedit' 'libsqlite' 'libopenssl')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_heimdal-devel() {
  pkgdesc="Heimdal headers and libraries"
  groups=('development')
  depends=('heimdal-libs' 'libxcrypt-devel' 'libedit-devel' 'libdb-devel' 'libsqlite-devel')

  mkdir -p ${pkgdir}/usr

  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/

  mkdir -p ${pkgdir}/usr/bin
  cp ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
}
