# Maintainer (Arch): Andrea Scarpino <andrea@archlinux.org>
# Contributor (Arch): George Giorgidze <giorgidze (at) gmail.com>
# Contributor (Arch): Stefan Husmann <stefan-husmann@t-online.de>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: Luke Shumaker <lukeshu@parabola.nu>

pkgbase=antlr2
pkgname=('antlr2' antlr2-{java,python2,cxx})
pkgver=2.7.7
pkgrel=9
pkgrel+=.par1
pkgdesc="Language recognition tool providing a framework to build grammatical parsers"
arch=('x86_64')
arch+=('i686' 'armv7h')
url="http://www.antlr2.org/"
license=('custom:Public Domain')
makedepends=('java-environment' 'python2' 'bash' 'jh')
options=('staticlibs')
source=("http://www.antlr2.org/download/antlr-${pkgver}.tar.gz"
        "https://repo.maven.apache.org/maven2/antlr/antlr/$pkgver/antlr-$pkgver.pom"
        'runantlr2.sh'
        'gcc4.4.patch')
sha256sums=('853aeb021aef7586bda29e74a6b03006bcb565a755c86b66032d8ec31b67dbb9'
            '100f793ba27f8b4e4204edb46171ebf36e54e0f94cfc02527fea07a0bb1fceb7'
            '04c5b1e4d1fef6fe31852ecba2c18313dfa9d9971ee317274ea964b6f658c255'
            'b37444f9867544b8c7ad498b0bface51de0b34af8ef88eb7e2b298f102a10b1b')

prepare() {
  cd "${srcdir}/antlr-${pkgver}"
  patch -Np1 -i "${srcdir}"/gcc4.4.patch
}

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

  ./configure --prefix=/usr \
    --disable-examples \
    --disable-csharp
  make
}

_install_license() {
  install -Dm644 "$srcdir"/antlr-${pkgver}/LICENSE.txt \
    "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}

package_antlr2() {
  depends=('bash' 'antlr2-java')

  install -Dm755 "$srcdir"/runantlr2.sh "$pkgdir"/usr/bin/runantlr2
  _install_license
}

package_antlr2-java() {
  pkgdesc+=" (Java version)"
  depends=('java-runtime')

  provides=("java-antlr2=$pkgver")
  conflicts=("java-antlr2")
  replaces=("java-antlr2")

  DESTDIR="$pkgdir" jh mvn-install antlr antlr $pkgver \
    "$srcdir"/antlr-${pkgver}.pom \
    "$srcdir"/antlr-${pkgver}/antlr/antlr.jar \
    antlr2.jar

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

package_antlr2-python2() {
  pkgdesc+=" (Python version)"
  depends=('python2')

  provides=("python2-antlr2=$pkgver")
  conflicts=("python2-antlr2")
  replaces=("python2-antlr2")

  cd "$srcdir"/antlr-${pkgver}/lib/python
  python2 setup.py install --root="${pkgdir}" --optimize=1

  _install_license
}

package_antlr2-cxx() {
  pkgdesc+=" (C++ version)"

  cd "$srcdir"/antlr-${pkgver}/lib/cpp
  make prefix="${pkgdir}/usr" install

  _install_license
}
