# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>

_libname=hamcrest
pkgname=java-${_libname}
pkgver=1.3
pkgrel=4.parabola1
pkgdesc="Java matcher objects library"
arch=('any')
url="https://code.google.com/p/hamcrest"
license=('BSD')
depends=('java-runtime')
makedepends=('apache-ant' 'easymock' 'java-jarjar' 'java-qdox' 'jmock' 'jh' 'junit')
source=("https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/${_libname}/${_libname}-${pkgver}.tgz"
        "hamcrest-build.patch"
        "hamcrest-random_compilation_failure.patch")
sha256sums=('c6428e40d069fff3f99780efaae96c35ebdbf7cbfd475504254ebffcc19620c2'
            '7f78968554466dfd3f9f94c8bd9f9cc98e5588cdaf98497932faecd4ae3fdd9e'
            'ef400e638998ea3033f935082b7a51648f41ca55ac40501aa722349425a43566')

prepare() {
  cd "${srcdir}/${_libname}-${pkgver}"
  rm -v $(find . -name \*.jar)
  patch -Np1 -i "${srcdir}/hamcrest-build.patch"
  patch -Np1 -i "${srcdir}/hamcrest-random_compilation_failure.patch"
}

build() {
  cd "${srcdir}/${_libname}-${pkgver}"
  ant bigjar unit-test javadoc \
    -Dhaltonfailure=false -Dversion=${pkgver}
}

package() {
  cd "${srcdir}/${_libname}-${pkgver}"

  # Install license file
  install -Dm644 LICENSE.txt "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE.txt"

  # Install documentation
  install -d "${pkgdir}/usr/share/doc/${pkgname}"
  cp -r "build/temp/hamcrest-all-${pkgver}-javadoc.jar.contents" \
    "${pkgdir}/usr/share/doc/${pkgname}/javadoc"

  # Install Maven artifacts
  install -d "${pkgdir}/usr/share/java/${_libname}"
  export DESTDIR=${pkgdir}
  for artifact in $(printf '%s\n' *.pom|sed 's/-[0-9.]*\.pom$//'); do
    if [[ -f "build/${artifact}-${pkgver}.jar" ]]; then
      # This artifact has a jar file
      jh mvn-install "org.hamcrest" ${artifact} ${pkgver} \
        "${artifact}-${pkgver}.pom" \
        "build/${artifact}-${pkgver}.jar" \
        "${artifact}.jar"

      # Symlink them to /usr/share/java
      ln -s "/usr/share/java/${artifact}.jar" \
        "${pkgdir}/usr/share/java/${artifact//-/\/}-${pkgver}.jar"
      ln -s "/usr/share/java/${artifact}.jar" \
        "${pkgdir}/usr/share/java/${artifact//-/\/}.jar"
    else
      # This artifact is just a pom
      jh mvn-install "org.hamcrest" ${artifact} ${pkgver} \
        "${artifact}-${pkgver}.pom"
    fi
  done
}
