# Build recipe for ntfs-3g.
#
# Copyright (c) 2022 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=ntfs-3g_ntfsprogs
pkgname=ntfs-3g
version=2022.10.3
release=1

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

tarname=${program}-${version}.tgz

# Remote source(s)
fetch=https://tuxera.com/opensource/$tarname

description="
Drivers and utilities for the NTFS file system.

The ntfs-3g package contains a stable, read-write open source driver for
NTFS partitions.  NTFS partitions are used by most Microsoft operating
systems, ntfs-3g allows you to mount NTFS partitions in read-write mode
from your GNU/Linux system.  It uses the FUSE kernel module to be able to
implement NTFS support in user space.  The package also contains various
utilities useful for manipulating NTFS partitions.
"

homepage=https://www.tuxera.com/community/open-source-ntfs-3g/
license="GPLv2+, LGPLv2+"

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

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

    cd "$srcdir"

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

    # Make sure to point to the /usr hierarchy
    sed -i 's#$(DESTDIR)/sbin#$(DESTDIR)/usr/sbin#g' src/Makefile.in

    ./configure CPPFLAGS="$QICPPFLAGS" \
    CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-static=no \
     --enable-extras \
     --enable-posix-acls \
     --enable-xattr-mappings \
     --enable-crypto \
     --with-fuse=external \
     --disable-ldconfig \
     --build="$(gcc -dumpmachine)" \
     rootbindir=/usr/bin \
     rootsbindir=/usr/sbin \
     rootlibdir=/usr/lib${libSuffix} \

    make -j${jobs} V=1 \
     rootbindir=/usr/bin \
     rootsbindir=/usr/sbin \
     rootlibdir=/usr/lib${libSuffix} \
     DESTDIR="$destdir" install

    install -p -m 755 "${worktree}/archive/ntfs-3g/mount.ntfs" \
                      "${destdir}/usr/bin/mount.ntfs"

    # Check if 'ntfsallocate' was installed, if not remove its manpage
    if test ! -e "${destdir}/usr/bin/ntfsallocate"
    then
        rm -f "${destdir}/${mandir}/ntfsfallocate.8"
    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"
}

