#!/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.
#

PKGNAME=lvm2
PKGVERSION=2.02.183
PKGREVISION=1
PKGLICENSE="GPL-2.0-only LGPL-2.1-only"
PKGSUMMARY="LVM utilities"
PKGDOWNLOAD="https://mirrors.kernel.org/sourceware/$PKGNAME/LVM2.$PKGVERSION.tgz"
PKGDEPENDS="eudev libaio util-linux"
PKGOPTIONS="COMPLETION"

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

if [ "$COMPLETION" = "yes" ]; then
	PKGDEPENDS="readline $PKGDEPENDS"
	with_completion="--enable-readline"
else
	with_completion="--disable-readline"
fi

build()
{
	rm -rf LVM2.$PKGVERSION
	tar xvf LVM2.$PKGVERSION.tgz
	cd LVM2.$PKGVERSION

	# https://bugzilla.redhat.com/show_bug.cgi?id=1697870
	patch -p1 < ../patch-lld.patch
	# --disable-nls: does not build with --enable-nls
	patch -p0 < ../patch-musl.patch
	CC="$CC" \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
	LIBS="$LIBS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix= \
		--sbindir=/bin \
		--disable-nls \
		--enable-pkgconfig \
		--enable-udev_rules \
		--enable-udev_sync \
		$with_completion
	make
	make install DESTDIR=$DESTDIR

	cd ..
	rm -rf LVM2.$PKGVERSION
}
