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

_pkgname=geronimo-osgi-support
pkgname=java-${_pkgname}
pkgver=1.1
pkgrel=1
pkgdesc="Java libraries providing OSGi lookup support for Geronimo projects"
arch=('any')
url='http://geronimo.apache.org/'
license=('APACHE')
depends=('java-runtime')
makedepends=('java-environment' 'jh' 'osgi-compendium' 'osgi-core' 'subversion')
source=("svn+https://svn.apache.org/repos/asf/geronimo/specs/tags/${_pkgname}-${pkgver}/")
md5sums=('SKIP')

_artifacts=(geronimo-osgi-registry geronimo-osgi-locator)

prepare() {
  cd "${srcdir}/${_pkgname}-${pkgver}"
  mkdir -p {source,dist}
  for artifact in ${_artifacts[@]}; do
    mkdir -p "build/${artifact}/classes"
    cp -rv "${artifact}/src" source
  done
}

build() {
  cd "${srcdir}/${_pkgname}-${pkgver}"
  CLASSPATH="/usr/share/java/osgi.compendium.jar:/usr/share/java/osgi.core.jar:dist/*"
  for artifact in ${_artifacts[@]}; do
    javac -cp $CLASSPATH -d "build/${artifact}/classes" -encoding UTF-8 \
      $(find "${artifact}/src/main/java" -name \*.java)
    jar -cvf "dist/${artifact}.jar" -C "build/${artifact}/classes" .
  done

  javadoc -classpath $CLASSPATH -d build/javadoc -encoding UTF-8 \
    -sourcepath source/src/main/java -subpackages org
}

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

  # Install license file
  install -Dm644 LICENSE "${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 org.apache.geronimo.specs ${_pkgname} ${pkgver} pom.xml

  for artifact in ${_artifacts[@]}; do
    jh mvn-install org.apache.geronimo.specs ${artifact} ${pkgver} \
      "${artifact}/pom.xml" \
      "dist/${artifact}.jar" \
      "${artifact}-${pkgver}.jar"
    ln -s "/usr/share/java/${artifact}-${pkgver}.jar" \
      "${pkgdir}/usr/share/java/${artifact}.jar"
  done
}
