# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem              1.0
PortGroup               github 1.0

# Download distfiles from GitHub mirror because fetching from svn keeps being
# problematic.
github.setup            leahneukirchen netpbm-mirror 19387d1be30e80e8c4e865bfe053aed1094ca8af
name                    netpbm
version                 11.05.03
svn.revision            4867
set source_branch       advanced
distname                ${source_branch}-r${svn.revision}
set source_distfile     ${distfiles}

set userguide_commit    6b76a2a4c4ca084113a87c1e7e8376609a722626
set userguide_revision  4860
set userguide_branch    userguide
set userguide_distname  ${userguide_branch}-r${userguide_revision}
set userguide_distfile  ${userguide_distname}${extract.suffix}

checksums               ${source_distfile} \
                        rmd160  1998656641b75b05888326ffc14e6bb6a00a83df \
                        sha256  3d098112c776f4bf23078ea08f6bc49f690f75abdf238eb65471657f5c874f78 \
                        size    2991147 \
                        ${userguide_distfile} \
                        rmd160  f7a8750c531e8259128c1e0549b455efd0aa6462 \
                        sha256  f846e4a226a35924fffe01050177fb4959a21020f9ac067687ede05424071a12 \
                        size    1820520

categories              graphics
platforms               darwin freebsd linux
maintainers             {ryandesign @ryandesign} openmaintainer
license                 {BSD GPL-2 IJG Permissive}

description             Image manipulation

long_description        A whole bunch of utilities for primitive manipulation \
                        of graphic images. Wide array of converters from one \
                        graphics format to another, e.g. from g3 fax format to \
                        jpeg. Many basic graphics editing tools such as \
                        magnifying and cropping.

homepage                http://netpbm.sourceforge.net
github.tarball_from     archive

distfiles               ${source_distfile}:source \
                        ${userguide_distfile}:userguide

master_sites            ${github.master_sites}:source \
                        ${github.homepage}/archive/${userguide_commit}:userguide

if {[exists extract.rename]} {
    # set by the github portgroup, doesn't work with multiple dirs
    extract.rename      no
}
post-extract {
    move ${workpath}/${github.project}-${git.branch} ${workpath}/${distname}
    move ${workpath}/${github.project}-${userguide_commit} ${workpath}/${userguide_distname}

    set newest_mtime 0
    fs-traverse item ${worksrcpath} {
        if {[file isfile ${item}]} {
            set item_mtime [file mtime ${item}]
            if {${item_mtime} > ${newest_mtime}} {
                set newest_mtime ${item_mtime}
            }
        }
    }
    set fp [open ${workpath}/mtime w]
    puts ${fp} ${newest_mtime}
    close ${fp}

    # Delete the manpages that aren't for this subport.
    set libmanpages {libnetpbm pam pbm pgm pnm ppm}
    foreach f [glob ${workpath}/${userguide_distname}/*.html] {
        if {([lsearch -exact ${libmanpages} [file rootname [file tail ${f}]]] == -1) == (${subport} ne ${name})} {
            delete ${f}
        }
    }
}

patchfiles              patch-lib-Makefile.diff \
                        patch-other-pamx-Makefile.diff

variant universal {}

configure.cflags-append -fno-common -D_DARWIN_C_SOURCE

set jasperlib           -ljasper
set ldreloc             NONE
set ldshlib             {-shared -Wl,-soname,$(SONAME)}
set linker_can_do_explicit_library \
                        N
set netpbmlibtype       unixshared
set netpbmlibsuffix     so
set want_sse            N
set want_x11            N

configure {
    # The netpbm configure script is interactive, and appends its results to
    # the config.mk file; doc/INSTALL recommends packagers do that manually.
    copy -force ${worksrcpath}/config.mk.in ${worksrcpath}/config.mk
    set configmk [open ${worksrcpath}/config.mk a]
    puts ${configmk} "
CC = ${configure.cc}
CFLAGS = ${configure.cflags} ${configure.cppflags} [get_canonical_archflags cc]
CFLAGS_CONFIG = \$(CFLAGS)
JASPERHDR_DIR = ${prefix}/include
JASPERLIB = ${jasperlib}
JBIGHDR_DIR = ${prefix}/include
JBIGLIB = -ljbig
JPEGLIB = -ljpeg
LDFLAGS = ${configure.ldflags} [get_canonical_archflags ld]
LDRELOC = ${ldreloc}
LDSHLIB = ${ldshlib}
LINKER_CAN_DO_EXPLICIT_LIBRARY = ${linker_can_do_explicit_library}
NETPBMLIB_RUNTIME_PATH = ${prefix}/lib
NETPBMLIBTYPE = ${netpbmlibtype}
NETPBMLIBSUFFIX = ${netpbmlibsuffix}
PKGMANDIR = share/man
PNGLIB = -lpng
RGB_DB_PATH = ${prefix}/share/${name}/rgb.txt
TIFFLIB = -ltiff
WANT_SSE = ${want_sse}
WANT_X11 = ${want_x11}
ZLIB = -lz
"
    close ${configmk}
}

platform darwin {
    set ldshlib {$(LDFLAGS) -dynamiclib -install_name $(NETPBMLIB_RUNTIME_PATH)/libnetpbm.$(MAJ).$(NETPBMLIBSUFFIX) -compatibility_version $(MAJ) -current_version $(MAJ).$(MIN).$(NETPBM_POINT_RELEASE)}
    set netpbmlibtype dylib
    set netpbmlibsuffix dylib
}

# Non-Darwin platforms are untested.
platform linux {
    set ldreloc {ld --reloc}
    set linker_can_do_explicit_library Y
}

platform freebsd {
    depends_build-append port:libtool

    patchfiles-append patch-libopt.c.diff

    set jasperlib ${prefix}/lib/libjasper.la
    set ldshlib "${prefix}/lib/glibtool --mode=link gcc"
}

pre-build {
    set fp [open ${workpath}/mtime r]
    set mtime [read -nonewline ${fp}]
    close ${fp}
    build.env-append    SOURCE_DATE_EPOCH=${mtime}
}

build.args              messages=yes
build.type              gnu

set pkgtargets          {}

post-build {
    # Build manpages from html files.
    system -W ${workpath}/${userguide_distname} "${build.cmd} MAKEMAN='${worksrcpath}/buildtools/makeman -v' USERGUIDE=. -f ${worksrcpath}/buildtools/manpage.mk manpages"

    # The tests can only be run after things have been destrooted ("packaged").
    system -W ${worksrcpath} "${build.cmd} ${pkgtargets} pkgdir=${destroot}${prefix}"
}

destroot {
    # Install manpages.
    system -W ${workpath}/${userguide_distname} "${build.cmd} MANDIR=${destroot}${prefix}/share/man USERGUIDE=. -f ${worksrcpath}/buildtools/manpage.mk installman"

    move ${destroot}${prefix}/misc ${destroot}${prefix}/share/${name}

    # Fix ownership of files installed during the build phase.
    # Not using fs-traverse to call file attributes because that doesn't
    # handle symlinks. Not using chown proc because that doesn't support
    # changing the group: https://trac.macports.org/ticket/55332
    #chown ${destroot} ${install.user} ${install.group}
    system "chown -R '${install.user}':'${install.group}' ${destroot}"
}

if {${subport} eq ${name}} {
    revision            0
    installs_libs       no

    # TODO: Don't build libnetpbm again in netpbm subport.

    depends_lib-append  port:jasper \
                        port:jbigkit \
                        path:include/turbojpeg.h:libjpeg-turbo \
                        port:libnetpbm \
                        port:libpng \
                        port:libxml2 \
                        port:tiff \
                        port:zlib

    # Remove library from subdirs, leaving only the programs.
    #patchfiles-append   patch-${subport}-GNUmakefile.diff

    # GNUmakefile warns that parallel build dependencies are only declared correctly
    # when using the "all" target, but the above patch is enough to change that.
    #use_parallel_build  no

    # Install programs.
    lappend pkgtargets  install.bin
    # Install data files.
    lappend pkgtargets  install.data

    test.run            yes
    test.target         check
    test.args           pkgdir=${destroot}${prefix} \
                        resultdir=${workpath}/test \
                        tmpdir=${workpath}/.tmp

    post-destroot {
        # Delete files installed by the libnetpbm subport.
        delete ${destroot}${prefix}/share/${name}/rgb.txt

        # Install documentation.
        set docdir ${prefix}/share/doc/${subport}
        xinstall -d ${destroot}${docdir}
        xinstall -m 0644 -W ${worksrcpath} \
            README \
            doc/COPYRIGHT.PATENT \
            doc/GPL_LICENSE.txt \
            doc/HISTORY \
            doc/USERDOC \
            doc/copyright_summary \
            doc/lgpl_v21.txt \
            doc/patent_summary \
            ${destroot}${docdir}
    }

    variant x11 description {Build the pamx image viewer} {
        global want_x11
        set want_x11            Y
        depends_build-append    port:pkgconfig
        depends_lib-append      port:xorg-libX11
    }

    default_variants    +x11

    github.livecheck.branch ${source_branch}
} else {
    livecheck.type      none
}

subport libnetpbm {
    revision            0
    license             Permissive

    # TODO: descriptions

    # Remove programs from subdirs, leaving only the library.
    #patchfiles-append   patch-${subport}-GNUmakefile.diff

    build.target        lib/all

    # Install libraries and headers.
    lappend pkgtargets  install-dev
    # Install rgb.txt data file.
    lappend pkgtargets  lib/install.data

    post-build {
        # Build netpbm-config and netpbm.pc like installnetpbm.pl would.
        copy ${worksrcpath}/buildtools/config_template ${build.dir}/netpbm-config
        copy ${worksrcpath}/buildtools/pkgconfig_template ${build.dir}/netpbm.pc
        set files [list ${build.dir}/netpbm-config ${build.dir}/netpbm.pc]
        reinplace -q "/^@/d" {*}${files}
        reinplace -q "s|@BINDIR@|${prefix}/bin|g" {*}${files}
        reinplace -q "s|@DATADIR@|${prefix}/share/${name}|g" {*}${files}
        reinplace -q "s|@INCLUDEDIR@|${prefix}/include|g" {*}${files}
        reinplace -q "s|@LIBDIR@|${prefix}/lib|g" {*}${files}
        reinplace -q "s|@LINKDIR@|${prefix}/lib|g" {*}${files}
        reinplace -q "s|@MANDIR@|${prefix}/share/man|g" {*}${files}
        reinplace -q "s|@VERSION@|${version}|g" {*}${files}
    }

    post-destroot {
        # Install netpbm-config and netpbm.pc.
        xinstall -m 0755 ${build.dir}/netpbm-config ${destroot}${prefix}/bin/
        xinstall -m 0644 ${build.dir}/netpbm.pc ${destroot}${prefix}/lib/pkgconfig/

        # Add symlinks for old library names that some old software still uses.
        set new_library [file readlink ${destroot}${prefix}/lib/libnetpbm.${netpbmlibsuffix}]
        foreach old_library {pbm pgm pnm ppm} {
            ln -s ${new_library} ${destroot}${prefix}/lib/lib${old_library}.${netpbmlibsuffix}
        }
    }
}

# work around bug in Apple's gcc build 4061
platform darwin 8 {
    depends_build-append port:gmake
    build.cmd ${prefix}/bin/gmake
    use_parallel_build  no
    pre-configure {
        set fl [open "| ${configure.cc} --version"]
        set data [read $fl]
        close $fl
        if {[regexp "build 4061" ${data}]} {
            ui_msg "On Mac OS X ${macos_version}, ${name} @${version} does not work with gcc version \"${data}\"."
            return -code error "incompatible gcc version"
        }
    }
}

patchfiles-append       patch-python.diff
patchfiles-append       makeman.patch

set python_branch       3.12
set python_version      [string map {. {}} ${python_branch}]

configure.python        ${prefix}/bin/python${python_branch}

depends_build-append    port:python${python_version}
post-patch {
    reinplace \
        "s|__MACPORTS_PYTHON__|${configure.python}|g" \
        ${worksrcpath}/buildtools/makeman \
        ${worksrcpath}/buildtools/manpage.mk
}
