#!/bin/sh

# This creates a binary tarball for the GP2X/Wiz.
# NOTE: This is run when executing 
# make -f Makefile.{gp2x|open2x|openwiz}
# It also expects PACKAGE_DATA_DIR and TOOLCHAIN to have been exported.

if test -a VERSION ; then
	VERSION="$(cat VERSION)"
	if test -a TARGET ; then
		TARGET="$(cat TARGET)"
		if [ -n "$PACKAGE_DATA_DIR" ] ; then
			if [ -n "$TOOLCHAIN" ] ; then
#-------------------------------------------------------------
				echo "Building GP2X/Wiz binary tarball..."
				
				mkdir -p ${TARGET}_${VERSION}_${TOOLCHAIN}/${PACKAGE_DATA_DIR}
				mkdir -p ${TARGET}_${VERSION}_${TOOLCHAIN}/games

				cp data/* ${TARGET}_${VERSION}_${TOOLCHAIN}/${PACKAGE_DATA_DIR}
				cp games-etc/*.p games-etc/*.txt ${TARGET}_${VERSION}_${TOOLCHAIN}/games
				cp games-etc/pipepanic/pipepanic.p games-etc/pipepanic/pipepanic.txt \
					${TARGET}_${VERSION}_${TOOLCHAIN}/games

				cp icon-${TARGET}-32.png ${TARGET}_${VERSION}_${TOOLCHAIN}/${TARGET}.png
				cp ${TARGET} gp2x/${TARGET}.gpe ${TARGET}_${VERSION}_${TOOLCHAIN}

				# The openwiz build requires libiconv.
				if [[ "${TOOLCHAIN}" == "openwiz" ]] ; then
					cp gp2x/*libiconv* ${TARGET}_${VERSION}_${TOOLCHAIN}
				fi

				cp README ${TARGET}_${VERSION}_${TOOLCHAIN}/readme.txt
				cp ChangeLog NEWS COPYING ${TARGET}_${VERSION}_${TOOLCHAIN}
				
				tar czvf ${TARGET}_${VERSION}_${TOOLCHAIN}.tar.gz ${TARGET}_${VERSION}_${TOOLCHAIN}
				
				rm -rf ${TARGET}_${VERSION}_${TOOLCHAIN}
				
				echo "All done."
#-------------------------------------------------------------
			else
				echo "ERROR: Variable \"TOOLCHAIN\" has not been set/exported."
			fi
		else
			echo "ERROR: Variable \"PACKAGE_DATA_DIR\" has not been set/exported."
		fi
	else
		echo "ERROR: File \"TARGET\" not found. This is a text file with the project name inside it."
	fi
else
	echo "ERROR: File \"VERSION\" not found. This is a text file with the version number inside it."
fi
