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

_libname=avalon-framework
pkgname=java-${_libname}
pkgver=4.2.0
pkgrel=7.parabola1
pkgdesc="Reusable components for J2SE applications."
arch=('any')
url="http://excalibur.apache.org/"
license=('APACHE')
depends=('java-runtime')
makedepends=('java-environment' 'java-avalon-logkit' 'jh' 'log4j-1.2')
source=("https://archive.apache.org/dist/excalibur/${_libname}/source/${_libname}-${pkgver}-src.tar.gz"
        "https://repo.maven.apache.org/maven2/${_libname}/${_libname}-api/${pkgver}/${_libname}-api-${pkgver}.pom"
        "https://repo.maven.apache.org/maven2/${_libname}/${_libname}-impl/${pkgver}/${_libname}-impl-${pkgver}.pom"
        'avalon-framework-rename_enum.patch')
md5sums=('fd42e4ed9d9cd7a4d2e7fca7ab8e847f'
         '7dd8406e2fda8ec924f094761b4695de'
         '1ae6d40afceecb706838ca4eb3157776'
         '577de617b81a6d73f2f19c543af99922')

prepare() {
  cd "${srcdir}/${_libname}"
  patch -Np1 -i "${srcdir}/avalon-framework-rename_enum.patch"
  mkdir -p build/classes
  mkdir -p build/classes/{api,impl,all}
}

build() {
  cd "${srcdir}/${_libname}"

  CLASSPATH="/usr/share/java/avalon-logkit.jar:/usr/share/java/log4j-1.2.jar"

  javac -cp $CLASSPATH -d "build/classes/api" \
    $(find "api/src/java" -name \*.java)
  javac -cp "$CLASSPATH:build/classes/api" -d "build/classes/impl" \
    $(find "impl/src/java" -name \*.java)
  javac -cp $CLASSPATH -d "build/classes/all" \
    $(find "api/src/java" "impl/src/java" -name \*.java)

  javadoc -classpath $CLASSPATH -d "build/javadoc" \
    -sourcepath "api/src/java" -subpackages org

  jar -cvf "${_libname}-api.jar" -C "build/classes/api" .
  jar -cvf "${_libname}-impl.jar" -C "build/classes/impl" .
  jar -cvf "${_libname}.jar" -C "build/classes/all" .
}

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

  # 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
  artifacts=("${_libname}-api" "${_libname}-impl")
  export DESTDIR=${pkgdir}
  for artifact in "${artifacts[@]}"; do
    jh mvn-install ${_libname} ${artifact} ${pkgver} \
      "${srcdir}/${artifact}-${pkgver}.pom" \
      "${artifact}.jar" \
      "${artifact}-${pkgver}.jar"
    ln -s "/usr/share/java/${artifact}-${pkgver}.jar" \
      "${pkgdir}/usr/share/java/${artifact}.jar"
  done

  install -Dm644 "${_libname}.jar" "${pkgdir}/usr/share/java/${_libname}/${_libname}.jar"
}
