# -*- 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         44.2
revision        0
license         BSD
categories      science cad
maintainers     {gmail.com:degnan.68k @bpdegnan} \
                {mark @markemer} \
                openmaintainer

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/

distname        ${name}-${version}
master_sites    https://sourceforge.net/projects/ngspice/files/ng-spice-rework/${version}

checksums       rmd160  4194feaa1163de2bad9459d79be0a47ea0b4b4ec \
                sha256  e7dadfb7bd5474fd22409c1e5a67acdec19f77e597df68e17c5549bc1390d7fd \
                size    11198335

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

#this strips the .x from 4.x 
set major_version [string range ${version} 0 [expr {[string first "." ${version}]-1}]]


# checking whether C compiler accepts -std=gnu11... no
# C compiler cannot compile C11 code, etc.
compiler.c_standard     2011
compiler.cxx_standard   2011

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

    # freetype2 headers are not found by default
    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

    # https://trac.macports.org/ticket/70175
    patchfiles-append   patch-fix-linking.diff

    depends_lib-append  port:readline \
                        port:libtool \
                        port:xorg-libX11 \
                        port:xorg-libXaw \
                        port:xorg-libXext

    configure.args      --enable-cider \
                        --enable-xspice \
                        --enable-pss \
                        --disable-openmp \
                        --disable-silent-rules \
                        --with-readline=${prefix} \
                        --with-x

    # https://trac.macports.org/ticket/70665
    # https://github.com/macports/macports-ports/commit/e485b0a0c8125cb9d8bcc6241a9c2016514188c8
    if {${os.platform} eq "darwin" && ${os.major} <= 10} {
        configure.cppflags-append \
                                -D__NOEXTENSIONS__

        # Do not let an arbitrary compiler be set as CC_FOR_BUILD,
        # do not link C++ code with gcc.
        patchfiles-append       patch-use-cxx-to-link-cxx-code.diff
        configure.env-append    CC_FOR_BUILD=${configure.cc}
    }

    # Set CFLAGS and LDFLAGS
    configure.ldflags-append -L${prefix}/lib

    build.args          VERBOSE=1
    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
    }

    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
    description         PDF manual for ngspice
    long_description    {*}${description}

    distname            ${name}-${major_version}-manual.pdf
    extract.suffix
    extract.only

    checksums           rmd160  a6cf586696a6307df9be3d40091b355829b35e90 \
                        sha256  ab302f45ce5f866279638c8e7a4fb8e6a3b8b67cfbfd76d7951ccc4f636ce83e \
                        size    2575573

    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
    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:autoconf \
                        port:automake \
                        port:bison \
                        port:flex \
                        port:gawk \
                        port:readline \
                        port:libtool

    configure.args      --enable-cider \
                        --enable-xspice \
                        --enable-pss \
                        --enable-relpath \
                        --disable-openmp \
                        --with-readline=${prefix} \
                        --with-ngshared

    # https://trac.macports.org/ticket/70665
    # https://github.com/macports/macports-ports/commit/e485b0a0c8125cb9d8bcc6241a9c2016514188c8
    if {${os.platform} eq "darwin" && ${os.major} <= 10} {
        configure.cppflags-append \
                                -D__NOEXTENSIONS__
    }

    # Set CFLAGS and LDFLAGS
    configure.cflags-append     -Wall -I${prefix}/include/freetype2

    # Use the correct prefix
    use_configure       yes
    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
    }
}
