# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>

pkgname=lftp
pkgver=4.9.2
pkgrel=5
pkgdesc="Sophisticated command line based FTP client (net-utils)"
arch=('i686' 'x86_64')
license=('GPL3')
groups=('net-utils')
depends=('gcc-libs'
         'ca-certificates'
         'expat'
         'gettext'
         'libexpat'
         'libgnutls'
         'libiconv'
         'libidn2'
         'libintl'
         'libreadline'
         'libunistring'
         'openssh'
         'zlib')
makedepends=('gcc'
             'autotools'
             'gettext-devel'
             'libexpat-devel'
             'libgnutls-devel'
             'libiconv-devel'
             'libidn2-devel'
             'libreadline-devel'
             'libunistring-devel'
             'zlib-devel')
optdepends=('perl: needed for convert-netscape-cookies and verify-file')
url="https://lftp.yar.ru/"
backup=('etc/lftp.conf')
source=(https://lftp.yar.ru/ftp/${pkgname}-${pkgver}.tar.xz
        lftp-configure-needtrio.patch
        lftp-4.7.6-no-undefined.patch
        lftp-correct-gettext-macros.patch)
options=('debug' '!strip')
sha256sums=('c517c4f4f9c39bd415d7313088a2b1e313b2d386867fe40b7692b83a20f0670d'
            'a3781d946913a0d39a41d5b86390f255b36f67e9aea502e0117e701e295c7ea0'
            'e9a289f30d0d7fd9994165b6991a722dbe529f1fdbd738d5b44b48ec9c773bf4'
            'd6d74b134385e1c3f824b9a960376a2bcb7c0e39acab2ae1c3c764f9d338e650')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}
  patch -p1 -i ${srcdir}/lftp-configure-needtrio.patch
  patch -p1 -i ${srcdir}/lftp-4.7.6-no-undefined.patch
  patch -p1 -i ${srcdir}/lftp-correct-gettext-macros.patch

  autoreconf -fiv
}

build() {
  [[ -d "build-${CARCH}" ]] && rm -rf "build-${CARCH}"
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}

  local CYGWIN_CHOST="${CHOST/-msys/-cygwin}"
  ../${pkgname}-${pkgver}/configure \
    --build=${CYGWIN_CHOST} \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --with-gnutls \
    --without-openssl \
    --without-included-regex \
    --with-expat \
    --with-libidn2 \
    --disable-rpath
  make
}

check() {
  cd ${srcdir}/build-${CARCH}
  make -k check
}

package() {

  cd ${srcdir}/build-${CARCH}
  make DESTDIR=${pkgdir} install
  
  grep -q 'set ssl:ca-file' ${pkgdir}/etc/lftp.conf || echo 'set ssl:ca-file /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem' >> ${pkgdir}/etc/lftp.conf

}
