# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=cryptsetup
pkgver=2.7.5
pkgrel=1
pkgdesc="Userspace setup tool for transparent encryption of block devices using the Linux 2.6 cryptoapi"
url="https://gitlab.com/cryptsetup/cryptsetup"
arch="all"
license="GPL-2.0-or-later WITH cryptsetup-OpenSSL-exception"
makedepends_build="asciidoctor bash coreutils"
makedepends_host="
	argon2-dev
	json-c-dev
	lvm2-dev
	openssl-dev>3
	popt-dev
	util-linux-dev
	"
makedepends="$makedepends_build $makedepends_host"
checkdepends="device-mapper which sharutils"
subpackages="$pkgname-dev $pkgname-doc $pkgname-libs $pkgname-openrc"
source="https://www.kernel.org/pub/linux/utils/cryptsetup/v${pkgver%.*}/cryptsetup-$pkgver.tar.gz
	dmcrypt.initd
	dmcrypt.confd
	"

# secfixes:
#   2.4.3-r0:
#     - CVE-2021-4122
#   2.3.4-r0:
#     - CVE-2020-14382

build() {
	# Disable support for external tokens since it requires
	# dlvsym(3) which is a GNU extension and not available in musl.
	if [ -z "$BOOTSTRAP" ]; then
		export CFLAGS="$CFLAGS -flto=auto"
	fi
	./configure \
		--build="$CBUILD" \
		--host="$CHOST" \
		--prefix=/usr \
		--sbindir=/sbin \
		--disable-static \
		--enable-libargon2 \
		--with-crypto_backend=openssl \
		--disable-external-tokens \
		--disable-ssh-token
	make
}

check() {
	make check
}

package() {
	make DESTDIR=$pkgdir install

	install -Dm644 "$srcdir"/dmcrypt.confd "$pkgdir"/etc/conf.d/dmcrypt
	install -Dm755 "$srcdir"/dmcrypt.initd "$pkgdir"/etc/init.d/dmcrypt

	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/
	install -m644 README.md FAQ.md docs/v$pkgver-ReleaseNotes \
		-t "$pkgdir"/usr/share/doc/$pkgname/

	# mandoc does not support the .so directive which replace the
	# content of a man page with that of another. cryptsetup uses
	# several man pages which just contain an .so directive. Replace
	# these with symlinks to make them work by default in mandoc.
	#
	# See https://inbox.vuxu.org/mandoc-tech/20101024194129.GJ20876@iris.usta.de/#r
	grep -l -R '^\.so ' "$pkgdir"/usr/share/man | \
		xargs awk '/^\.so/ { system("ln -fs " $2 " " FILENAME) }'
}

libs() {
	pkgdesc="Cryptsetup shared library"

	amove usr/lib
}

sha512sums="
1c2752baacca9c3ae6c507b9e86a0b31ee8b3d508d4233292ad3df432679b875810a4bef93eaa680e58203b04ca30e8e215c91a8a83d2c61905180633c372edf  cryptsetup-2.7.5.tar.gz
2a7abe572809d070579ceaeeeb25f050d32408756c1086ddf0e16f41bde239cfb1b8b95e1009362188faccd8060926cbf48a04714e8ddbb09ae3c903f6a490c8  dmcrypt.initd
74422d5e1614b43af894ea01da1ea80d805ec7f77981cbb80a6b1a4becad737a8825d7269812499095a7f50d39fa7da5bf4e4edae63529b1fe87b9176943a733  dmcrypt.confd
"
