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

name		guile18
version		1.8.8
revision    7
categories	lang
maintainers	nomaintainer
platforms	darwin
license     LGPL-2.1+

description	GNU's Ubiquitous Intelligent Language for Extension (guile)
long_description	\
		Guile is an interpreter for the Scheme programming	\
		language, packaged for use in a wide variety of		\
		environments. Guile implements Scheme as described in	\
		the Revised^5 Report on the Algorithmic Language Scheme	\
		(usually known as R5RS), providing clean and general	\
		data and control structures.				\
									\
		Guile goes beyond the rather austere language presented	\
		in R5RS, extending it with a module system, full access	\
		to POSIX system calls, networking support, multiple	\
		threads, dynamic linking, a foreign function call	\
		interface, powerful string processing, and many other	\
		features needed for programming in the real world.

#user_notes	You may need to set the environment variable		\
#		DYLD_LIBRARY_PATH to ${prefix}/lib (or wherever		\
#		the guile libraries have been installed) if you want	\
#		to load guile modules from an application linked with	\
#		libguile.  This should only apply to developers using	\
#		guile as an extension language.

distname        guile-${version}
homepage	https://www.gnu.org/software/guile/guile.html
master_sites	gnu:guile

checksums       md5     18661a8fdfef13e2fcb7651720aa53f3 \
                sha1    548d6927aeda332b117f8fc5e4e82c39a05704f9 \
                rmd160  8ac185a72ad8394b14d5377549e5bbf02889faaa

patchfiles	patch-srfi-60.c.diff \
		patch-libguile-fports.c.diff \
		patch-no-stack-checking.diff \
		endl.patch

depends_lib	port:readline \
                port:gettext \
                port:libiconv \
                port:libtool \
                port:gmp

# guile-using applications may need to set DYLD_LIBRARY_PATH to the
# guile library path so they can be loaded at runtime. They should not
# use ${prefix}/lib because this includes other MacPorts libraries that
# they may not wish to load instead. Accordingly, we install guile
# libraries into ${prefix}/lib/$libsubdir For compatibility, we symlink
# everything back into ${prefix}/lib.
# See https://trac.macports.org/ticket/52494
set libsubdir guile18

configure.args	CPPFLAGS="-I${prefix}/include"		\
		LDFLAGS="-L${prefix}/lib"		\
		--infodir="${prefix}/share/info"	\
		--mandir="${prefix}/share/man"		\
		--enable-regex                          \
		--disable-error-on-warning \
        --libdir ${prefix}/lib/${libsubdir}

post-patch {
    # This changes configure to do what the author actually intended based on their comments
    reinplace "s|-Werror -Wmissing-braces|-Werror=missing-braces|" ${worksrcpath}/configure
}

# Unable to cross compile, so we need to be able to run the built code
if {${os.arch} eq "i386" && ${os.major} >= 11} {
    supported_archs i386 x86_64
    set universal_archs_supported {i386 x86_64}
} elseif {${os.arch} eq "i386" && ${build_arch} eq "x86_64"} {
    supported_archs i386 x86_64 ppc
    set universal_archs_supported {i386 x86_64 ppc}
} elseif {${os.arch} eq "i386"} {
    supported_archs i386 ppc
    set universal_archs_supported {i386 ppc}
} elseif {${build_arch} eq "ppc64"} {
    supported_archs ppc ppc64
    set universal_archs_supported {ppc ppc64}
} else {
    supported_archs ${build_arch}
    set universal_archs_supported ${build_arch}
}

platform darwin {
    if {[variant_isset universal]} {
        set merger_host(x86_64) x86_64-apple-${os.platform}${os.major}
        set merger_host(i386) i686-apple-${os.platform}${os.major}
        set merger_host(ppc64) powerpc64-apple-${os.platform}${os.major}
        set merger_host(ppc) powerpc-apple-${os.platform}${os.major}
        set merger_configure_args(x86_64) "--build=x86_64-apple-${os.platform}${os.major}"
        set merger_configure_args(i386) "--build=i686-apple-${os.platform}${os.major}"
        set merger_configure_args(ppc) "--build=powerpc-apple-${os.platform}${os.major}"
        set merger_configure_args(ppc64) "--build=powerpc64-apple-${os.platform}${os.major}"
    } elseif {${build_arch} eq "i386"} {
        configure.args-append \
            --host=i686-apple-${os.platform}${os.major} \
            --build=i686-apple-${os.platform}${os.major}
    } elseif {${build_arch} eq "x86_64"} {
        configure.args-append \
            --host=x86_64-apple-${os.platform}${os.major} \
            --build=x86_64-apple-${os.platform}${os.major}
    } elseif {${build_arch} eq "ppc"} {
        configure.args-append \
            --host=powerpc-apple-${os.platform}${os.major} \
            --build=powerpc-apple-${os.platform}${os.major}
    } elseif {${build_arch} eq "ppc64"} {
        configure.args-append \
            --host=powerpc64-apple-${os.platform}${os.major} \
            --build=powerpc64-apple-${os.platform}${os.major}
    }
}

set merger_dont_diff "${prefix}/include/libguile/scmconfig.h"

post-destroot {
    # Clean up libsubdir redirection and provide symlinks to ${prefix}/lib
    file rename ${destroot}${prefix}/lib/${libsubdir}/pkgconfig/guile-1.8.pc ${destroot}${prefix}/lib/pkgconfig/
    file delete ${destroot}${prefix}/lib/${libsubdir}/pkgconfig

    foreach f [glob -tails -directory ${destroot}${prefix}/lib/${libsubdir} *] {
        ln -s ${libsubdir}/$f ${destroot}${prefix}/lib
    }



    # Remove conflicting files and rename to make guile18 installable
    # with guile 2.0. Update internal paths and links.
    file delete ${destroot}${prefix}/share/${name}/1.6/ice-9/and-let\*.scm
    file delete ${destroot}${prefix}/share/info/goops.info
    file delete ${destroot}${prefix}/share/info/guile-tut.info
    file delete ${destroot}${prefix}/share/info/guile.info
    file delete ${destroot}${prefix}/share/info/guile.info-1
    file delete ${destroot}${prefix}/share/info/guile.info-2
    file delete ${destroot}${prefix}/share/info/guile.info-3
    file delete ${destroot}${prefix}/share/info/guile.info-4
    file delete ${destroot}${prefix}/share/info/guile.info-5
    file delete ${destroot}${prefix}/share/info/guile.info-6
    file delete ${destroot}${prefix}/share/info/guile.info-7
    file delete ${destroot}${prefix}/share/info/r5rs.info
    
    file rename ${destroot}${prefix}/bin/guile ${destroot}${prefix}/bin/${name}
    file rename ${destroot}${prefix}/bin/guile-config ${destroot}${prefix}/bin/${name}-config
    file rename ${destroot}${prefix}/bin/guile-snarf ${destroot}${prefix}/bin/${name}-snarf
    file rename ${destroot}${prefix}/bin/guile-tools ${destroot}${prefix}/bin/${name}-tools

    file rename ${destroot}${prefix}/include/guile ${destroot}${prefix}/include/guile18

    file rename ${destroot}${prefix}/include/libguile ${destroot}${prefix}/include/libguile18
    file rename ${destroot}${prefix}/include/libguile.h ${destroot}${prefix}/include/libguile18.h

    file rename ${destroot}${prefix}/share/aclocal/guile.m4 ${destroot}${prefix}/share/aclocal/guile18.m4

    file rename ${destroot}${prefix}/lib/libguile.a ${destroot}${prefix}/lib/libguile18.a
    file rename ${destroot}${prefix}/lib/libguile.la ${destroot}${prefix}/lib/libguile18.la
    file rename ${destroot}${prefix}/lib/libguile.dylib ${destroot}${prefix}/lib/libguile18.dylib

    file rename ${destroot}${prefix}/share/man/man1/guile.1 ${destroot}${prefix}/share/man/man1/guile18.1
    
    reinplace "s|-lguile |-lguile18 |g" ${destroot}${prefix}/bin/guile18-config
    reinplace "s|guile-config|guile18-config|g" ${destroot}${prefix}/bin/guile18-config
    reinplace "s|guile-tools|guile18-tools|g" ${destroot}${prefix}/bin/guile18-tools
    reinplace "s|${prefix}/bin/guile|${prefix}/bin/guile18|g" ${destroot}${prefix}/bin/guile18-config
    reinplace "s|-lguile |-lguile18 |g" ${destroot}${prefix}/lib/pkgconfig/guile-1.8.pc
    reinplace "/^includedir=/s|$|/guile18|g" ${destroot}${prefix}/lib/pkgconfig/guile-1.8.pc
    reinplace "s|libguile|libguile18|g" ${destroot}${prefix}/lib/libguile18.la

    reinplace "s|,guile|,guile18|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|guile-tools|guile18-tools|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|guile-config|guile18-config|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|GUILE_PROGS|GUILE1_8_PROGS|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|GUILE_FLAGS|GUILE1_8_FLAGS|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|GUILE_SITE_DIR|GUILE1_8_SITE_DIR|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|GUILE_CHECK|GUILE1_8_CHECK|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|GUILE_MODULE_CHECK|GUILE1_8_MODULE_CHECK|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|GUILE_MODULE_AVAILABLE|GUILE1_8_MODULE_AVAILABLE|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|GUILE_MODULE_REQUIRED|GUILE1_8_MODULE_REQUIRED|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|GUILE_MODULE_EXPORTS|GUILE1_8_MODULE_EXPORTS|g" ${destroot}${prefix}/share/aclocal/guile18.m4
    reinplace "s|GUILE_MODULE_REQUIRED_EXPORT|GUILE1_8_MODULE_REQUIRED_EXPORT|g" ${destroot}${prefix}/share/aclocal/guile18.m4    

    fs-traverse src_file  ${destroot}${prefix}/include/ {
        if { [ file isfile ${src_file} ] } {
            reinplace "s|\\<guile\\/|\\<guile18\\/|g"   $src_file
            reinplace "s|libguile\\.h|libguile18\\.h|g" $src_file
            reinplace "s|libguile/|libguile18/|g"       $src_file
        }
    }
}

livecheck.type  regex
livecheck.url   "https://ftp.gnu.org/pub/gnu/guile/?C=N;O=D"
livecheck.regex {guile-(1\.8(?:\.\d+)*)}