# Maintainer: Luke Shumaker <lukeshu@sbcglobal.net>
# Maintainer (Arch): Guillaume ALAUX <guillaume@archlinux.org>
# Contributor (Arch): Andrew Wright <andreww@photism.org>
# Contributor (Arch): Paul Mattal <paul@archlinux.org>

pkgname=apache-ant
pkgver=1.9.6
pkgdesc="A Java-based build tool"
license=('Apache' 'custom:BSD3')
url="http://ant.apache.org/"

# Arch splits it into pkgname=('apache-ant' 'apache-ant-doc')
provides=("$pkgname-doc=$pkgver")
replaces=("$pkgname-libre" "$pkgname-doc")
conflicts=("$pkgname-libre" "$pkgname-doc")

pkgrel=1.parabola1
arch=('any')
depends=('java-environment')
makedepends=('jh')

_mirror=http://archive.apache.org/dist
source=($_mirror/ant/source/${pkgname}-${pkgver}-src.tar.bz2{,.asc}
        ${pkgname}.sh ${pkgname}.csh)
sha512sums=('cfd4fd5da725bb32d85c84f62835ce9511107da89c14487466d58df82d1f2b9ce6e363d3d02b10846c8463cae3287cb787f96f9103121b2e0e045b6027634ff0'
            'SKIP'
            'b5bd49bc99161e86f7198181aff7c47c8045e8cac7b7c1e2531dc49b71f1659ae35f90d33b01076b6bf665aa99f951c47ba09a0408b0aca43184f68ebd201768'
            'c8c4be2928af648e064d4dff9efb71e250d0fb4baa318932a88de04c9c2d328b8b1af0321e732d2f302d25893e1a627da5c72264b8bd6bd50e662ca10e3f50bf')
# Run this to get the validpgpkeys:
#  curl -s https://www.apache.org/dist/ant/KEYS | gpg --with-colons --with-fingerprint - | grep ^fpr: | cut -d: -f10
validpgpkeys=(5B1E231C400B113C061C26508104644F51898504
              47309207D818FFD8DCD3F83F1931D684307A10A5
              5F35E131F832ED23F761578BEFA3E779EDF62C35
              CE8075A251547BEE249BC151A2115AE15F6B8B72
              B36135CBA375AA1ADE562A1C6E947594C152431A
              F54C925C2454F21D86692540A0BFF93DAA0077B0
              07C69F931EE82E694E73B54203F68CBDDE8884A0
              9C60C6B3A5A9DF8FEDD299D65BE0BA8CB80602AE
              14604968898CEB74A5329360AEB01A153B7C75B1
              5CDF153E81AB0522A1E5BFBE483C23C67BF8BE8E
              7B8E57E91F116BECF9ACE6AF37A0E062971731FB
              F19E751B68B907C4F2E6B7B18D6D0AD09711DBFC
              06A228AAB83A18A8DF7B84B08614D6AB265B4C63
              61B656E44615E2AEF78E8DCF0E69F809710038F5
              0BADE59B0EC24E68C03CA4815EFAD9FE82A7FBCD)

# This list is adapted from https://ant.apache.org/manual/install.html#librarydependencies
# The format is:
#   jarname:description:pkgname:jarpath
_library_dependencies=(
  #jakarta-regexp-1.3.jar:Disregard
  jakarta-oro-2.0.8.jar:'<ftp task>':jakarta-oro:/usr/share/java/jakarta-oro.jar
  junit.jar:'<junit> task':junit:/usr/share/java/junit.jar
  xalan.jar:'<junitreport> task':xalan-java:/usr/share/java/xalan.jar
  antlr.jar:'<antlr> task':java-antlr2:/usr/share/java/antlr2.jar
  bsf.jar:'<script> task':java-commons-bsf2:/usr/share/java/bsf2.jar
  groovy-all.jar:'Groovy with <script> and <scriptdef> tasks':groovy:/usr/share/groovy/embeddable/groovy-all.jar
  #netrexx.jar:'<netrexx> task, Rexx with <script> task':???????:???????
  js.jar:'Javascript with <script> task':java-rhino:/usr/share/java/js.jar
  jython.jar:'Python with <script> task':jython:/opt/jython/jython.jar
  #jpython.jar:Disregard
  #jacl.jar:'TCL with <script> task':???????:???????
  #tcljava.jar:'TCL with <script> task':???????:???????
  beanshell.jar:'BeanShell with <script> task':'beanshell>=1.3':/usr/share/java/bsh.jar
  jruby.jar:'Ruby with <script> task':jruby:/opt/jruby/lib/jruby.jar
  #judo.jar:'Judoscript with <script> task':???????:??????
  commons-logging.jar:'CommonsLoggingListener':java-commons-logging:/usr/share/java/commons-logging/commons-logging.jar
  #log4j.jar:'Log4jListener':???????:???????
  commons-net.jar:'<ftp>, <rexec> and <telnet> tasks':'java-commons-net1>=1.4.0':/usr/share/java/commons-net.jar
  bcel.jar:"'classfileset' data type, JavaClassHelper for ClassConstants filter reader":java-bcel:/usr/share/java/bcel.jar
  mail.jar:'<mimemail> task, mime encoding with <mail>':java-gnumail:/usr/share/java/gnumail.jar
  activation.jar:'<mimemail> task, mime encoding with <mail>':java-activation-gnu:/usr/share/java/activation.jar
  jdepend.jar:'<jdepend> task':java-jdepend:/usr/share/java/jdepend.jar
  resolver.jar:"external files for 'xmlcatalog' datatype":java-resolver:/usr/share/java/resolver.jar
  jsch.jar:'<sshexec> and <scp> tasks':'java-jsch>=0.1.42':/usr/share/java/jsch.jar
  #jai.jar:'<image> task':???????:???????
)

for _dep in "${_library_dependencies[@]}"; do
  IFS=: read _jarname _desc _pkgname _jarpath <<<"$_dep"
  optdepends+=("${_pkgname}: ${_desc}")
  makedepends+=("${_pkgname}")
done
unset _dep _jarname _desc _pkgname _jarpath

build() {
  # This directory is what becomes
  # http://archive.apache.org/dist/ant/binaries/${pkgname}-${pkgver}-bin.tar.bz2
  # which is what Arch uses
  rm -rf "${srcdir}"/${pkgname}-${pkgver}-bin
  mkdir -p "${srcdir}"/${pkgname}-${pkgver}-bin

  cd "${srcdir}"/${pkgname}-${pkgver}
  # Symlink to external optional libraries/
  local _dep _jarname _desc _pkgname _jarpath
  for _dep in "${_library_dependencies[@]}"; do
    IFS=: read _jarname _desc _pkgname _jarpath <<<"$_dep"
    ln -sfv "${_jarpath}" lib/optional/${_jarname}
  done
  # Build
  export JAVA_HOME=/usr/lib/jvm/default
  sh build.sh -Ddist.dir="${srcdir}"/${pkgname}-${pkgver}-bin dist
}

package() {
  cd "${srcdir}"/${pkgname}-${pkgver}-bin
  source "${srcdir}"/${pkgname}.sh # Get the ANT_HOME env var
  source "/etc/profile.d/jre.sh"   # Get the JAVA_HOME env var

  ## Install profile.d script
  install -d "${pkgdir}"/etc/profile.d
  install -m755 "${srcdir}"/${pkgname}.{csh,sh} "${pkgdir}"/etc/profile.d/

  ## Install the main distribution
  install -d "${pkgdir}"/${ANT_HOME}
  cp -Rp * "${pkgdir}"/${ANT_HOME}

  ## Fix the distribution
  # Remove Windows .bat/.cmd files
  rm -f "${pkgdir}"/${ANT_HOME}/bin/*.{bat,cmd}
  # Fix python2 path
  sed -i 's|/usr/bin/python|/usr/bin/python2|' "${pkgdir}"/${ANT_HOME}/bin/runant.py
  # Symlink to external optional libraries
  local _dep _jarname _desc _pkgname _jarpath
  for _dep in "${_library_dependencies[@]}"; do
    IFS=: read _jarname _desc _pkgname _jarpath <<<"$_dep"
    ln -svfr "${pkgdir}/${_jarpath}" "${pkgdir}${ANT_HOME}/lib/${_jarname}"
  done

  ## Symlink the /opt distribution to the normal places

  cd "${pkgdir}"/${ANT_HOME}/lib
  local pom a
  for pom in *.pom; do
    a=${pom%.pom}
    base=$(jh mvn-basename org.apache.ant $a $pkgver)
    install -d "${pkgdir}/${base%/*}"
    ln -sfvr "${pkgdir}/${ANT_HOME}/lib/$a.pom" "${pkgdir}/${base}.pom"
    if [[ -e "${pkgdir}/${ANT_HOME}/lib/$a.jar" ]]; then
	    ln -sfvr "${pkgdir}/${ANT_HOME}/lib/$a.jar" "${pkgdir}/${base}.jar"
    fi
  done

  cd "${pkgdir}"/${ANT_HOME}/bin
  install -d "${pkgdir}"/usr/bin
  local prog
  for prog in *; do
    ln -sfvr "${pkgdir}/${ANT_HOME}/bin/$prog" "${pkgdir}/usr/bin/$prog"
  done
  install -d "${pkgdir}"/usr/share/{doc,licenses}/${pkgname}
  ln -sfvrT "${pkgdir}/${ANT_HOME}/README"   "${pkgdir}/usr/share/doc/${pkgname}/README"
  ln -sfvrT "${pkgdir}/${ANT_HOME}/WHATSNEW" "${pkgdir}/usr/share/doc/${pkgname}/WHATSNEW"
  ln -sfvrT "${pkgdir}/${ANT_HOME}/manual"   "${pkgdir}/usr/share/doc/${pkgname}/html"
  ln -sfvrT "${pkgdir}/${ANT_HOME}/LICENSE"  "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  ln -sfvrT "${pkgdir}/${ANT_HOME}/NOTICE"   "${pkgdir}/usr/share/licenses/${pkgname}/NOTICE"
  mv -T     "${pkgdir}/${ANT_HOME}/etc"      "${pkgdir}/etc/${pkgname}"
  ln -sfvrT "${pkgdir}/etc/${pkgname}"       "${pkgdir}/${ANT_HOME}/etc"
}