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

pkgname=slf4j
pkgver=1.7.21
pkgrel=1
pkgdesc="A successor to the log4j project."
arch=('any')
url='http://www.slf4j.org'
license=('MIT')
depends=('java-runtime')
makedepends=('java-commons-logging' 'java-environment' 'jh' 'log4j-1.2')
optdepends=('log4j')
source=("${url}/dist/${pkgname}-${pkgver}.tar.gz")
sha1sums=('0f383b6641c32799ffb5c2c36aae68647ad61856')

_artifacts=(slf4j-api slf4j-migrator slf4j-simple jcl-over-slf4j jul-to-slf4j log4j-over-slf4j slf4j-jdk14 slf4j-nop slf4j-jcl slf4j-log4j12)

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  rm -rv *.jar
  rm -rv integration/lib/*.jar
  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/commons-logging.jar:/usr/share/java/log4j-1.2.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.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 "org.${pkgname}" "${pkgname}-parent" ${pkgver} pom.xml

  for artifact in ${_artifacts[@]}; do
    jh mvn-install "org.${pkgname}" ${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
}
