#-*- 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           xcode 1.0

name                activewire
version             4.0
categories          devel
license             GPL-2+
platforms           darwin
maintainers         nomaintainer

description         Driver for the ActiveWire board

long_description    An independent project focusing on the development \
                    of drivers for the ActiveWire board on Mac OS X. \
                    The REALbasic Plugin not included.

homepage            https://sourceforge.net/projects/activewire-osx/
master_sites        sourceforge:project/activewire-osx/activewire-osx/${version}

distname            aw_${version}

checksums           rmd160  11740b6f835a2ebfbaea14f12dcec921a8a9a03b \
                    sha256  1af16eaa1a0b7b70d75cad5e21f023e0efa5aba928bf106d25bacbac8eaa4a61 \
                    size    1126950

worksrcdir          ${distname}/Source/
use_dmg             yes

xcode.configuration Default

default_variants    +doc \
                    +dev

variant awdriver description {Install ActiveWire driver} {}
variant awconfig description {Install awconfig utility} {}
variant doc description {Install documentation} {}
variant dev description {Install header files for development} {}

# variant realbasic description {Install REALbasic plugin} {}

patchfiles          patch-Shared-aw_sock.h.diff \
                    patch-Shared-libCdsaCrypt-libCdsaCrypt.cpp.diff

pre-configure {
    foreach i {libaw awdriver awconfig} {
        reinplace "s|/usr/local|${prefix}|g" "${worksrcpath}/$i/$i.xcodeproj/project.pbxproj"
        reinplace "s|/usr/share/man/man1/|${prefix}/include|g" "${worksrcpath}/$i/$i.xcodeproj/project.pbxproj"
        if {${configure.sdkroot} == ""} {
            reinplace "\\|/Developer/SDKs/MacOSX10.4u.sdk|d" "${worksrcpath}/$i/$i.xcodeproj/project.pbxproj"
        } else {
            reinplace "s|/Developer/SDKs/MacOSX10.4u.sdk|${configure.sdkroot}|g" "${worksrcpath}/$i/$i.xcodeproj/project.pbxproj"
        }
    }
}

build {
    set xcode_build_args [xcode::get_build_args]

    foreach i {libaw awdriver awconfig} {
        if {[variant_isset $i] || $i == "libaw"} {
            set command "cd '${worksrcpath}/$i' && ${build.cmd} ${xcode_build_args}"
            ui_debug "Assembled Command: ${command}"
            system ${command}
        }
    }
}

destroot {
    xinstall -m 0755 -d ${destroot}${prefix}/share/activewire
    xinstall -m 0755 -d ${destroot}${prefix}/share/doc/activewire

    set xcode_build_args [xcode::get_build_args]

    foreach {target dest} {libaw lib awconfig bin awdriver bin} {
        set pre_command "cd '${worksrcpath}/${target}' && ${build.cmd} \
            ${xcode_build_args} install DSTROOT=${destroot} \
            INSTALL_PATH=${prefix}"

        if {[variant_isset $target] || $target == "libaw"} {
            set command "${pre_command}/${dest}"
            ui_debug "Assembled Command: ${command}"
            system ${command}
        }
    }

    if {[variant_isset doc]} {
        set doc_path "${worksrcpath}/../Documentation/"
        foreach i [glob -tails -directory  $doc_path *] {
            file copy $doc_path/$i "${destroot}${prefix}/share/doc/activewire"
        }

        set example_path "${worksrcpath}/../Examples"
        system "cp -r '${example_path}/C Example' ${destroot}${prefix}/share/doc/activewire"
        system "cp -r '${example_path}/Cocoa Example' ${destroot}${prefix}/share/doc/activewire"
    }
}

livecheck.regex     "/activewire-osx/(\\d+(?:\.\\d+)*)"
