#!/bin/sh

#This creates a binary tarball for the GP2X.
#NOTE: This is run when executing 'make -f Makefile.gp2x'. It also
#expects PACKAGE_DATA_DIR 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
#-------------------------------------------------------------
			echo "Building GP2X binary tarball..."
			
			mkdir -p ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/skins/original
			mkdir -p ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/skins/tronic
			mkdir -p ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/levels
			mkdir -p ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/locales/en_GB
			mkdir -p ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/locales/pl_PL
			
			cp icon32.png ${TARGET}_${VERSION}_gp2x/${TARGET}.png
			cp ${TARGET} gp2x/${TARGET}.gpe ${TARGET}_${VERSION}_gp2x/
			cp data/skins/original/*16.bmp data/skins/original/*.ttf ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/skins/original/
			cp data/skins/original/skinrc ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/skins/original/
			cp data/skins/tronic/*16.bmp data/skins/tronic/*.ttf ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/skins/tronic/
			cp data/skins/tronic/LICENSE-font ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/skins/tronic/
			cp data/skins/tronic/skinrc ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/skins/tronic/
			cp data/levels/*.dat ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/levels/
			cp data/locales/en_GB/localerc ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/locales/en_GB/
			cp data/locales/en_GB/text ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/locales/en_GB/
			cp data/locales/pl_PL/localerc ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/locales/pl_PL/
			cp data/locales/pl_PL/text ${TARGET}_${VERSION}_gp2x/${PACKAGE_DATA_DIR}/locales/pl_PL/

			cp README.gp2x ${TARGET}_${VERSION}_gp2x/readme.txt
			cp COPYING ChangeLog ${TARGET}_${VERSION}_gp2x/
			
			tar czvf ${TARGET}_${VERSION}_gp2x.tar.gz ${TARGET}_${VERSION}_gp2x
			
			rm -rf ${TARGET}_${VERSION}_gp2x
			
			echo "All done."
#-------------------------------------------------------------
		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
