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

pkgname=javamail
pkgver=1.5.4
pkgrel=1
pkgdesc="JavaMail API Reference Implementation"
arch=('any')
url="https://javamail.java.net"
license=('CDDL, GPL2 with Classpath Exception')
depends=('java-runtime')
makedepends=('apache-ant' 'jh' 'tomcat8')
source=("https://java.net/downloads/${pkgname}/source/${pkgname}-${pkgver}-src.zip"
        "https://repo1.maven.org/maven2/com/sun/mail/javax.mail/${pkgver}/javax.mail-${pkgver}.pom"
        "https://repo1.maven.org/maven2/javax/mail/javax.mail-api/${pkgver}/javax.mail-api-${pkgver}.pom"
        "https://repo1.maven.org/maven2/com/sun/mail/logging-mailhandler/${pkgver}/logging-mailhandler-${pkgver}.pom")
md5sums=('b544903115ebc14702101e607f0429bd'
         'f41932d909b8511b8ad0a4c711040147'
         '313ab57304d245eeefbac89e0e213801'
         'acc5f53f9bbdac0dd91faa5127cb11b0')

build() {
  cd ${srcdir}
  ant release \
    -Djavaee.jar=/usr/share/java/tomcat8/servlet-api.jar

  cp "target/release/mail.jar" "javax.mail.jar"

  jar -cvf "javax.mail-api.jar" -C "mail/target/classes" "javax"
  jar -cvf "logging-mailhandler.jar" -C "mail/target/classes" "com/sun/mail/util/logging"
}

package() {
  cd "${srcdir}"

  # Install license file
  install -Dm644 "mail/src/main/resources/META-INF/LICENSE.txt" \
    "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE.txt"

  # Install documentation
  install -d "${pkgdir}/usr/share/doc/${pkgname}"
  cp -r "target/release/docs/javadocs" "${pkgdir}/usr/share/doc/${pkgname}"

  # Install Maven artifacts
  export DESTDIR=${pkgdir}
  for artifact in "all" "dsn" "gimap" "imap" "mailapi" "pop3" "smtp"; do
    if [[ -f "target/release/lib/${artifact}.jar" ]]; then
      # This artifact has a jar file
      jh mvn-install "com.sun.mail" ${artifact} ${pkgver} \
        "${artifact}/pom.xml" \
        "target/release/lib/${artifact}.jar" \
        "${artifact}.jar"

      # Symlink them to /usr/share/java
      ln -s "/usr/share/java/${artifact}.jar" \
        "${pkgdir}/usr/share/java/${artifact}-${pkgver}.jar"
    else
      # This artifact is just a pom
      jh mvn-install "com.sun.mail" ${artifact} ${pkgver} \
        "pom.xml"
    fi
  done

  for artifact in "javax.mail" "logging-mailhandler"; do
    jh mvn-install "com.sun.mail" ${artifact} ${pkgver} \
      "${artifact}-${pkgver}.pom" \
      "${artifact}.jar" \
      "${artifact}.jar"

    # Symlink them to /usr/share/java
    ln -s "/usr/share/java/${artifact}.jar" \
      "${pkgdir}/usr/share/java/${artifact}-${pkgver}.jar"
  done

  jh mvn-install "javax.mail" "javax.mail-api" ${pkgver} \
    "javax.mail-api-${pkgver}.pom" \
    "javax.mail-api.jar" \
    "javax.mail-api.jar"
  ln -s "/usr/share/java/javax.mail-api.jar" \
    "${pkgdir}/usr/share/java/javax.mail-api-${pkgver}.jar"
}