# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: bill-auger <bill-auger@programmer.net>
#
# Maintainer (arch): Caleb Maclennan <caleb@alerque.com>

# parabola changes and rationale:
# - Added armv7h and i686 support
# - Removed non-free parts

pkgname=netpbm
pkgver=10.86.50
_rev=5219 # "Release 10.86.50", 2026-06-27
pkgrel=1
pkgdesc='A toolkit for manipulation of graphic images'
arch=(x86_64)
license=(Artistic-1.0 GPL-2.0-only LGPL-2.0-only MIT)
url="http://netpbm.sourceforge.net/"
depends=(bash
         glibc
         jbigkit
         libgcc
         libjpeg-turbo
         libpng
         libtiff
         libxml2
         perl
         zlib)
makedepends=(python)
source=(https://downloads.sourceforge.net/project/netpbm/super_stable/$pkgver/netpbm-$pkgver.tgz
        "netpbm-userguide::svn+http://svn.code.sf.net/p/netpbm/code/userguide#revision=$_rev"
        netpbm-CAN-2005-2471.patch
        netpbm-security-code.patch
        netpbm-security-scripts.patch
        netpbm-build-manpages.patch
        reproducible-man-gzip.patch)
sha256sums=('4c64f64835a138053a57f644debdef8279a2866eb3df11de9b97f9be155a5214'
            'SKIP'
            '7348274b72b8285add042d0f45d124c9833206ee3440bd1846cfc792b9b4d5e5'
            '155a4013a7eec259ec87239fd94fd6e67dc89088399031d3a8582bd841154862'
            '5ee27b4187577cbb9e85f6b36c5e5a421e03927f9195f888be7be2b647a5ac9d'
            'c289ec584097e2fdd7a975299d5a51e8266a706832c3d39105e4dd99e57ee94b'
            'b0be94fbfbf247552c5cc3f1cdd93e2d549f59c3d218350ccc1bea44e9bebc9b')

# Parabola: Mundane packaging considerations
pkgrel+=.parabola1
arch+=(armv7h i686)

# Parabola: Generate the tarball with non-free stuff removed
_srcrel=1 # increment this to rebuild the sourceball (mksource) without bumping pkgver/_rev
mksource=("${pkgname}-${pkgver}::svn+http://svn.code.sf.net/p/netpbm/code/super_stable#revision=$_rev"
          "${pkgname}-userguide::svn+http://svn.code.sf.net/p/netpbm/code/userguide#revision=$_rev")
mksha256sums=('SKIP'
              'SKIP')
mkdepends=(subversion)
mksource() {
  cd $pkgname-$pkgver

  # ./doc/copyright_summary is a 2001 analysis of Netpbm 9.20.  While
  # outdated, it gives us a starting point for pruning non-free parts.
  # But actually, it's a copy of Debian's `debian/copyright` file; we
  # can a more recent copy from Debian:
  # https://salsa.debian.org/debian-phototools-team/netpbm/-/blob/debian/2%2510.0-15.3/debian/copyright
  #
  # That's a link the last Debian tag before netpbm 10.78 / Debian
  # Bookworm; see
  # https://alioth-lists.debian.net/pipermail/pkg-phototools-devel/2026-July/019006.html

  # hpcdtoppm: Forbids noncommercial use.
  #
  # But it's not even really included in the upstream tarball:
  #
  #  - `./converter/ppm/hpcdtoppm/hpcdtoppm` is a stub shell-script
  #    that directs you where to download the "real" (non-free) thing.
  #  - `./converter/ppm/hpcdtoppm/pcdovtoppm` (formerly pcdindex) uses
  #    hpcdtoppm, and so is useless without hpcdtoppm
  #
  # Functionality lost: Converting from a Photo-CD.
  rm -r                                                      ./converter/ppm/hpcdtoppm/
  sed -i 's/hpcdtoppm//g'                                    ./converter/ppm/Makefile
  sed -Ei '/(hpcdtoppm|pcdovtoppm)/d'                        ./test/legacy-names.{test,ok}
  sed -i '/hpcdtoppm/d'                                      ./GNUmakefile
  sed -i '/hpcdtoppm/d'                                      ./buildtools/manpage.mk
  rm                                                         ../netpbm-userguide/hpcdtoppm.html
  rm                                                         ../netpbm-userguide/pcdovtoppm.html
  rm                                                         ../netpbm-userguide/pcdindex.html
  sed -Ei \
      -e '/"(hpcdtoppm|pcdovtoppm|pcdindex)\.html"/{N;d;}' \
      -e '/hpcdtoppmdistribution/,/^$/d'                     ../netpbm-userguide/directory.html

  drop() {
    local cfile=$1
    local dir="$(dirname -- "$cfile")"
    local name="$(basename --suffix=.c -- "$cfile")"

    rm                                   "${dir}/${name}.c"
    sed -Ei "s/\\b${name}\b//"           "${dir}/Makefile"
    sed -Ei "/\\b${name}\\b/d"           test/all-in-place.{test,ok}
    rm                                   "../netpbm-userguide/${name}.html"
    sed -Ei "/\"${name}\\.html\"/{N;d;}" "../netpbm-userguide/directory.html"
  }

  # ppmtogif: Patent encumbered.
  #
  #                             ... the GIF patents have been expired for several decades; it is
  # no-longer encumbered.  Note that previous Parabola netpbm packages
  # removed it, regardless.

  # pamchannel: "No copyright / license text found"
  #
  # This was a false-positive, even in v9.20 it was clearly written by
  # Bryan Henderson, who contributed it to the public domain.

  # pamtopnm: "No copyright / license text found"
  #
  # This was a false-positive, even in v9.20 it was clearly written by
  # Bryan Henderson, who contributed it to the public domain.

  # pbmto4425: No license.
  #
  # Unknown authorship / provenance.
  #
  # Functionality lost: Display on an an AT&T 4425 terminal (circa
  # 1983) (but not added to Netpbm until 1993 or 1994).
  drop                          ./converter/pbm/pbmto4425.c
  sed -i '/pbmto4425/d'         ./test/pbm-misc-converters.test
  sed -i '/^2141128209 77$/d'   ./test/pbm-misc-converters.ok
  sed -i \
      -e '/^<b><a href="pbmto4425\.html">pbmto4425<\/a><\/b>,$/d' \
      -e '/^<p><b>pbmto4425<\/b>/,/^$/d' \
                                ../netpbm-userguide/ppmtoterm.html

  # pbmtoln03: No license.
  #
  # While the copyright is presumably is held by Tim Cook
  # <tim@deakin.edu.au> and Ingo Wilken, no license is given.
  #
  # Fuctionality lost: Printing to a DEC LN03 printer (circa 1985)
  # (but not added to Netpbm until 1993).
  drop                          ./converter/pbm/pbmtoln03.c
  sed -i '/pbmtoln03/d'         ./test/pbm-misc-converters.test
  sed -i '/^1463148415 108$/d'  ./test/pbm-misc-converters.ok

  # pbmtolps: No license.
  #
  # While the copyright is presumably held by George Phillips (or the
  # University of British Columbia?), no license is given.
  #
  # Functionality lost: Converting to "lines" PostScript, which will
  # be imaged much faster than the usual PostScript "image" operator.
  drop                          ./converter/pbm/pbmtolps.c
  sed -i '/^pbmtolps /,/^$/d'   ./test/ps-alt-roundtrip.test
  sed -i '/^<b><a href="pbmtolps\.html">pbmtolps<\/a><\/b>,$/d' \
                                ../netpbm-userguide/{pbmto{epsi,psg3},pnmtops,pstopnm}.html

  # pbmtopk: No license.
  #
  # While copyright is presumably held by tomas rokicki and AJCD, no
  # license is given.
  #
  # Functionality lost: Convert to a Metafont "Packed (PK)" font file.
  drop                          ./converter/pbm/pbmtopk.c

  # pktopbm: No license.
  #
  # While copyright is presumably held by tomas rokicki, and AJCD, and
  # Janneke Nieuwenhuizen <janneke@gnu.org>, no license is given.
  #
  # Functionality lost: Convert from a Metafont "Packed (PK)" font
  # file.
  drop                          ./converter/pbm/pktopbm.c

  # spottopgm: No license.
  #
  # While copyright is presumably held by Warren Toomey, no license is
  # given.
  #
  # Functionality lost: Convert from SPOT satellite images.
  drop                          ./converter/pgm/spottopgm.c

  # ppmtopjxl: No license.
  #
  # While copyright is presumably held by AJCD, no license is given.
  #
  # Functionality lost: Convert to PCL (the language of HP printers),
  # for HP PaintJet and PaintJet XL printers.
  drop                          ./converter/ppm/ppmtopjxl.c
  sed -i '/^<a href="ppmtopjxl\.html"><b>ppmtopjxl<\/b><\/a>,$/d' \
                                ../netpbm-userguide/{pbmtolj,pnmtopclxl,ppmtopj}.html

  # Parabola's Netpbm package has historically (since it was first
  # forked from Arch's Netpbm in 2015) always removed "pnmtopnm", but
  # I'm pretty sure that was a typo for "pamtopnm".
}

# Parabola: Use the tarball with non-free stuff removed
source=(https://repo.parabola.nu/other/${pkgname}-libre/${pkgname}-${pkgver}-libre${_srcrel}.tar.gz{,.sig}
        "${source[@]:2}")
validpgpkeys+=('99195DD3BB6FE10A2F36ED8445698744D4FFBFC9'   # Luke T. Shumaker <lukeshu@parabola.nu>
               '3954A7AB837D0EA9CFA9798925DB7D9B5A8D4B40')  # bill-auger

prepare() {
  cd $pkgname-$pkgver

  patch -p1 < ../netpbm-CAN-2005-2471.patch
  patch -p1 < ../netpbm-security-code.patch
  patch -p1 < ../netpbm-security-scripts.patch
  patch -p1 < ../netpbm-build-manpages.patch
  patch -p1 < ../reproducible-man-gzip.patch

  cp config.mk.in  config.mk
  echo 'CFLAGS_SHLIB = -fPIC' >> config.mk
  echo "NETPBM_DOCURL = file:///usr/share/doc/${pkgname}/html" >> 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 -e 's|/sharedlink|/lib|' -e 's|/staticlink|/lib|' -i lib/Makefile
  sed -i 's|install.manwebmain install.manweb install.man|install.man|' GNUmakefile
}

build() {
  cd $pkgname-$pkgver
  CFLAGS+=' -std=gnu17'
  make

  # Generating useful man pages with html doc
  mkdir -p man
  make -C man USERGUIDE=../../netpbm-userguide -f ../buildtools/manpage.mk manpages
}

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

  install -m755 -d "${pkgdir}/usr/share/man/man1"
  install -m755 -d "${pkgdir}/usr/share/man/man3"
  install -m755 -d "${pkgdir}/usr/share/man/man5"

  make -C man MANDIR="${pkgdir}/usr/share/man" USERGUIDE=../../netpbm-userguide -f ../buildtools/manpage.mk installman

  install -D -m644 -t "${pkgdir}/usr/share/doc/${pkgname}/html" ../netpbm-userguide/*

# 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"
}
