# Maintainer (Parabola): Luke Shumaker <lukeshu@sbcglobal.net>

_pkgver=2.1-7r2-arduino4
_gitver='commit=a8cd90f8505d7c472b57550b1192fe67885dd638'

####

pkgname=java-rxtx
pkgver=${_pkgver//-/.}
pkgdesc="A native library providing serial and parallel communication for Java"
license=(LGPL2.1)

pkgrel=4
arch=('i686' 'x86_64')
depends=('java-runtime')
makedepends=('java-environment')

case "$_pkgver" in
    *arduino*)
	url="https://github.com/arduino/RXTX"
	source=("libre://RXTX-$pkgver.tar.gz")
	md5sums=('a71156587dc8d28d1ff4ece376bc0140')
	mksource=("git://github.com/arduino/RXTX.git#${_gitver}")
	mkmd5sums=('SKIP')
	_dirname=RXTX
	;;
    *)
	url="http://rxtx.qbang.org/"
	source=("http://rxtx.qbang.org/pub/rxtx/rxtx-${_pkgver}.zip")
	md5sums=('9290b4832d46199f8d798a531209640b')
	_dirname=rxtx-{$_pkgver}
	;;
esac

source+=(
  0000-configure-java-version.patch
  0001-makefile-destdir.patch
  0002-makefile-javadoc-dir.patch
  0003-lockdir-location.patch
  GroupMembership.txt)

md5sums+=('a36caaca717018341d69605f962d1c2a'
          '39c88043032de742c6b51d90a26eee2c'
          'a910e0c75a9c687d2b2f782e3d9982e3'
          '19b19c2514bce563be2a812888e1d9b4'
          '9dc461eab358e3b78c4b688977997116')

_MAKEFLAGS=('JHOME=/usr/share/java' 'RXTX_PATH=/usr/lib')

_changelog() {
  t="`echo -e '\t'`"
  
  # Parabola changes
  ls "$srcdir"/*.patch | sort -r | while read file; do
    echo "${file##*/}"
    sed "1s/./$t$t&/;1q" "$file"
  done

  # Arduino changes
  if [[ -d "$srcdir/$_dirname/.git" ]]; then
    pushd "$srcdir/$_dirname" > /dev/null
    git log --pretty=format:"%h %ad%n$t$t%s" --date=short|head -n-2
    popd > /dev/null
  fi

  # RXTX ChangeLog
  grep "^[0-9 $t]" "$srcdir/$_dirname/ChangeLog"
}

build() {
  _changelog > "$srcdir/ChangeLog.txt"
  cd "$srcdir/$_dirname"
  . /etc/profile.d/jdk.sh

  patch -Np1 -i "$srcdir/0000-configure-java-version.patch" # requires autogen
  patch -Np1 -i "$srcdir/0001-makefile-destdir.patch" # requires autogen
  patch -Np1 -i "$srcdir/0002-makefile-javadoc-dir.patch" # requires autogen
  patch -Np1 -i "$srcdir/0003-lockdir-location.patch"
  rm *.m4 && ./autogen.sh

  ./configure --prefix=/usr
  make "${_MAKEFLAGS[@]}" all docs
}

package() {
  cd "$srcdir/$_dirname"
  . /etc/profile.d/jdk.sh

  make "${_MAKEFLAGS[@]}" DESTDIR="$pkgdir" install

  install -d "$pkgdir/usr/share/doc/$pkgname/"
  install -m644 "$srcdir/ChangeLog.txt" "$pkgdir/usr/share/doc/$pkgname/"
  install -m644 "$srcdir/GroupMembership.txt" "$pkgdir/usr/share/doc/$pkgname/"
  cp -r javadoc "$pkgdir/usr/share/doc/$pkgname/"
}