#!/bin/sh

# This builds an ipk for the Zaurus.
# NOTE: This is run when executing 'make -f Makefile.zaurus'.

if test -a VERSION ; then
	VERSION="$(cat VERSION)"
	if test -a TARGET ; then
		TARGET="$(cat TARGET)"
		if [ -n "$PREFIX" ] ; then
#-------------------------------------------------------------
			echo "Building ipk..."
			
			mkdir -p ipk/CONTROL
			mkdir -p ipk/${PREFIX}/apps/Games
			mkdir -p ipk/${PREFIX}/bin
			mkdir -p ipk/${PREFIX}/pics
			mkdir -p ipk/${PREFIX}/share/${TARGET}/levels
			mkdir -p ipk/${PREFIX}/share/${TARGET}/sounds/default
			mkdir -p ipk/${PREFIX}/share/${TARGET}/sounds/skins
			mkdir -p ipk/${PREFIX}/share/${TARGET}/rob
			mkdir -p ipk/${PREFIX}/share/${TARGET}/skins/tronic
			mkdir -p ipk/${PREFIX}/share/${TARGET}/locales/cz_CZ
			mkdir -p ipk/${PREFIX}/share/${TARGET}/locales/de_DE
			mkdir -p ipk/${PREFIX}/share/${TARGET}/locales/en_GB
			mkdir -p ipk/${PREFIX}/share/${TARGET}/locales/id_ID
			mkdir -p ipk/${PREFIX}/share/${TARGET}/locales/pl_PL
			mkdir -p ipk/${PREFIX}/share/${TARGET}/locales/ru_RU
			mkdir -p ipk/${PREFIX}/share/${TARGET}/locales/sk_SK
			mkdir -p ipk/${PREFIX}/share/${TARGET}/locales/sv_SE

			# GNU Robbo uses oggs but we'll convert them to wavs here.
			oggdec data/sounds/default/*.ogg
			sed -i.orig -e 's/.ogg/.wav/' data/sounds/skins/default.dat

			cp data/levels/* ipk/${PREFIX}/share/${TARGET}/levels/
			cp data/sounds/default/*.wav ipk/${PREFIX}/share/${TARGET}/sounds/default/
			cp data/sounds/skins/default.dat ipk/${PREFIX}/share/${TARGET}/sounds/skins/
			cp data/rob/*.bmp ipk/${PREFIX}/share/${TARGET}/rob/

			# GNU Robbo uses pngs but we'll use bmps which should have already been converted.
			cp data/skins/tronic/*.bmp data/skins/tronic/skinrc ipk/${PREFIX}/share/${TARGET}/skins/tronic/

			cp data/locales/cz_CZ/* ipk/${PREFIX}/share/${TARGET}/locales/cz_CZ/
			cp data/locales/de_DE/* ipk/${PREFIX}/share/${TARGET}/locales/de_DE/
			cp data/locales/en_GB/* ipk/${PREFIX}/share/${TARGET}/locales/en_GB/
			cp data/locales/id_ID/* ipk/${PREFIX}/share/${TARGET}/locales/id_ID/
			cp data/locales/pl_PL/* ipk/${PREFIX}/share/${TARGET}/locales/pl_PL/
			cp data/locales/ru_RU/* ipk/${PREFIX}/share/${TARGET}/locales/ru_RU/
			cp data/locales/sk_SK/* ipk/${PREFIX}/share/${TARGET}/locales/sk_SK/
			cp data/locales/sv_SE/* ipk/${PREFIX}/share/${TARGET}/locales/sv_SE/

			cat <<EOF > zaurus/control
Package: ${TARGET}
Priority: optional
Section: Games
Version: ${VERSION}
Architecture: arm
Maintainer: Thunor <thunorsif@hotmail.com>
Depends: libSDL libSDL_mixer
License: GPL
Description: GNU Robbo for the Sharp Zaurus
Source: http://gnurobbo.sourceforge.net/
EOF

			cp zaurus/control ipk/CONTROL/
			cp zaurus/${TARGET}.desktop ipk/${PREFIX}/apps/Games/
			cp icon32.png ipk/${PREFIX}/pics/${TARGET}.png
			cp ${TARGET} zaurus/run${TARGET} ipk/${PREFIX}/bin/
			cat README.zaurus README > ipk/${PREFIX}/share/${TARGET}/README
			cp ChangeLog NEWS COPYING LICENSE-sound ipk/${PREFIX}/share/${TARGET}/

			zaurus/ipkg-build ipk
			
			rm -rf ipk
			
			mv ${TARGET}_${VERSION}_arm.ipk ${TARGET}_${VERSION}_zaurus.ipk
			
			# Restore the ogg default.dat and remove the wavs we created above.
			rm data/sounds/default/*.wav
			mv -f data/sounds/skins/default.dat.orig data/sounds/skins/default.dat

			echo "All done."
#-------------------------------------------------------------
		else
			echo "ERROR: Variable \"PREFIX\" 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
