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

_libname=jsch-agent-proxy
_pkgname=jsch.agentproxy
pkgname=java-${_libname}
pkgver=0.0.8
pkgrel=1
pkgdesc="Proxy to ssh-agent and Pageant in Java"
arch=('any')
url='http://www.jcraft.com/jsch-agent-proxy/'
license=('BSD')
depends=('java-runtime')
makedepends=('java-environment' 'java-jsch' 'jh' 'jna')
source=("https://github.com/ymnk/${_libname}/archive/${pkgver}.tar.gz"
        'jsch-agent-proxy-remove_maven3_prerequisite.patch'
        'jsch-agent-proxy-bundles_to_jars.patch'
        'jsch-agent-proxy-poms.patch')
sha1sums=('811eb8d5ff1524bc1b2f9fa94f5ecda958b5dc41'
          '649d1a9fc1eacd125a85d5b9ec42c0ec2f29fa6e'
          '4bdb1f5b2c5367ae66d01857b3d7b501edf20835'
          'ee4112801473130951ed7559caeaf9be48a8dd62')

_artifacts=(core jsch sshagent usocket-nc usocket-jna pageant connector-factory)

prepare() {
  cd "${srcdir}/${_libname}-${pkgver}"
  patch -Np1 -i "${srcdir}/jsch-agent-proxy-remove_maven3_prerequisite.patch"
  patch -Np1 -i "${srcdir}/jsch-agent-proxy-bundles_to_jars.patch"
  patch -Np1 -i "${srcdir}/jsch-agent-proxy-poms.patch"
  mkdir -p {source,dist}

  for artifact in ${_artifacts[@]}; do
    mkdir -p "build/${artifact}/classes"
    cp -rv "${_libname}-${artifact}/src" source
  done
}

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

  CLASSPATH="/usr/share/java/jsch.jar:/usr/share/java/jna.jar:/usr/share/java/jna-platform.jar:dist/*"
  for artifact in ${_artifacts[@]}; do
    javac -cp $CLASSPATH -d "build/${artifact}/classes" -encoding UTF-8 \
      $(find "${_libname}-${artifact}/src/main/java" -name \*.java)
    jar -cvf "dist/${_pkgname}.${artifact}.jar" -C "build/${artifact}/classes" .
  done

  javadoc -classpath $CLASSPATH -d "build/javadoc" -encoding UTF-8 \
    -sourcepath source/src/main/java -subpackages com
}

package() {
  cd "${srcdir}/${_libname}-${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 com.jcraft jsch.agentproxy ${pkgver} pom.xml

  for artifact in ${_artifacts[@]}; do
    jh mvn-install com.jcraft "jsch.agentproxy.${artifact}" ${pkgver} \
      "${_libname}-${artifact}/pom.xml" \
      "dist/${_pkgname}.${artifact}.jar" \
      "${_pkgname}.${artifact}-${pkgver}.jar"

    ln -s "/usr/share/java/${_pkgname}.${artifact}-${pkgver}.jar" \
      "${pkgdir}/usr/share/java/${_pkgname}.${artifact}.jar"
  done
}
