# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=dpkg
pkgver=1.22.11
pkgrel=0
pkgdesc="The Debian Package Manager"
url="https://wiki.debian.org/Teams/Dpkg"
arch="all"
license="GPL-2.0-or-later AND BSD-2-Clause"
depends="tar xz"
makedepends="
	bzip2-dev
	libmd-dev
	linux-headers
	perl
	po4a
	zlib-dev
	"
checkdepends="gzip"
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-zsh-completion
	"
source="https://deb.debian.org/debian/pool/main/d/dpkg/dpkg_$pkgver.tar.xz"

# secfixes:
#   1.21.8-r0:
#     - CVE-2022-1664

prepare() {
	default_prepare

	# On the builders /home/buildozer has S_ISGID set this is
	# inherited in $srcdir and causes some dpkg test to end up with
	# a permission mismatch. To fix this remove the S_ISGID from
	# $srcdir.
	chmod -R g-s "$srcdir"
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--with-libz \
		--with-libbz2 \
		--disable-dselect \
		--disable-start-stop-daemon \
		--disable-nls \
		--disable-static \
		--with-zshcompletionsdir=/usr/share/zsh/site-functions
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	touch "$pkgdir/var/lib/$pkgname"/status
	touch "$pkgdir/var/lib/$pkgname"/available
}

dev() {
	default_dev
	pkgdesc="Debian package development tools"
	depends="perl"

	mkdir -p "$subpkgdir"/usr/bin "$subpkgdir"/usr/share/dpkg \
		"$subpkgdir"/usr/lib/dpkg

	mv "$pkgdir"/usr/bin/dpkg-architecture \
		"$pkgdir"/usr/bin/dpkg-buildpackage \
		"$pkgdir"/usr/bin/dpkg-checkbuilddeps \
		"$pkgdir"/usr/bin/dpkg-distaddfile \
		"$pkgdir"/usr/bin/dpkg-genchanges \
		"$pkgdir"/usr/bin/dpkg-gencontrol \
		"$pkgdir"/usr/bin/dpkg-gensymbols \
		"$pkgdir"/usr/bin/dpkg-mergechangelogs \
		"$pkgdir"/usr/bin/dpkg-name \
		"$pkgdir"/usr/bin/dpkg-parsechangelog \
		"$pkgdir"/usr/bin/dpkg-scanpackages \
		"$pkgdir"/usr/bin/dpkg-scansources \
		"$pkgdir"/usr/bin/dpkg-shlibdeps \
		"$pkgdir"/usr/bin/dpkg-source \
		"$pkgdir"/usr/bin/dpkg-vendor \
		"$pkgdir"/usr/bin/dpkg-buildflags \
		"$pkgdir"/usr/bin/dpkg-genbuildinfo \
		"$pkgdir"/usr/bin/dpkg-buildapi \
		"$subpkgdir"/usr/bin/

	mv "$pkgdir"/usr/share/perl* "$subpkgdir"/usr/share/
	mv "$pkgdir"/usr/share/dpkg/*.mk \
		"$subpkgdir"/usr/share/dpkg
}

sha512sums="
8caeb52625e34109bbc3abf1b62792053f989e560b541ec9de85f3fe563f8bcb06db102dbae83058aba16e6f4729260e15e1c7a470762a261bec5460358c0fe9  dpkg_1.22.11.tar.xz
"
