### this file is sourced not run
PKGVER=0.11.2
PKGBUILD=1
PKGARCH=noarch

# source: james
TARBALL=refind-bin-$PKGVER.zip
MD5SUM=fdb73e33432e70a7e4e4f47b2b295b87
SRC_URL=http://downloads.sourceforge.net/refind/$TARBALL
BUNDLE=

PKG_NOSTRIP=yes # don't strip to preserve Secure Boot signatures

SLACKREQ=
SLACKDESC="refind: refind $PKGVER (Versatile UEFI bootloader)
refind: 
refind: rEFInd is a versatile bootloader/bootmanager for UEFI systems. rEFInd 
refind: has many features and is one of the bootmanager used by Fatdog for 
refind: booting on UEFI systems. This package contains binaries for both
refind: 64-bit and 32-bit UEFI/EFI systems.
refind: 
refind: This is a re-packaged binary distribution from rEFInd website:
refind: http://www.rodsbooks.com/refind/, signed with Fatdog64 key.
refind: 
refind: The files are kept in /usr/share/refind-$PKGVER
"

### default pkg_download
### prepare
pkg_prepare() {
	if pkg_check $SRC_DIR $TARBALL $MD5SUM; then
		cp -a $SRC_DIR/../keys . &&
		mkdir -p usr/share && cd usr/share &&
		unzip $SRC_DIR/$TARBALL
	fi
}

### default pkg_package
### build
pkg_build() {
	# sign all EFI files
	cd /usr/share/refind* &&

	# remove existing signatures
	find . -name '*.efi' |
	while read -r p; do
		while true; do
			sbattach --remove $p 2> /dev/null || break 
		done
	done &&

	# sign with our keys
	find . -name '*.efi' |
	while read -r p; do
		sbsign --key /keys/fatdog64.key --cert /keys/fatdog64.crt $p 2> /dev/null &&
		mv ${p}.signed $p || return 1
	done &&
	
	# copy our certs
	cp -a /keys/fatdog64.{cer,crt} /usr/share/refind*/keys &&
	rm -rf /keys &&
	
	# remove refind keys (we've re-sign the binaries), and keep a note
	(
	cd /usr/share/refind*/keys &&
	rm -f refind.{cer,crt} &&
	cat << EOF >> README.txt

===========================

Fatdog64 Note:
-------------
refind.cer and refind.crt have been removed from this package.
This is because all of the binaries have been re-signed with
Fatdog's own certificate.

In replacement, fatdog64.cer and fatdog64.crt have been included here.

All the above examples with refind.crt/refind.cer are still valid,
provided you replace instance of "refind.cer" with "fatdog64.cer"
and replace "refind.crt" with "fatdog64.crt".

EOF
	)

	# our icons
	cp /tmp/pkg/os_fatdog.png /usr/share/refind*/refind/icons &&

	# install man pages
	mkdir -p /usr/share/man/man8 &&
	install -m644 /usr/share/refind*/docs/man/*.8 /usr/share/man/man8 &&
	rm -r /usr/share/refind*/docs/man &&
	
	# move the rest of the docs to /usr/share/doc
	mv /usr/share/refind*/docs /usr/share/doc/refind-$PKGVER &&
	
	# remove unsupported arches
	rm -r /usr/share/refind*/refind/*aa64* &&

	# add symlink
	# rm -rf /usr/share/doc/refind /usr/share/refind &&
	# ln -s refind-$PKGVER /usr/share/doc/refind &&
	# ln -s refind-bin-$PKGVER /usr/share/refind
	
	pkg_build_slackdesc
}


