# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor (Hyperbola): André Silva <emulatorman@hyperbola.info>

pkgname=netpbm
pkgver=10.73.31
pkgrel=1
pkgrel+=.parabola1
pkgdesc="A toolkit for manipulation of graphic images"
pkgdesc+=", without nonfree parts and patent issues"
arch=('x86_64')
arch+=('i686' 'armv7h')
license=('custom' 'BSD' 'GPL' 'LGPL')
url="http://netpbm.sourceforge.net/"
depends=('perl' 'libpng' 'libtiff' 'libxml2')
makedepends=('python2' 'jbigkit' 'subversion')
options=('!makeflags')
# Get docs with: wget --recursive --relative -nH http://netpbm.sourceforge.net/doc/
source=(https://downloads.sourceforge.net/project/netpbm/super_stable/$pkgver/netpbm-$pkgver.tgz
        https://sources.archlinux.org/other/packages/netpbm/netpbm-doc-31Jan2014.tar.xz{,.sig}
        netpbm-CAN-2005-2471.patch netpbm-security-code.patch netpbm-security-scripts.patch)
sha256sums=('584c95f1ccdcd66d5ca68d7d5501728ae1184cba324d713f6b2a3ca86424243b'
            '74bcf840ee643c6917330c382484010cb99c004a3fcf05391bebcac63815acb3'
            'SKIP'
            '991aba7897e0710687369b4e2edfba1397ecd77d62578eac496afbc47b5ca970'
            '2eaa0f842d90bb3dcbc66273d58ec9117b97029da231b141159e580e65b98eb0'
            '2c397588d9cba2c40dee55c07713d5e77e0e04245db939fdc1962d7de4cf4a33')
validpgpkeys=('5357F3B111688D88C1D88119FCF2CB179205AC90')

prepare() {
  cd $pkgname-$pkgver

  # Removing hpcdtoppm/pcdindex (non comercial)
  rm -rv converter/ppm/hpcdtoppm

  # Removing ppmtogif/giftopnm (patent restriction)
  rm -v converter/other/giftopnm.c converter/ppm/ppmtogif.c
  rm -v test/gif-quant-roundtrip.test test/gif-roundtrip.test test/all-in-place.ok

  # Removing programs without license information
  rm -v other/pamchannel.c test/pamchannel.ok
  rm -v converter/other/pamtopnm.c
  rm -v converter/pbm/pbmto4425.c
  rm -v converter/pbm/pbmtoln03.c
  rm -v converter/pbm/pbmtolps.c
  rm -v converter/pbm/pbmtopk.c
  rm -v converter/pbm/pktopbm.c
  rm -v converter/ppm/ppmtopjxl.c
  rm -v converter/pgm/spottopgm.c

  rm -v test/avs-roundtrip.test test/facesaver-roundtrip.test test/ilbm-roundtrip.test test/pamchannel.test \
        test/pamditherbw.test test/pamenlarge.test test/pamfile.test test/pfm-roundtrip.test \
        test/pgmtoppm.test test/pict-roundtrip.test test/pnminvert.test test/pnmremap1.test \
        test/pnmremap2.test test/pnmtopnm-plain.test test/pnm-pam-roundtrip.test test/pnm-plain-roundtrip.test \
        test/ps-alt-roundtrip.test test/ps-roundtrip.test test/sbig-roundtrip.test test/st4-roundtrip.test \
        test/targa-roundtrip.test test/utahrle-roundtrip.test test/winicon-roundtrip.test test/xwd-roundtrip.test

  # Removing references about hpcdtoppm/pcdindex (non comercial)
  sed -i 's/hpcdtoppm//g' converter/ppm/Makefile
  sed -i 's/hpcdtoppm\.1//g' buildtools/manpage.mk
  sed -i 's/pcdindex\.1//g' buildtools/manpage.mk

  # Removing references about ppmtogif/giftopnm (patent restriction)
  sed -i 's/ppmtogif//g' converter/ppm/Makefile test/all-in-place.test
  sed -i 's/ppmtogif\.1//g' buildtools/manpage.mk
  sed -i 's/giftopnm//g' converter/other/Makefile test/all-in-place.test
  sed -i 's/giftopnm\.1//g' buildtools/manpage.mk
  sed -i '/giftopnm/d' converter/other/pnmtopalm/README

  # Removing references about programs without license information
  sed -i 's/pamchannel//g' other/Makefile test/all-in-place.test
  sed -i 's/pamchannel\.1//g' buildtools/manpage.mk
  sed -i 's/pamchannel\.test//g' test/Test-Order

  sed -i 's/pamtopnm//g' converter/other/Makefile test/all-in-place.test
  sed -i 's/pamtopnm\.1//g' buildtools/manpage.mk
  sed -i '/pnmtopnm/d' netpbm.c

  sed -i 's/pbmto4425//g' converter/pbm/Makefile test/all-in-place.test
  sed -i 's/pbmto4425\.1//g' buildtools/manpage.mk

  sed -i 's/pbmtoln03//g' converter/pbm/Makefile test/all-in-place.test
  sed -i 's/pbmtoln03\.1//g' buildtools/manpage.mk

  sed -i 's/pbmtolps//g' converter/pbm/Makefile test/all-in-place.test
  sed -i 's/pbmtolps\.1//g' buildtools/manpage.mk

  sed -i 's/pbmtopk//g' converter/pbm/Makefile test/all-in-place.test
  sed -i 's/pbmtopk\.1//g' buildtools/manpage.mk

  sed -i 's/pktopbm//g' converter/pbm/Makefile test/all-in-place.test
  sed -i 's/pktopbm\.1//g' buildtools/manpage.mk

  sed -i 's/ppmtopjxl//g' converter/ppm/Makefile test/all-in-place.test
  sed -i 's/ppmtopjxl\.1//g' buildtools/manpage.mk

  sed -i 's/spottopgm//g' converter/pgm/Makefile test/all-in-place.test
  sed -i 's/spottopgm\.1//g' buildtools/manpage.mk

  #Removing references about nonfree licenses part
  sed -i '338,383d' doc/copyright_summary

  patch -p1 < ../netpbm-CAN-2005-2471.patch
  patch -p1 < ../netpbm-security-code.patch
  patch -p1 < ../netpbm-security-scripts.patch
  sed -i 's|#!/usr/bin/python|#!/usr/bin/python2|' buildtools/makeman
  sed -i 's|@python|@python2|' buildtools/manpage.mk

  cp config.mk.in  config.mk
  [ "${CARCH}" = 'x86_64' ] && echo 'CFLAGS_SHLIB = -fPIC' >> config.mk
  [ "${CARCH}" = 'armv7h' ] && echo 'CFLAGS_SHLIB = -fPIC' >> config.mk
  echo "NETPBM_DOCURL = file://${srcdir}/doc" >> config.mk
  echo 'TIFFLIB = libtiff.so' >> config.mk
  echo 'JPEGLIB = libjpeg.so' >> config.mk
  echo 'PNGLIB = libpng.so' >> config.mk
  echo 'ZLIB = libz.so' >> config.mk
  echo 'JBIGLIB = /usr/lib/libjbig.a' >> config.mk

  sed -i 's|misc|share/netpbm|' common.mk
  sed -i 's|/link|/lib|' lib/Makefile
  sed -i 's|install.manwebmain install.manweb install.man|install.man|' GNUmakefile
}

build() {
  cd $pkgname-$pkgver
  make
  # Generating useful man pages with html doc
  make MAKEMAN="${srcdir}/$pkgname-$pkgver/buildtools/makeman" USERGUIDE=. \
    -C ../doc -f "${srcdir}/$pkgname-$pkgver/buildtools/manpage.mk" manpages
}

package() {
  cd $pkgname-$pkgver
  make pkgdir="${pkgdir}/usr" PKGMANDIR=share/man install-run install-dev

# Removing dummy man pages
  rm "${pkgdir}"/usr/share/man/man{1,3,5}/*

  make MAKEMAN="${srcdir}/$pkgname-$pkgver/buildtools/makeman" MANDIR="${pkgdir}/usr/share/man" \
    -C ../doc -f "${srcdir}/$pkgname-$pkgver/buildtools/manpage.mk" installman

# Replace obsolete utility
  echo -e '#!/bin/sh\npamditherbw $@ | pamtopnm\n' > "${pkgdir}/usr/bin/pgmtopbm"

# Licensing.  Note that each program in the package has a separate license.
  install -D -m644 "${srcdir}/$pkgname-$pkgver/doc/copyright_summary" \
    "${pkgdir}/usr/share/licenses/${pkgname}/copyright_summary.txt"
}
