arch 2.63 KB
Newer Older
Efe Çetin's avatar
Efe Çetin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#!/bin/bash
SOURCE_DATE_EPOCH=$(date +'%s')
if ! type -p gettext >/dev/null; then
	gettext() {
		printf "%s\n" "$@"
	}
fi

write_kv_pair() {
	local key="$1"
	shift

	for val in "$@"; do
		if [[ $val = *$'\n'* ]]; then
			error "$(gettext "Invalid value for %s: %s")" "$key" "$val"
			exit $E_PKGBUILD_ERROR
		fi
		printf "%s = %s\n" "$key" "$val"
	done
}

list_package_files() {
	(
		export LC_COLLATE=C
		shopt -s dotglob globstar
		# bash 5.0 only works with combo directory + file globs
		printf '%s\0' **/*
	)
}

__write_pkginfo() {
	#Create .PKGINFO
	echo -e "# Generated by unibuild"
	echo -e "# using $(fakeroot -v)" 
	echo -e "pkgname = $name" 
	echo -e "pkgbase = $name"
	echo -e "pkgver = $version-$release"
	echo -e "pkgdesc = $description" 
	echo -e "url = $url" 
	echo -e "builddate = $SOURCE_DATE_EPOCH" 
	echo -e "packager = $maintainer" 
	echo -e "size = $(du -sb $INSTALLDIR | cut -f1)" 
	echo -e "arch = $arch" 
	echo -e "license = $license" 
	write_kv_pair "replaces"    "${replaces[@]}"
	write_kv_pair "group"       "${groups[@]}"
	write_kv_pair "conflict"    "${conflicts[@]}"
	write_kv_pair "provides"    "${provides[@]}"
	write_kv_pair "backup"      "${backup[@]}"
	write_kv_pair "depend"      "${depends[@]}"
	write_kv_pair "optdepend"   "${optdepends[@]//+([[:space:]])/ }"
	write_kv_pair "makedepend"  "${builddepends[@]}"
	write_kv_pair "checkdepend" "${checkdepends[@]}"
}

__write_buildinfo() {
	#Create .BUILDINFO
	echo -e "format = 2" >> $PKGDIR/.BUILDINFO
	echo -e "pkgname = $name" >> $PKGDIR/.BUILDINFO
	echo -e "pkgbase = $name" >> $PKGDIR/.BUILDINFO
	echo -e "pkgver = $version-$release" >> $PKGDIR/.BUILDINFO
	echo -e "pkgarch = $arch" >> $PKGDIR/.BUILDINFO
	local sum="$(sha256sum "$CURDIR/$name")"
	sum=${sum%% *}
	echo -e "pkgbuild_sha256sum = $sum" >> $PKGDIR/.BUILDINFO 
	echo -e "packager = $maintainer" >> $PKGDIR/.BUILDINFO
	echo -e "builddate = $SOURCE_DATE_EPOCH" >> $PKGDIR/.BUILDINFO 
	echo -e "builddir = $BUILDDIR" >> $PKGDIR/.BUILDINFO


	local pkginfos_parsed=($(LC_ALL=pkexec sh -c "pacman -Qi" | awk -F': ' '\
		/^Name .*/ {printf "%s", $2} \
		/^Version .*/ {printf "-%s", $2} \
		/^Architecture .*/ {print "-"$2} \
		'))
	write_kv_pair "installed" "${pkginfos_parsed[@]}"
}



_create_metadata(){
	__write_pkginfo >> $PKGDIR/.PKGINFO
	__write_buildinfo >> $PKGDIR/.BUILDINFO
	
}

_package(){
	cp -prfv $INSTALLDIR/* $PKGDIR/
	cd $PKGDIR
	#Create .MTREE
	list_package_files | LANG=C bsdtar -cnf - --format=mtree \
		--options='!all,use-set,type,uid,gid,mode,time,size,md5,sha256,link' \
		--null --files-from - --exclude .MTREE | gzip -c -f -n > .MTREE
	tar --zstd -cf $CURDIR/$name-$version-$release.pkg.tar.zst * .PKGINFO .BUILDINFO
}