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

pkgname=subversion
pkgver=1.8.13
pkgrel=2
pkgdesc="A Modern Concurrent Version Control System"
arch=('i686' 'x86_64')
url="http://subversion.apache.org/"
license=('APACHE')
groups=('VCS')
depends=('libsqlite' 'file' 'libserf' 'libsasl')
makedepends=('python2' 'perl' 'swig' 'ruby' 'libsqlite-devel' 'libserf-devel' 'libsasl-devel')
optdepends=('bash-completion: for svn bash completion'
            'python2: for some hook scripts'
            'ruby: for some hook scripts')
provides=('svn')
options=('!makeflags' '!libtool' '!emptydirs')
noextract=("subversion-${pkgver}.tar.bz2")
source=(http://www.apache.org/dist/subversion/subversion-${pkgver}.tar.bz2{,.asc}
        svnclean
        01-make-build-directories.patch
        02-fix-linking.patch
        03-fix-linking-perl.patch
        04-fix-switch-tests.patch
        05-retry-loop.patch
        06-svnadmin-binary-mode.patch
        07-fix-path-tests.patch
        08-ruby-test-fix.patch
        09-wincrypt.patch
        10-perl-vendors.patch
        11-XFAIL-case-sensitive-move-tests.patch
        12-fix-svnlook-test.patch
        13-dso_open.patch
        14-fix_svnauthz_tests.patch
        15-XFAIL_case_sensitive_tree_conflict.patch
        16-XFAIL_sqlite3_temp_table_tests.patch
        17-dont-build-tests.patch
        18-fix-comment-quotes.patch
        subversion-1.8.0-msys2.patch
        remove-checking-symlink.patch)
md5sums=('4413417b529d7bdf82f74e50df02e88b'
         'SKIP'
         '07ee0cb01bdf8468cb8d4c22c0d6bc04'
         'dbf63cb39ddea409a37bc6647065fae9'
         '779ba1926eb4ae336c82a3bb7c7d9405'
         'e72377cfd487e747802d3cac4791446a'
         '3dd9bd4c57c117feb47a5b031fd8963d'
         '4baa242af00a44ff7336f673b5ae6cb0'
         '74cd0f3f6e1690446d9045af4a329283'
         '87c5714962fe0aa2f681a74ea65b858e'
         '5c9424fa51cb6b45de95d977e468809c'
         '62d78ade3ac905df83591f084ae8d279'
         '0ed12264773291d1816790855edb8143'
         '8e0e89f7b5d8b270c381503323ee83e2'
         'da2379bad17a4a8d9058ee77585a844b'
         'f8517036988cc78f881a9e016a3997d0'
         '405c1bd35472a789c01ee2997da38af1'
         '70ea60f69961e2282a28f3bbf2772d0a'
         '906757eb7d836d91d8659621a2a8e6e8'
         '75e6f9210afa69d1838d85169012a59f'
         '7c608350472b2a398d861fc7fe139c75'
         '97df6f58cc6b2d27d7870a5f7d5378f2'
         'be151d5feeab94774c1366382044f821')

PYTHON_SITELIB=/usr/lib/python2.7/site-packages

prepare() {
  cd $startdir/
  [ -d $srcdir/$pkgname-$pkgver ] && rm -rf -d $srcdir/$pkgname-$pkgver
  tar -xjvf ${SRCDEST}/${pkgname}-${pkgver}.tar.bz2 -C $srcdir || true

  cd $srcdir/$pkgname-$pkgver

  sed -i 's|/usr/bin/env python|/usr/bin/env python2|' \
    tools/hook-scripts/{,mailer/{,tests/}}*.py \
    tools/client-side/mergeinfo-sanitizer.py

  sed -i 's|/usr/bin/env python|/usr/bin/env python2|' gen-make.py build/getversion.py
  sed -i 's|except IOError,|except IOError as|' build/getversion.py

  patch -p1 -i ${srcdir}/01-make-build-directories.patch
  patch -p1 -i ${srcdir}/02-fix-linking.patch
  patch -p1 -i ${srcdir}/03-fix-linking-perl.patch
  patch -p1 -i ${srcdir}/04-fix-switch-tests.patch
  patch -p1 -i ${srcdir}/05-retry-loop.patch
  patch -p1 -i ${srcdir}/06-svnadmin-binary-mode.patch
  patch -p1 -i ${srcdir}/07-fix-path-tests.patch
  patch -p1 -i ${srcdir}/08-ruby-test-fix.patch
  patch -p1 -i ${srcdir}/09-wincrypt.patch
  patch -p1 -i ${srcdir}/10-perl-vendors.patch
  patch -p1 -i ${srcdir}/11-XFAIL-case-sensitive-move-tests.patch
  patch -p1 -i ${srcdir}/12-fix-svnlook-test.patch
  patch -p1 -i ${srcdir}/13-dso_open.patch
  patch -p1 -i ${srcdir}/14-fix_svnauthz_tests.patch
  patch -p1 -i ${srcdir}/15-XFAIL_case_sensitive_tree_conflict.patch
  patch -p1 -i ${srcdir}/16-XFAIL_sqlite3_temp_table_tests.patch
  patch -p1 -i ${srcdir}/17-dont-build-tests.patch
  patch -p1 -i ${srcdir}/18-fix-comment-quotes.patch
  patch -p1 -i ${srcdir}/subversion-1.8.0-msys2.patch
  patch -p1 -i ${srcdir}/remove-checking-symlink.patch

  PYTHON=/usr/bin/python2 ./autogen.sh
}

build() {
  cd ${pkgname}-${pkgver}
  export PYTHON=/usr/bin/python2
  ./configure --prefix=/usr \
      --build=${CHOST} \
      --with-apr=/usr \
      --with-apr-util=/usr \
      --with-zlib=/usr \
      --with-serf=/usr \
      --with-sqlite=/usr \
      --without-apxs \
      --without-gnome-keyring \
      --enable-shared --disable-static \
      --with-ruby-sitedir=/usr/lib/ruby/vendor_ruby \
      --enable-disallowing-of-undefined-references \
      --enable-local-library-preloading

  #make LT_LDFLAGS="-L$Fdestdir/usr/lib"
  # make swig_pydir=/usr/lib/python2.7/site-packages/libsvn \
  #  swig_pydir_extra=/usr/lib/python2.7/site-packages/svn swig-py swig-pl swig-rb

  make clean-swig
  make autogen-swig
  make all

  plain "Compiling swig bindings: ruby"
  make swig-rb

  plain "Compiling swig bindings: perl"
  make swig-pl

  plain "Compiling swig bindings: python"
  make swig-py swig_pydir=${PYTHON_SITELIB}/libsvn swig_pydir_extra=${PYTHON_SITELIB}/svn
}

#check() {
#   cd ${pkgname}-${pkgver}
#   export LANG=C LC_ALL=C
#   make check check-swig-pl check-swig-py check-swig-rb CLEANUP=yes # check-javahl
#}

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

  make DESTDIR="${pkgdir}" INSTALLDIRS=vendor \
    swig_pydir=${PYTHON_SITELIB}/libsvn \
    swig_pydir_extra=${PYTHON_SITELIB}/svn \
    install install-tools install-swig-py install-swig-pl install-swig-rb

  install -dm755 "${pkgdir}"/usr/share/subversion
  cp -a tools/hook-scripts "${pkgdir}"/usr/share/subversion/
  rm "${pkgdir}"/usr/share/subversion/hook-scripts/*.in

  ## svnclean
  install -Dm 755 ${srcdir}/svnclean "${pkgdir}"/usr/bin/svnclean

  install -Dm 644 tools/client-side/bash_completion \
     "${pkgdir}"/usr/share/bash-completion/completions/subversion
  for i in svn svnadmin svndumpfilter svnlook svnsync svnversion; do
      ln -sf subversion "${pkgdir}"/usr/share/bash-completion/completions/${i}
  done

  # Remove illegal files
  find ${pkgdir}/usr -type f -name "*::*" | xargs rm -f

  # fix permissons
  find ${pkgdir}/usr -type f \( -name *.dll -o -name *.exe \) | xargs chmod 0755
}
