SUMMARY="Uncompress rar files"
DESCRIPTION="UnRAR decompresses rar files. It is a powerful archive manager \
that can backup your data and reduce the size of email attachments. UnRAR can \
decompress the following file formats:
- RAR
- ZIP
- CAB
- ARJ
- JZH
- TAR
- GZ and TAR.GZ
- BZ2 and TAR.BZ2
- ACE
- UUE
- JAR (Java Archive)
- ISO
- 7Z
- XZ
- Z (Unix Compress)"
HOMEPAGE="https://www.rarlab.com/rar_add.htm"
COPYRIGHT="1993-2021 Alexander Roshal"
LICENSE="UnRAR"
REVISION="1"
SOURCE_URI="https://www.rarlab.com/rar/unrarsrc-$portVersion.tar.gz"
CHECKSUM_SHA256="3e96421f568e438af6dcdaef717c48eb93b825d97058ebcb173b9bfc57807be3"
SOURCE_DIR="unrar"
ADDITIONAL_FILES="
	test.rar
	unrar.rdef
	"

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="x86_gcc2 x86"

PROVIDES="
	unrar$secondaryArchSuffix = $portVersion
	lib:libunrar$secondaryArchSuffix = $portVersion
	"
if [ -z "$secondaryArchSuffix" ]; then
PROVIDES="$PROVIDES
	cmd:unrar = $portVersion
	"
fi
REQUIRES="
	haiku$secondaryArchSuffix
	"

PROVIDES_devel="
	unrar${secondaryArchSuffix}_devel = $portVersion
	devel:libunrar$secondaryArchSuffix = $portVersion
	"
REQUIRES_devel="
	unrar$secondaryArchSuffix == $portVersion
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	"
BUILD_PREREQUIRES="
	cmd:cut
	cmd:g++$secondaryArchSuffix
	cmd:make
	cmd:sed
	"

PATCH()
{
	if [ "$effectiveTargetArchitecture" = x86_gcc2 ]; then
		sed -i \
			-e "/^CXXFLAGS=/ s/ -Wno-logical-op-parentheses//;" \
			-e "/^CXXFLAGS=/ s/ -Wno-dangling-else//;" \
			makefile
	fi
	sed -i -e "/^LDFLAGS=/ d;" makefile
}

BUILD()
{
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	sed \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		"$portDir"/additional-files/unrar.rdef > unrar.rdef

	for i in unrar lib sfx; do
		mkdir -p $i
		cd $i
		ln -f -s ../*.cpp ../*.hpp ../makefile .
		CPPFLAGS="-D_BSD_SOURCE" LDFLAGS="-lbsd" make $i
		cd ..
	done
}

INSTALL()
{
	install -d "$libDir"/unrar
	install -t "$libDir"/unrar sfx/default.sfx
	make -C unrar DESTDIR="$prefix" install-unrar
	install lib/libunrar.so $libDir
	install -d "$includeDir"/unrar
	install -t "$includeDir"/unrar dll.hpp

	prepareInstalledDevelLib libunrar
	fixPkgconfig
	packageEntries devel $developDir

	if [ -n "$secondaryArchSuffix" ]; then
		rm -rf $prefix/bin
	else
		addResourcesToBinaries unrar.rdef "$binDir"/unrar
		mimeset -f "$binDir"/unrar
	fi
}

TEST()
{
	rm -rf dir
	"$sourceDir"/unrar/unrar x "$portDir"/additional-files/test.rar
	test "`cat dir/haiku.txt`" = Haiku

	cat sfx/default.sfx "$portDir"/additional-files/test.rar >dir/sfxtest
	cd dir
	chmod +x sfxtest
	./sfxtest
	test "`cat dir/haiku.txt`" = Haiku
}
