#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# TODO: re-enable documentation once asciidoc is available.

PKGNAME=btrfs-progs
PKGVERSION=5.2.2
PKGREVISION=1
PKGLICENSE="GPLv20"
PKGSUMMARY="userspace utilities to manage btrfs filesystems"
PKGDOWNLOAD="https://mirrors.edge.kernel.org/pub/linux/kernel/people/kdave/$PKGNAME/$PKGNAME-v$PKGVERSION.tar.xz"
PKGDEPENDS="lzo util-linux xmlto:build zlib"
PKGOPTIONS="CONVERT PYTHON ZSTD"

: ${CHOST:=$(uname -m)-linux-musl}
: ${CBUILD:=$(uname -m)-linux-musl}
: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${CONVERT:=yes}
: ${PYTHON:=yes}
: ${ZSTD:=yes}

if [ "$CONVERT" = "yes" ]; then
	PKGDEPENDS="e2fsprogs $PKGDEPENDS"
	with_convert="--enable-convert"
else
	with_convert="--disable-convert"
fi

if [ "$PYTHON" = "yes" ]; then
	PKGDEPENDS="python $PKGDEPENDS"
	with_python="--enable-python"
else
	with_python="--disable-python"
fi

if [ "$ZSTD" = "yes" ]; then
	PKGDEPENDS="zstd $PKGDEPENDS"
	with_zstd="--enable-zstd"
else
	with_zstd="--disable-zstd"
fi

rm -rf $PKGNAME-v$PKGVERSION
tar xvf $PKGNAME-v$PKGVERSION.tar.xz
cd $PKGNAME-v$PKGVERSION

# --disable-backtrace: not available on musl.
CC="$CC" \
CFLAGS="$CFLAGS" \
LDFLAGS="$LDFLAGS" \
LIBS="$LIBS" \
./configure \
	--build=$CBUILD \
	--host=$CHOST \
	--prefix=/usr \
	--disable-backtrace \
	--disable-documentation \
	$with_convert \
	$with_python \
	$with_zstd
make
make install DESTDIR=$DESTDIR

cd ..
rm -rf $PKGNAME-v$PKGVERSION
