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

_pkgname=commons-cli
pkgname=java-${_pkgname}
pkgver=1.4
pkgrel=1
pkgdesc="Java API for parsing command line options passed to programs"
arch=('any')
url="http://commons.apache.org/cli/"
license=('APACHE')
depends=('java-runtime')
makedepends=('java-environment' 'jh' 'junit')
source=("https://archive.apache.org/dist/commons/cli/source/${_pkgname}-$pkgver-src.tar.gz")
sha256sums=('a1def8d94e4fe6be4ffde100a74a647fe3df5ba02f85e3c9191fbb00be880f16')

prepare() {
  cd "$srcdir/${_pkgname}-$pkgver-src"
  mkdir -p build/classes
  mkdir -p build/test-classes
}

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

  javac -d build/classes $(find src/main/java -name \*.java)
  javadoc -d "build/javadoc" -sourcepath "src/main/java" -subpackages org

  CLASSPATH="build/classes:/usr/share/java/junit.jar"
  javac -classpath $CLASSPATH -d "build/test-classes" \
    $(find "src/test/java" -name \*.java)

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

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

  # 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 \
    'pom.xml' "${_pkgname}.jar" "${_pkgname}-$pkgver.jar"

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