#!/bin/bash

DISTRO_ROOT="/home/darkstar/Playground/zaurus/quasar/distro"
INPUT_ROOT="$DISTRO_ROOT/input"
OUTPUT_ROOT="$DISTRO_ROOT/output"

DEV_ROOT="/home/darkstar/Playground/zaurus"
QUASAR_ROOT="$DEV_ROOT/quasar"

function compileForSharp()
{
	cd "$DEV_ROOT"
	source dev-arm-qpe.sh

	cd "$QUASAR_ROOT"
	make clean > /dev/null 2>&1
	tmake quasar-qtopia.pro > Makefile
	make clean > /dev/null 2>&1
	make -j3 > /dev/null 2>&1

	if [ ! -f "$QUASAR_ROOT/quasar" ]
	then
		echo "Build failed..."
		exit 1
	else
		echo "Build successful."
	fi
	
	arm-linux-strip --strip-all "$QUASAR_ROOT/quasar"
	
	rm -Rf "$INPUT_ROOT"
	mkdir -p "$INPUT_ROOT"
	cp -R "$DISTRO_ROOT/skeletons/sharp/"* "$INPUT_ROOT"
	cp "$QUASAR_ROOT/quasar" "$INPUT_ROOT/opt/QtPalmtop/bin/quasar"
	make clean > /dev/null 2>&1

	# copy skins and clean up
	SHARE_ROOT="$INPUT_ROOT/opt/QtPalmtop/share/quasar"
	rm -Rf "$SHARE_ROOT/skins"
	cp -R "$DISTRO_ROOT/skins" "$SHARE_ROOT/"
	rm -Rf "$SHARE_ROOT/skins/default/"*.xcf

	# copy images
	IMAGE_ROOT="$INPUT_ROOT/opt/QtPalmtop/pics/quasar"
	rm -f "$IMAGE_ROOT/"* > /dev/null 2>&1
	cp "$DISTRO_ROOT/images/"* "$IMAGE_ROOT/"
	

	# create package
	cd "$DISTRO_ROOT"
	find "$INPUT_ROOT" -name ".svn" -exec /bin/rm -rf '{}' \; > /dev/null 2>&1
	./ipkg-build "$INPUT_ROOT" "$OUTPUT_ROOT"
}

function compileForPdaXrom()
{
	cd "$DEV_ROOT"
	source dev-arm-pdaxrom.sh

	cd "$QUASAR_ROOT"
	make clean > /dev/null 2>&1
	qmake quasar-pdaxrom.pro > Makefile
	make clean > /dev/null 2>&1
	make -j3 > /dev/null 2>&1

	if [ ! -f "$QUASAR_ROOT/quasar" ]
	then
		echo "Build failed..."
		exit 1
	else
		echo "Build successful."
	fi

	armv5tel-cacko-linux-strip -s "$QUASAR_ROOT/quasar"

	rm -Rf "$INPUT_ROOT"
	mkdir -p "$INPUT_ROOT"
	cp -R "$DISTRO_ROOT/skeletons/pdaXrom/"* "$INPUT_ROOT"
	cp "$QUASAR_ROOT/quasar" "$INPUT_ROOT/usr/bin/quasar"
	make clean > /dev/null 2>&1
	
	# copy skins and clean up
	SHARE_ROOT="$INPUT_ROOT/usr/share/quasar"
	rm -Rf "$SHARE_ROOT/skins"
	cp -R "$DISTRO_ROOT/skins" "$SHARE_ROOT/"
	rm -Rf "$SHARE_ROOT/skins/default/"*.xcf
	
	# copy images
	IMAGE_ROOT="$SHARE_ROOT/pics/quasar"
	rm -f "$IMAGE_ROOT/"* > /dev/null 2>&1
	cp "$DISTRO_ROOT/images/"* "$IMAGE_ROOT/"
	
	# create package
	cd "$DISTRO_ROOT"
	find "$INPUT_ROOT" -name ".svn" -exec /bin/rm -rf '{}' \; > /dev/null 2>&1
	./ipkg-build "$INPUT_ROOT" "$OUTPUT_ROOT"
}

rm -Rf "$INPUT_ROOT"

mkdir -p "$INPUT_ROOT"
mkdir -p "$OUTPUT_ROOT"

echo
echo "Building for Qtopia..."
compileForSharp

echo
echo "Building for pdaXrom..."
compileForPdaXrom

echo

rm -Rf "$INPUT_ROOT"
