# Recipe created by recipetool
# recipetool create -o netpbm_10.73.33.bb https://sourceforge.net/projects/netpbm/files/super_stable/10.73.33/netpbm-10.73.33.tgz

#ref: https://bkhome.org/news/201706/netpbm-compiled-in-openembedded.html

LICENSE = "GPLv2"
LIC_FILES_CHKSUM = "file://doc/GPL_LICENSE.txt;md5=079b27cd65c86dbc1b6997ffde902735"

SRC_URI = "https://sourceforge.net/projects/netpbm/files/super_stable/${PV}/netpbm-${PV}.tgz \
 file://shared_files-fix.patch \
 file://oeendiangen \
 file://libopt"
SRC_URI[md5sum] = "49f4a7c31a42415c0dd94f6147765705"
SRC_URI[sha1sum] = "5edc9fdeca86cca60e973e2d3310444016682331"
SRC_URI[sha256sum] = "e370c3593c3a2a38edd7181c1ebfc40f03dba6ade915ad46ca5d4eb46c3e381a"
SRC_URI[sha384sum] = "44d157793a73fe432ef4c55ac81a1421c05fbeaa66097a0c292845a7d370dd4d7c146837428d65f24f82568ce4c20e48"
SRC_URI[sha512sum] = "283558e0b6ab81662906851aa2ac35a927def10a274029a8a2ae38fcbe5da1237db64b4d8aee7a511a9fff8bdf06ad78aa15d625060056f76ecfe15285694365"

DEPENDS = "libx11 jpeg libpng tiff libxpm librsvg perl-native flex-native libxml2 zlib librsvg"
inherit pkgconfig perlnative

#override OE...
CFLAGS_append = " -Wno-error=format-security "

# NOTE: this is a Makefile-only piece of software.

do_configure () {
 sed -i -e 's%libpng\-config \-\-ldflags%pkg-config --libs libpng16%' buildtools/configure.pl
 sed -i -e 's%libpng\-config \-\-cflags%pkg-config --cflags libpng16%' buildtools/configure.pl
 sed -i -e 's%xml2\-config %pkg-config libxml-2.0 %' buildtools/configure.pl
 sed -i -e 's%libpng$(PNGVER)\-config %pkg-config libpng16 %' converter/other/Makefile
 sed -i -e 's%xml2\-config %pkg-config libxml-2.0 %' converter/other/Makefile
 sed -i -e 's%xml2\-config %pkg-config libxml-2.0 %' GNUmakefile
 
 #ref: https://t2sde.org/packages/netpbm
 #echo -e "\ngnu\nregular\nshared\ny\n\n\n\n\n\n\n\n\n\n`pkgprefix libdir libx11`/libX11.so\n\nnone\n" | ./configure
 echo -e '\ngnu\nregular\nshared\nn\n\n\n\n\n\n\n\n\n\n\n' | ./configure
 
 #20210103 cross-compiling for rpi4, binaries all x86_64, fix...
 sed -i '/^CC =/d' ./config.mk
 sed -i '/^CFLAGS =/d' ./config.mk
 sed -i '/^CFLAGS_MERGE =/d' ./config.mk
 sed -i -e 's%^LDRELOC =.*%LDRELOC = $(LD) --reloc%' ./config.mk
 
# #20210103
# #this is supposed to be automatically generated, but is broken...
# echo '#ifndef PM_CONFIG_H
##define PM_CONFIG_H
##include <inttypes.h>' > pm_config.h
# if [ "${TARGET_ARCH}" == "x86_64" ];then
#  echo '#define HAVE_INT64 1' >> pm_config.h
# else
#  if [ "${TARGET_ARCH}" == "aarch64" ];then
#   echo '#define HAVE_INT64 1' >> pm_config.h
#  else
#  echo '#define HAVE_INT64 0' >> pm_config.h
#  fi
# fi
# cat pm_config.in.h >> pm_config.h
# echo '#endif' >> pm_config.h
 
 #20210103
 # The following replaces the endiangen binary program being run and uses
 # the target endian.h header instead. note, got this from old classic-oe recipe,
 # ref: https://github.com/openembedded/openembedded/tree/master/recipes/netpbm
 cp -f ${WORKDIR}/oeendiangen buildtools/endiangen
 chmod 755 buildtools/endiangen
 touch buildtools/endiangen
 
 #20210103
 # the following replaces the typegen binary executable...
 case "${TARGET_ARCH}" in
  *64) #x86_64 or aarch64
   echo '#!/bin/sh
echo "/* This was generated by the program typegen */
#ifndef INTTYPES_H_NETPBM
#define INTTYPES_H_NETPBM
typedef unsigned int uint32_t;
typedef signed int int32_t;
typedef signed int int_fast32_t;
typedef unsigned int uint_fast32_t;
#endif"' > buildtools/typegen
  ;;
  *)
   echo '#!/bin/sh
echo "/* This was generated by the program typegen */
#ifndef INTTYPES_H_NETPBM
#define INTTYPES_H_NETPBM
typedef unsigned long uint32_t;
typedef signed long int32_t;
typedef signed long int_fast32_t;
typedef unsigned long uint_fast32_t;
#endif"' > buildtools/typegen
  ;;
 esac
 chmod 755 buildtools/typegen
 touch buildtools/typegen
 
 #20210103 have x86_64 libopt binary (compiled in easyos buster)...
 cp -f ${WORKDIR}/libopt buildtools/
 chmod 755 buildtools/libopt
 touch buildtools/libopt

}


do_compile () {
 oe_runmake
}

do_install () {
    #this will install into folder /tmp/netpbm...
    if [ -d /tmp/netpbm ];then
     rm -rf /tmp/netpbm
    fi
    oe_runmake package
    install -d ${D}/usr/bin
    #install -D ${WORKDIR}/out-netpbm/bin/* ${D}/usr/bin
    install -d ${D}/usr/lib
    #install -D ${WORKDIR}/out-netpbm/lib/* ${D}/usr/lib
    install -d ${D}/usr/share/netpbm
    #install -D ${WORKDIR}/out-netpbm/misc/* ${D}/usr/share/netpbm
    #...crap, converts symlinks into files.
    cp -a -f /tmp/netpbm/bin/* ${D}/usr/bin/
    cp -a -f /tmp/netpbm/lib/* ${D}/usr/lib/
    cp -a -f /tmp/netpbm/misc/* ${D}/usr/share/netpbm/
}

#ERROR: netpbm-10.73.33-r0 do_package_qa: QA Issue: /usr/bin/pnmtotiffcmyk contained in package netpbm requires libtiff.so.5(LIBTIFF_4.0)(64bit), but no providers found in RDEPENDS_netpbm? [file-rdeps]
#...well, that lib does exist!
ERROR_QA_remove = "file-rdeps"

RDEPENDS_netpbm += "perl"

HOMEPAGE = "http://sourceforge.net/projects/netpbm"
SUMMARY = "Collection of primitive graphics tools converters etc"
