# SliTaz package receipt.

PACKED_SIZE="72.0K"
UNPACKED_SIZE="80.0K"
PACKAGE="memtest"
VERSION="6.20"
CATEGORY="base-system"
SHORT_DESC="Memory failures detection tool."
MAINTAINER="pascal.bellard@slitaz.org"
LICENSE="GPL2"
SOURCE="memtest86+"
TARBALL="$SOURCE-$VERSION.tar.gz"
WEB_SITE="https://www.memtest.org/"
WGET_URL="https://github.com/memtest86plus/memtest86plus/archive/refs/tags/v$VERSION.tar.gz"

BUILD_DEPENDS="xz lz4 lzsa salvador gcc83"

# What is the latest version available today?
current_version()
{
	wget -O - ${WGET_URL%/arch*}/tags 2>/dev/null | \
	sed '/tag\//!d;s|.*tag/v*||;s|".*||;q'
}

asm()
{
	cc -o $1.o -Wa,-a=$1.lst,-acm -c $1.S
	objcopy --only-section=.text -O binary $1.o $1.bin
}

tune_lzma()
{
	file=$1
	shift
	for i in lzma lz4x2 lzsa1x2 lzsa2x2 zx0x2 ; do
		cp $stuff/*.S $stuff/pack .
		sed "s/VERSION/$VERSION/;s|lzma1=|&nice=${1:-64},|" -i pack
		while [ -n "$2" ]; do
			sed "s|^#define PROP_$2 .*|#define PROP_$2 $3|" -i unlzma.S
			shift 2
		done
		sed -i "s|uncompress|un${i%x2}|" unpack.S
		asm bootloader
		asm unpack
		cp pack pack.bak
		./pack --build bootloader.bin unpack.bin
		PACKER=${i%x2} ./pack $file $file.${i%x2}
		if [ $(stat -c %s $file.${i%x2}) -ge $((0x101F0)) ]; then
			rm $file.${i%x2}
			cp pack.bak pack
			sed -e "s|^#define FLAT16.*// lzma case|#define FLAT16			0 // ${i%x2}|" -i unpack.S
			asm unpack
			./pack --build bootloader.bin unpack.bin
			PACKER=${i%x2} ./pack $file $file.${i%x2}
		fi
		mv unpack.lst unpack.lst.${i%x2}
		if [ "${i%x2}" != "$i" ]; then
			cp pack.bak pack
			sed -e "s|^#define DOUBLE_PACKED|#define DOUBLE_PACKED	1 // $i|" -i unpack.S
			asm unpack
			./pack --build bootloader.bin unpack.bin
			PACKER=${i%x2} ./pack $file $file.$i
			mv unpack.lst unpack.lst.$i
		fi
	done
}

# Rules to configure and make the package.
compile_rules()
{
	sed -i 's|64, MT_VERSION "." GIT_HASH|65, MT_VERSION ".SliTaz"|;s|"\.x32"|".386"|' app/display.c
	sed 's|gcc|gcc-83|;s|march=i586|march=i486|' -i build32/Makefile
	patch -p0 < $stuff/memtest86\+-6-386.patch
	cd build32
	make boot/startup.o
	objdump -t boot/startup.o | \
	awk '/patch/ { print "sed -i \"s|" $5 "|0x" $1 "|\" ../boot/setup.S" }' | sh
	make memtest.bin
	tune_lzma memtest.bin 255,mf=bt2 LC 3 PB 2
	for i in lzma lz4 lz4x2 lzsa1 lzsa1x2 lzsa2 lzsa2x2 zx0 zx0x2 ; do
		[ $(stat -c "%s" memtest.bin.$i) -gt 2000 ] && continue
		echo "$i error"
		[ $i != lzma ] || return 1
	done
} 


# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir -p $fs/boot $install/usr/share/doc
	cp $src/README* $install/usr/share/doc
	cp $src/LICENSE $install/usr/share/doc
	cp $src/build32/memtest.bin.lzma $fs/boot/memtest.exe
}

# Pre and post install commands for Tazpkg.
post_install()
{
	[ -s $1/boot/isolinux/isolinux.cfg ] &&
	! grep -qs 'Check 32 bits memory' $1/boot/isolinux/isolinux.cfg && sed -i \
's|LABEL md5|LABEL memtest mem ram\
	MENU LABEL Check 32 bits memory\
	KERNEL /boot/memtest.exe\
\n&|' $1/boot/isolinux/isolinux.cfg
	true
}
