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

_pkgname=asm
pkgname=java-${_pkgname}
pkgver=5.0.4
pkgrel=1.1
pkgdesc="An all purpose Java bytecode manipulation and analysis framework"
arch=('any')
url="http://asm.ow2.org/"
license=('BSD')
depends=('java-runtime')
makedepends=('apache-ant' 'java-ow-util-ant-tasks' 'jh')
provides=("${pkgname}4")
replaces=("${pkgname}4")
source=("https://download.forge.ow2.org/${_pkgname}/${_pkgname}-${pkgver}.tar.gz"
        "https://anonscm.debian.org/cgit/pkg-java/lib${_pkgname}4-java.git/plain/debian/osgi/${_pkgname}-all.osgi"
        "https://anonscm.debian.org/cgit/pkg-java/lib${_pkgname}4-java.git/plain/debian/osgi/${_pkgname}-analysis.osgi"
        "https://anonscm.debian.org/cgit/pkg-java/lib${_pkgname}4-java.git/plain/debian/osgi/${_pkgname}-commons.osgi"
        "https://anonscm.debian.org/cgit/pkg-java/lib${_pkgname}4-java.git/plain/debian/osgi/${_pkgname}-debug-all.osgi"
        "https://anonscm.debian.org/cgit/pkg-java/lib${_pkgname}4-java.git/plain/debian/osgi/${_pkgname}-tree.osgi"
        "https://anonscm.debian.org/cgit/pkg-java/lib${_pkgname}4-java.git/plain/debian/osgi/${_pkgname}-util.osgi"
        "https://anonscm.debian.org/cgit/pkg-java/lib${_pkgname}4-java.git/plain/debian/osgi/${_pkgname}-xml.osgi"
        "https://anonscm.debian.org/cgit/pkg-java/lib${_pkgname}4-java.git/plain/debian/osgi/${_pkgname}.osgi"
        "java-asm-remove_bnd.patch"
        "java-asm-rm_sigtest.patch")
md5sums=('bc328e4336a4fc30636b395b7f8081f3'
         'e9642186799f0b84a566fdf5f7fa3316'
         '482e376fdab1748bd6752f393d06785c'
         'd1106e99bd3cd95276de3e2486c52ca8'
         'a2628c0dcf771135d2d6df89d0adcc34'
         '2181f546ffd31355fb2b4edb4ff1e041'
         '713b3705dd6ae1ca85e739c090fe61a6'
         '40df5b93d559f9adf0dbdd4b42906b09'
         'b98f5fbd36e71efdaa5989fe683214cc'
         'c3518a026924311bf9b626dc83ccdc74'
         '19f97cfd07a888271a13b22b3aea05b6')

_ow_util_ant_tasks='/usr/share/java/ow_util_ant_tasks.jar'

prepare() {
  cd "${srcdir}/${_pkgname}-${pkgver}"
  rm -v examples/jasmin/test/jasmin.jar
  cp -v ../{asm,asm-{all,analysis,commons,debug-all,tree,util,xml}}.osgi archive
  patch -Np1 -i "${srcdir}/java-asm-remove_bnd.patch"
  patch -Np1 -i "${srcdir}/java-asm-rm_sigtest.patch"
}

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

  echo "objectweb.ant.tasks.path ${_ow_util_ant_tasks}" >> build.properties
  ant dist
}

package() {
  cd "${srcdir}/${_pkgname}-${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 output/dist/doc/javadoc/user "${pkgdir}/usr/share/doc/${pkgname}/javadoc"
  cp -r output/dist/examples         "${pkgdir}/usr/share/doc/${pkgname}/examples"

  # Install Maven artifacts
  export DESTDIR=${pkgdir}
  for artifact in $(find "output/dist/lib" -name \*.pom | sed 's/\.\///' | sed 's/-[0-9.]*\.pom$//'); do
    # Set artifactId
    _artifact=${artifact##*/}

    # Set mvn-basename to different versions
    base=$(jh mvn-basename "org.ow2.asm" ${_artifact} ${pkgver})
    _base=${base//${pkgver}/4.x}
    install -d "${pkgdir}${_base%/*}"

    if [[ -f ${artifact}-${pkgver}.jar ]]; then
      # This artifact has a jar file
      jh mvn-install "org.ow2.asm" ${_artifact} ${pkgver} \
        "${artifact}-${pkgver}."{pom,jar} \
        "${_artifact}-4.jar"
      ln -s "/usr/share/java/${_artifact}-4.jar" \
        "${pkgdir}/usr/share/java/${_artifact}-${pkgver}.jar"

      # Jar symlink
      ln -s "${base}.jar" \
        "${pkgdir}${_base}.jar"
    else
      # This artifact is just a pom
      jh mvn-install "org.ow2.asm" ${_artifact} ${pkgver} \
        "${artifact}-${pkgver}.pom"
    fi

    # Pom symlink
    ln -s "${base}.pom" \
      "${pkgdir}${_base}.pom"
  done
}
