# -*- 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            expect
conflicts       bahamut whois
version         5.45.4
revision        3
categories      devel
license         Tcl/Tk
maintainers     nomaintainer
description     An automating and testing tool
long_description    Expect is a tool for automating interactive applications \
            such as telnet, ftp, passwd, fsck, rlogin, tip, etc. \
            Expect is also useful for testing these same applications. \
            And by adding Tk, you can also wrap interactive \
            applications in X11 GUIs.
homepage        http://expect.sourceforge.net/

# Set tclv and md5 checksum to the current MacPorts Tcl port version, though port doesn't
# break if they aren't in sync.

set tclv        8.6.10

master_sites    sourceforge:project/expect/Expect/${version}:exp \
                sourceforge:project/tcl/Tcl/${tclv}:tcl

dist_subdir     tcltk
distname        ${name}${version}

distfiles       ${distname}.tar.gz:exp \
                tcl${tclv}-src.tar.gz:tcl

checksums       ${distname}.tar.gz \
                rmd160  3463e841a451581e289aa59aa87dad2e3195eaa4 \
                sha256  49a7da83b0bdd9f46d04a04deec19c7767bb9a323e40c4781f89caf760b92c34 \
                size    632363 \
                tcl${tclv}-src.tar.gz \
                rmd160  e44c76e48b77c825dc6fb7a78ef8f7297106784a \
                sha256  5196dbf6638e3df8d5c87b5815c8c2b758496eb6f0e41446596c9a4e638d87ed \
                size    10144235

depends_lib     port:tcl

patchfiles      patch-implicitly-defined-functions.diff

post-patch {
    # For each example that gets installed in ${prefix}/bin, fix its
    # shebang (#!) line to directly run expect (why not?) to fix
    # "can't find package Expect while executing 'package require Expect'"
    foreach example {autoexpect autopasswd cryptdir decryptdir dislocate \
        ftp-rfc kibitz lpunlock mkpasswd multixterm passmass rftp \
        rlogin-cwd timed-read timed-run tknewsbiff tkpasswd unbuffer \
        weather xkibitz xpstat} {
        set file ${worksrcpath}/example/${example}
        system "patch ${file} < ${filespath}/patch-example.diff"
        reinplace "s|@PREFIX@|${prefix}|g" ${file}
    }
}

# bundled config.{guess,sub} are from 2003, update with copies from automake
depends_build-append    port:automake
post-patch {
    copy -force {*}[glob ${prefix}/share/automake-*/config.{guess,sub}] \
         ${worksrcpath}/tclconfig/
}

configure.cppflags-append -I${workpath}/tcl${tclv}/generic -I${workpath}/tcl${tclv}/unix
configure.ldflags-append -ltclstub8.6
configure.args      --mandir=${prefix}/share/man \
            --with-tcl=${prefix}/lib \
            --with-tclinclude=${workpath}/tcl${tclv}/generic/

test.run        yes
test.target     test

post-destroot       { 
            ln -s expect${version}/libexpect${version}.dylib ${destroot}${prefix}/lib/libexpect.dylib
            # Fix libexpect's install name to match its new location.
            system "install_name_tool -id ${prefix}/lib/libexpect.dylib ${destroot}${prefix}/lib/libexpect.dylib"
            # The expect binary has already been linked against the old install
            # name, so we need to fix that too.
            system "install_name_tool -change libexpect${version}.dylib ${prefix}/lib/libexpect.dylib ${destroot}${prefix}/bin/expect"

            file mkdir ${destroot}${prefix}/share/doc/${name}/examples
            xinstall -m 644 {*}[glob ${worksrcpath}/example/*] \
                ${destroot}${prefix}/share/doc/${name}/examples

            xinstall -m 644 -W ${worksrcpath} ChangeLog FAQ INSTALL NEWS README \
                ${destroot}${prefix}/share/doc/${name}
            }

livecheck.type      regex
livecheck.url       https://sourceforge.net/projects/${name}/files/
livecheck.regex     ${name}(\[0-9.\]+)${extract.suffix}
