# -*- 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 84e5657d9279ad7ea01fb48a46b3c52cfc6f3950
name                    netpbm
version                 11.10.05
svn.revision            5096
set source_branch       advanced
distname                ${source_branch}-r${svn.revision}
set source_distfile     ${distfiles}

set userguide_commit    aebdb0411d99d4b933cf7c72f81a0c5c2b0b8d6d
set userguide_revision  5079
set userguide_branch    userguide
set userguide_distname  ${userguide_branch}-r${userguide_revision}
set userguide_distfile  ${userguide_distname}${extract.suffix}

checksums               ${source_distfile} \
                        rmd160  e294b04bc63e6bb217c1635589eb2f06b2c63455 \
                        sha256  29a98f9aae6eb511a79c96f274e1c2d1146a8245542bd42e9d2bce8e4e70a6e4 \
                        size    3023221 \
                        ${userguide_distfile} \
                        rmd160  15fa1dfd9fbe0a5d59b252412cdae69147ecd86f \
                        sha256  f2a0e1e35faeb06d9441e8f5c1075bccba63ed13b55276e21081ecb063712f36 \
                        size    1823747

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

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 \
                        manpage.mk.patch \
                        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

destroot.target

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

destroot.destdir        pkgdir=${destroot}${prefix}

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

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

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

    post-patch {
        # Replace path to gs, used only when the GHOSTSCRIPT environment variable
        # isn't set and when gs is not found in PATH.
        reinplace -E "s|/usr(/bin/gs)|${prefix}\\1|g" \
            ${worksrcpath}/converter/other/pstopnm.c \
            ${workpath}/${userguide_distname}/pstopnm.html
    }

    # 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.
    destroot.target-append \
                        install.bin
    # Install data files.
    destroot.target-append \
                        install.data

    test.run            yes
    test.target         check
    test.args           ${destroot.destdir} \
                        RGBDEF=${prefix}/share/netpbm/rgb.txt \
                        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    path:bin/pkg-config: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.
    destroot.target-append \
                        install-dev
    # Install rgb.txt data file.
    destroot.target-append \
                        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

set python_branch       3.13
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
}
