# -*- 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
# $Id: Portfile 79850 2011-06-28 03:58:49Z ryandesign@macports.org $

PortSystem          1.0

name                freeimage
version             3.15.0
revision            1
license             FreeImage-1.0 GPL-2
categories          graphics
platforms           macosx

maintainers         nomaintainer

description         Library for FreeImage, a dependency-less graphics library
long_description    FreeImage is a library for developers who would like to \
                    support most popular graphics image formats. Some \
                    highlights are: extremely simple in use, not limited to \
                    the local PC (unique FreeImageIO) and Plugin driven!

homepage            http://freeimage.sourceforge.net/
master_sites        sourceforge
distname            FreeImage[strsed ${version} {g/\.//}]
use_zip             yes
worksrcdir          FreeImage

checksums           md5     3b4f08e4985b269beb29a2fced1ef888 \
                    sha1    ec2faa01d1b312aab68d77afe62861759c790100 \
                    rmd160  5d309d0d8335129d00d6b5455613c89a419ebcac

set VERLIBNAME libfreeimage.[lindex [split ${version} .] 0].dylib

post-patch {
    # Libraries extension is ".dylib" on Darwin, not ".so"
    reinplace {s/\.so/.dylib/} ${worksrcpath}/Makefile.gnu

    # A dash separate the library name from its version on Darwin
    reinplace /^SHAREDLIB/s/-/./ ${worksrcpath}/Makefile.gnu

    # Use libtool(1) instead of ar(1)
    reinplace {s/\$(AR) r/libtool -o/} ${worksrcpath}/Makefile.gnu

    # Do not force installation as root
    reinplace {s/-o root -g root//} ${worksrcpath}/Makefile.gnu
    
    # Darwin requires different arguments to build dynamic libraries
    reinplace "s|-Wl,-soname,\$(VERLIBNAME)|-dynamiclib -install_name \$(PREFIX)/lib/\$(VERLIBNAME) -compatibility_version \$(VER_MAJOR) -current_version \$(VER_MAJOR).\$(VER_MINOR)|g" ${worksrcpath}/Makefile.gnu

    # Darwin does not have a command ldconfig
    reinplace "s|ldconfig||g" ${worksrcpath}/Makefile.gnu
}

use_configure       no

variant universal {}
if {[variant_isset universal]} {
    set archflags ${configure.universal_cflags}
} else {
    set archflags ${configure.cc_archflags}
}

build.args          -f Makefile.gnu \
                    PREFIX=${prefix} \
                    VERLIBNAME=${VERLIBNAME} \
                    CC="${configure.cc} ${configure.cflags} ${archflags}" \
                    CXX="${configure.cxx} ${configure.cxxflags} ${archflags}" \
                    LDFLAGS="${configure.ldflags}"

destroot.args       -f Makefile.gnu \
                    INCDIR=${destroot}${prefix}/include \
                    INSTALLDIR=${destroot}${prefix}/lib \
                    PREFIX=${prefix} \
                    VERLIBNAME=${VERLIBNAME}

post-destroot {
    set docdir ${prefix}/share/doc/${name}
    xinstall -d ${destroot}${docdir}
    xinstall -m 644 -W ${worksrcpath} Whatsnew.txt license-fi.txt \
        license-gpl.txt ${destroot}${docdir}
    copy ${worksrcpath}/Examples ${destroot}${docdir}/examples
}

test.run            yes
test.cmd            ./testAPI
test.dir            ${worksrcpath}/TestAPI
test.target

pre-test {
    # Use correct compiler and flags when compiling test
    reinplace "s:g++:${configure.cxx} ${configure.cxxflags} ${archflags}:" ${test.dir}/Makefile

    # Build the test
    system "make -C ${test.dir}"
}

livecheck.type      regex
livecheck.regex     {FreeImage (\d+(?:\.\d+)*) released}
