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

pkgname=jmock
pkgver=1.2.0
pkgrel=1
pkgdesc="A library that supports test-driven development of Java code with mock objects"
arch=('any')
url="http://www.jmock.org/"
license=('custom')
depends=('java-runtime')
makedepends=('java-environment' 'cglib' 'jh' 'junit')
source=("https://github.com/${pkgname}-developers/${pkgname}-library/archive/${pkgver}.tar.gz"
        "https://repo1.maven.org/maven2/${pkgname}/${pkgname}/${pkgver}/${pkgname}-${pkgver}.pom"
        "https://repo1.maven.org/maven2/${pkgname}/${pkgname}-cglib/${pkgver}/${pkgname}-cglib-${pkgver}.pom"
        "jmock-junit3_api.patch")
md5sums=('624d6a8a60ba6caa3abb2e5cdff86560'
         'd045f9fa7c37e604b2e7724f7d22c05d'
         '1d4816028f07a979c31cbe984d9ff850'
         'd213e801f1b50f773e033da0dd20ecf2')

prepare() {
  cd "${srcdir}/${pkgname}-library-${pkgver}"
  patch -Np1 -i "${srcdir}/jmock-junit3_api.patch"
  find . -name \*.jar -delete
  mkdir -p build/classes
  mkdir -p build/cglib/classes
}

build() {
  cd "${srcdir}/${pkgname}-library-${pkgver}"

  CLASSPATH="build/classes:/usr/share/java/cglib-nodep.jar:/usr/share/java/junit.jar"
  javac -classpath $CLASSPATH -d "build/classes" \
    $(find "src/org" -not -path "*/org/jmock/cglib*" -name \*.java)
  javac -classpath $CLASSPATH -d "build/cglib/classes" \
    $(find "src/org/jmock/cglib" -name \*.java)
  javadoc -classpath $CLASSPATH -d "build/javadoc" -sourcepath "src" -subpackages org

  jar -cvf "${pkgname}.jar" -C "build/classes" .
  jar -cvf "${pkgname}-cglib.jar" -C "build/cglib/classes" .
}

package() {
  cd "${srcdir}/${pkgname}-library-${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/javadoc" "${pkgdir}/usr/share/doc/${pkgname}"

  # Install Maven artifacts
  export DESTDIR=${pkgdir}
  jh mvn-install ${pkgname} ${pkgname} ${pkgver} \
    "${srcdir}/${pkgname}-${pkgver}.pom" \
    "${srcdir}/${pkgname}-library-${pkgver}/${pkgname}.jar" \
    "${pkgname}.jar"
  jh mvn-install ${pkgname} "${pkgname}-cglib" ${pkgver} \
    "${srcdir}/${pkgname}-cglib-${pkgver}.pom" \
    "${srcdir}/${pkgname}-library-${pkgver}/${pkgname}-cglib.jar" \
    "${pkgname}-cglib.jar"

  ln -s "/usr/share/java/${pkgname}.jar" \
    "${pkgdir}/usr/share/java/${pkgname}-${pkgver}.jar"
  ln -s "/usr/share/java/${pkgname}-cglib.jar" \
    "${pkgdir}/usr/share/java/${pkgname}-cglib-${pkgver}.jar"
}
