# -*- 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 119199 2014-04-20 04:03:10Z mcalhoun@macports.org $

PortSystem          1.0

name                freeimage
version             3.16.0
revision            1
set major           [lindex [split ${version} .] 0]
categories          graphics
platforms           macosx
license             {FreeImage-1 GPL-2 GPL-3}

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:project/freeimage/Source%20Distribution/${version}

distname            FreeImage[strsed ${version} {g/\.//}]
use_zip             yes
worksrcdir          FreeImage

checksums           rmd160  f0dcfb1514a288bbb2bc182130469b91d575e6cd \
                    sha256  03af32dd60140d07515ddfe7b4f467baa3b888a72ea78828811aded7adb42f60

patchfiles          patch-Makefile.fip.diff

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

    # A dot separates the library name from its version on Darwin
    reinplace /^SHAREDLIB/s/-/./ ${worksrcpath}/Makefile.gnu \
        ${worksrcpath}/Makefile.fip

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

    # Do not force installation as root
    reinplace {s/-o root -g root//} ${worksrcpath}/Makefile.gnu \
        ${worksrcpath}/Makefile.fip

    # 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 \
        ${worksrcpath}/Makefile.fip

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

use_configure       no

# In the future, implement in the build phase?
#configure.pipe      no
#configure.ccache    no
#configure.distcc    no

# FreeImage uses -O3, which is set in the Makefiles
configure.optflags-delete -Os

variant universal {}

# Reimplement parts of portconfigure.tcl
if {[info exists configure.cxx_stdlib] &&
    ${configure.cxx_stdlib} ne {} &&
    [string match *clang* ${configure.cxx}]
} then {
    configure.cxxflags-append -stdlib=${configure.cxx_stdlib}
    configure.ldflags-append  -stdlib=${configure.cxx_stdlib}
}

if {${configure.sdkroot} ne ""} {
    configure.ldflags-append "-Wl,-syslibroot,${configure.sdkroot}"
}

foreach flags {cflags cxxflags} compiler {cc cxx} {
    eval configure.${flags}-append [get_canonical_archflags ${compiler}]
    
    if {${configure.sdkroot} ne ""} {
        configure.${flags}-append -isysroot${configure.sdkroot}
    }
    
    if {${configure.march} ne "" } {
        configure.${flags}-append "-march=${configure.march}"
    }
    
    if {${configure.mtune} ne "" } {
        configure.${flags}-append "-mtune=${configure.mtune}"
    }
}

# running build_main and destroot_main makes use of environmental variables like MACOSX_DEPLOYMENT_TARGET

build {
    foreach makefile {Makefile.gnu Makefile.fip} libname {libfreeimage libfreeimageplus} {
        # Makefile overrides CFLAGS and CXXFLAGS, so append options to CC and CXX
        build.args -f ${makefile} \
            PREFIX=${prefix} \
            VERLIBNAME=${libname}.${major}.dylib \
            CC="${configure.cc} ${configure.cflags}" \
            CXX="${configure.cxx} ${configure.cxxflags}" \
            LDFLAGS="${configure.ldflags}"
        
        portbuild::build_main
    }
}

destroot {
    foreach makefile {Makefile.gnu Makefile.fip} libname {libfreeimage libfreeimageplus} {
        destroot.args -f ${makefile} \
            PREFIX="${prefix}" \
            VERLIBNAME=${libname}.${major}.dylib \
            INCDIR="${destroot}${prefix}/include" \
            INSTALLDIR="${destroot}${prefix}/lib"
        
        portdestroot::destroot_main
    }
}

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

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

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

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