# Build recipe for fuse (version 2).
#
# Copyright (c) 2019-2020 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.

# Exit immediately on any error
set -e

program=fuse
version=2.9.9
release=3

# Set custom package name
pkgname=fuse2

# Define a category for the output of the package name
pkgcategory=libs

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

# Remote source(s)
fetch=https://github.com/libfuse/libfuse/releases/download/fuse-${version}/$tarname

description="
Filesystem in userspace (release 2).

FUSE (Filesystem in Userspace) is a simple interface for userspace
programs to export a virtual filesystem to the Linux kernel.  FUSE
also aims to provide a secure method for non privileged users to
create and mount their own filesystem implementations.
"

homepage=https://github.com/libfuse/libfuse/
license="GPLv2+, LGPLv2.1"

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

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

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

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
    MOUNT_FUSE_PATH=/usr/sbin INIT_D_PATH=/etc/rc.d \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-lib \
     --enable-util \
     --disable-static \
     --disable-example \
     --build="$(gcc -dumpmachine)"

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

    # Replace rc script with a better one from Dragora!
    rm -f "${destdir}/etc/rc.d/fuse"
    cp -p "${worktree}/archive/fuse/rc.fuse" "${destdir}/etc/rc.d/"
    chmod 755 "${destdir}/etc/rc.d/rc.fuse"

    touch "${destdir}/etc/rc.d/.graft-config"

    # Rules for FUSE are already managed by eudev
    rm -rf "${destdir}/etc/udev" "${destdir}/dev"

    # 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"
}

