# -*- tcl -*-
# $Id: Portfile 62735 2010-01-15 11:18:27Z nox@macports.org $

PortSystem 1.0

name            freeimage
version         3.13.1
license         FreeImage-1.0 GPL-2
categories      graphics
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!
platforms       macosx
homepage        http://freeimage.sourceforge.net/
master_sites    sourceforge
distname        FreeImage[strsed ${version} {g/\.//}]
use_zip         yes
worksrcdir      FreeImage

checksums       md5     a2e20b223a2cf6a5791cc47686364e99 \
                sha1    52ba4453aa9682c57104c3420e58f843aaa6ab61 \
                rmd160  b3f3e7791ded7d3ce76eb2d9c2a5acb5687a16ee

patchfiles      patch-Makefile.gnu.diff

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
}

use_configure   no

set CC "${configure.cc} ${configure.cflags}"
set CXX "${configure.cxx} ${configure.cxxflags}"
set LDFLAGS ${configure.ldflags}
set VERLIBNAME libfreeimage.[lindex [split ${version} .] 0].dylib

build.target    FreeImage
build.args      -f Makefile.gnu \
                PREFIX=${prefix} \
                VERLIBNAME=${VERLIBNAME}

pre-build {
    build.args-append \
        CC="${CC}" \
        CXX="${CXX}" \
        LDFLAGS="${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++:${CXX}: ${test.dir}/Makefile

    # Use headers and libraries in source directories as Dist is not used
    reinplace s:-I../Dist/:-I../Source/: ${test.dir}/Makefile
    reinplace s:Dist/:: ${test.dir}/Makefile

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

variant universal {
    append CC " ${configure.universal_cflags}"
    append CXX " ${configure.universal_cxxflags}"
    append LDFLAGS " ${configure.universal_ldflags}"
}

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