# SliTaz package receipt.

PACKAGE="mingw32-runtime"
SOURCE="mingwrt"
VERSION="3.15.2-mingw32"
CATEGORY="development"
SHORT_DESC="MinGW32 runtime."
MAINTAINER="rcx@zoominternet.net"
LICENSE="PublicDomain"
TARBALL="$SOURCE-$VERSION-src.tar.gz"
WEB_SITE="http://www.mingw.org/"
WGET_URL="$SF_MIRROR/mingw/$TARBALL"
MINGW32_ID="i586-pc-mingw32"

DEPENDS="mingw32-w32api"
BUILD_DEPENDS="slitaz-toolchain mingw32-binutils \
mingw32-runtime-headers mingw32-w32api-headers mingw32-gcc-c mingw32-w32api"

# Configuration only needs included if we're in the build/wok environment
if [ -e $WOK/mingw32-toolchain/stuff/mingw32.conf ] ; then
	. $WOK/mingw32-toolchain/stuff/mingw32.conf
fi

# What is the latest version available today?
current_version()
{
	wget -O - https://sourceforge.net/projects/mingw/files/MinGW/Base/mingwrt/ 2>/dev/null | \
	sed '/scope="row/!d;s|.*href="|"https://sourceforge.net|;q' | xargs wget -O - 2>/dev/null | \
	sed '/scope="row/!d;s|.*/mingwrt-||;s|-src.*||;q'
}

# Rules to configure and make the package.
compile_rules()
{
	sed -i 's|/doc/|/share&|' configure */configure Makefile.in
	if [ "$MINGW32_ID" != "$MINGW32_TARGET" ] ; then
		echo "ERROR: Conf 'TARGET' string differs from Receipt 'ID' string"
		return 1
	fi

	find $src -name 'Makefile*' | xargs sed -i "s|^inst_.*:= *|&$DESTDIR/|"
	mkdir -p $DESTDIR/usr/bin
	rm -f -r $src/binutils
	mkdir -p $SOURCE-$VERSION-build
	cd $SOURCE-$VERSION-build
	
	$src/configure \
		--prefix=/usr/$MINGW32_TARGET \
		--host=$MINGW32_TARGET \
		--build=$BUILD_HOST &&
	make CFLAGS="-O2 -mms-bitfields -march=${TARGET_CPU-i386} -I$MINGW32_ROOT/include" LDFLAGS="-s" &&
	make install
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir -p $fs$MINGW32_ROOT
	cp -a $install/usr/bin $fs$MINGW32_ROOT
	cp -a $install/usr/lib $fs$MINGW32_ROOT
}

# Rules to setup after installation
post_install()
{
	# Archives get stripped during packaging; need to run ranlib
	chroot "$1/" find "/usr/$MINGW32_ID/lib/" -name *.a -exec $MINGW32_ID-ranlib {} \;
}

# Rules to clean the package
clean_wok()
{
	rm -r $SOURCE-$VERSION-build
}