# Build recipe for guile.
#
# Copyright (c) 2018-2019 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=guile
version=2.2.6
release=1

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/devel"

tarname=${program}-${version}.tar.lz

# Remote source(s)
fetch=http://ftp.gnu.org/gnu/guile/$tarname

homepage=http://www.gnu.org/software/guile/

description="
The GNU Guile programming language.

Guile is designed to help programmers create flexible applications that
can be extended by users or other programmers with plug-ins, modules,
or scripts.  With Guile you can create applications and games for the
desktop, the Web, the command-line, and more.

For more information, visit:
$homepage
"

license="GPLv3+, LGPLv3+"

# Source documentation
docs="AUTHORS COPYING* ChangeLog GUILE-VERSION HACKING NEWS README THANKS"
docsdir="${docdir}/${program}-${version}"

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --infodir=$infodir \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-static=no \
     --enable-shared=yes \
     --disable-rpath \
     --with-threads \
     --build="$(cc -dumpmachine)"

    # Uncomment the following (two) commented lines if you want to
    # build the documentation.  We do not build it by default in
    # order to save 9M, approx.

    make -j${jobs} V=1
    #make V=1 html
    make -j${jobs} DESTDIR="$destdir" install-strip
    #make -j${jobs} DESTDIR="$destdir" install-html

    # Delete generated charset.alias
    rm -f "${destdir}/usr/lib${libSuffix}/charset.alias"

    # Move misplaced file(s) for GDB
    mkdir -p "${destdir}/usr/share/gdb/auto-load/usr/lib${libSuffix}"
    mv "${destdir}/usr/lib${libSuffix}"/*-gdb.scm \
       "${destdir}/usr/share/gdb/auto-load/usr/lib${libSuffix}"

    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/*
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 '{}' +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"
}

