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

name            ngspice
version         36
revision        0
license         BSD
categories      science cad
maintainers     nomaintainer
description     Circuit simulator based on Spice 3f5, XSpice and others
long_description \
                Ngspice is a mixed-level/mixed-signal circuit simulator. Its \
                code is based on three open source software packages: Spice3f5, \
                Cider1b1 and Xspice. Ngspice is part of gEDA project, a full \
                suite of Electronic Design Automation tools.

homepage        http://ngspice.sourceforge.net/
master_sites    sourceforge:project/ngspice/ng-spice-rework/${version}

checksums       rmd160  094d76e789b65d10e7d80f2f2f90397ed0ec13c9 \
                sha256  4f818287efba245341046635b757ae81f879549b326a4316b5f6e697aa517f8c \
                size    8450071

set docdir      ${prefix}/share/doc/${name}

if {${name} eq ${subport}} {

    # freetype2 headers are not found by default
    # we could fix the header path in configure.ac, but then need autorecconf, etc
    configure.cppflags-prepend -I${prefix}/include/freetype2

    # TASK_BASIC_INFO_COUNT and friends were renamed in 10.8
    patchfiles-append   patch-ngspice-older-MACH-defines.diff

    depends_lib-append  port:ncurses \
                        port:libedit \
                        port:xorg-libX11 \
                        port:xorg-libXaw \
                        port:xorg-libXext \
                        port:xorg-libXmu \
                        port:xorg-libXt \
                        port:fftw-3

    configure.args      --enable-cider \
                        --enable-xspice \
                        --enable-pss \
                        --with-editline \
                        --with-x \
                        --disable-debug \
                        --disable-silent-rules

    post-destroot {
        xinstall -d ${destroot}${docdir}
        xinstall -m 644 -W ${worksrcpath} \
            ANALYSES \
            AUTHORS \
            BUGS \
            COPYING \
            ChangeLog \
            DEVICES \
            FAQ \
            NEWS \
            README \
            Stuarts_Poly_Notes \
            ${destroot}${docdir}
    }

    variant manual description {Legacy compatibility variant} {
        depends_run-append       port:ngspice-docs
    }

    variant lib description {Enable dynamic lib} {
        depends_run-append       port:ngspice-lib
    }

    variant openmp description {Add OpenMP support variant} {
        # ngspice does not specify what openmp version they require
        # version 2.5 is the least strict, and works
        compiler.openmp_version  2.5
        configure.args-append    --enable-openmp

        # see https://trac.macports.org/ticket/64511
        # not too much sure
        configure.cflags-append "-mcmodel=large"
        configure.cxxflags-append "-mcmodel=large"
        configure.ldflags-append "-mcmodel=large"
    }

    default_variants    +manual +lib
    
    livecheck.regex     ${name}-(\[0-9.\]+)${extract.suffix}

    # running the tests from MacPorts errors early, probably due to a permissions
    # issue when calling the X11 display subsystem. Running the tests manually from the
    # worksrcdir works fine, however
    # test.run            yes
    # test.target         check

} else {
    livecheck.type      none
}

subport ngspice-docs {
    supported_archs     noarch
    platforms           any
    description         PDF manual for ngspice
    long_description    {*}${description}
    
    distname            ${name}-${version}-manual.pdf
    extract.suffix
    extract.only

    checksums           rmd160  8c15aab0859fbf6540e3e2aa06f486e46ed13397 \
                        sha256  f9f308ddcd11ea9708cdcf74894c28befc4d1dd8f00edbcb76a5706daf36cdbc \
                        size    2397103

    use_configure       no

    build {}
    
    destroot {
        xinstall -d ${destroot}${docdir}
        xinstall -m 644 ${distpath}/${distname} ${destroot}${docdir}
    }
}

subport ngspice-lib {
    description         ngspice dynamic library
    long_description    {*}${description}

    # freetype2 headers are not found by default
    # we could fix the header path in configure.ac, but then need autorecconf, etc
    configure.cppflags-prepend -I${prefix}/include/freetype2

    # TASK_BASIC_INFO_COUNT and friends were renamed in 10.8
    patchfiles-append   patch-ngspice-older-MACH-defines.diff

    depends_lib-append  port:libedit \
                        port:fftw-3

    configure.args      --enable-cider \
                        --enable-pss \
                        --with-editline \
                        --disable-debug \
                        --disable-silent-rules \
                        --with-ngshared

    destroot {
        xinstall -d ${destroot}${prefix}/lib
        xinstall -m 755 -W ${worksrcpath}/src/.libs \
            libngspice.0.dylib \
            ${destroot}${prefix}/lib
        ln -s ${prefix}/lib/libngspice.0.dylib \
            ${destroot}${prefix}/lib/libngspice.dylib
        xinstall -d ${destroot}${prefix}/lib/pkgconfig
        xinstall -m 644 -W ${worksrcpath} \
            ngspice.pc \
            ${destroot}${prefix}/lib/pkgconfig
        xinstall -d ${destroot}${prefix}/include/ngspice
        xinstall -m 644 -W ${worksrcpath} \
            src/include/ngspice/sharedspice.h \
            ${destroot}${prefix}/include/ngspice
    }
}
