# Build recipe for squashfs-tools.
#
# Copyright (c) 2021 Matias Fonzo, <selk@dragora.org>.
# Copyright (c) 2022 DustDFG, <dfgdust@gmail.com>.
#
# 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=squashfs
pkgname=squashfs-tools
version=4.5
release=1

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

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

# Remote source(s)
fetch=https://downloads.sourceforge.net/project/squashfs/squashfs/squashfs${version}/$tarname

description="
Tools to create and extract Squashfs filesystems.

This is a squashed read-only filesystem for GNU/Linux.
"

homepage=https://github.com/plougher/squashfs-tools/
license=GPLv2+

# Source documentation
docs="
  ACKNOWLEDGEMENTS ACTIONS-README CHANGES COPYING README-${version} USAGE
  RELEASE-READMEs/DONATIONS RELEASE-READMEs/pseudo-file.example
"
docsdir="${docdir}/${pkgname}-${version}"

# The source has a custom directory
srcdir=${pkgname}-${version}

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

    cd "$srcdir"

    # Apply a patch to include LZIP support
    patch -p1 < "${worktree}/patches/squashfs-tools/squashfs-tools-4.5_lzip-0.diff"

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

    cd squashfs-tools

    make -j${jobs} CC=cc EXTRA_CFLAGS="$QICFLAGS -O3" LDFLAGS="$QILDFLAGS" \
     GZIP_SUPPORT=1 \
     LZIP_SUPPORT=1 \
     XZ_SUPPORT=1 \
     LZO_SUPPORT=0 \
     LZ4_SUPPORT=0 \
     ZSTD_SUPPORT=0 \
     COMP_DEFAULT=lzip \
     XATTR_SUPPORT=1 \
     install INSTALL_DIR="${destdir}/usr/bin"

    cd ../

    # Re-create symlinks for utilities
    (
        cd "${destdir}/usr/bin" || exit 1
        ln -sf mksquashfs sqfscat
        ln -sf unsquashfs sqfstar
    )

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

