#!/bin/sh

# Script for creating a Qtopia (cross)-compiler on Darwin/Mac OS X
#
# The location you got this script from should contain complete
# instructions about how to use this script. Do not run without
# these instructions!
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

srcdir=""
workdir=""
arcdir=""
error=0
skipnative=0
skipcross=0
skipembedded=0

# Loop over all args
echo ""
echo "***** makeqpe.sh: checking parameters ..."
echo ""
while :
do

# Break out if there are no more args
	case $# in
	0)
		break
		;;
	esac

# Get the first arg, and shuffle
	option=$1
	shift

# Make all options have two hyphens
	orig_option=$option	# Save original for error messages
	case $option in
	--*) ;;
	-*) option=-$option ;;
	esac

# Process an option
	case $option in
	--srcdir)
		case $# in
		0)
			error=1
			;;
		*)
			srcdir=$1
			shift
			;;
		esac
		;;
	--workdir)
		case $# in
		0)
			error=2
			;;
		*)
			workdir=$1
			shift
			;;
		esac
		;;
	--arcdir)
		case $# in
		0)
			error=3
			;;
		*)
			arcdir=$1
			shift
			;;
		esac
		;;
	--skip-native)
		skipnative=1
		;;
	--skip-cross)
		skipcross=1
		;;
	--skip-embedded)
		skipembedded=1
		;;
	*)
		error=4
		;;
	esac
done

# Check if all options are present
if [ x"$srcdir" = x ] || [ x"$workdir" = x ] || [ x"$arcdir" = x ]; then
	error=5
fi

# Output an error message if args are wrong
if [ $error != 0 ] ; then
	echo "Error $error"
	echo "Usage: makeqpe.sh --srcdir <source directory> --workdir <work directory> --arcdir <archive directory>"
	exit 1
fi

# Construct absolute paths for all dirs
echo ""
echo "***** makeqpe.sh: checking the specified directories ..."
echo ""
origdir=`pwd`
cd "$srcdir" || exit 1
abssrcdir=`pwd`
cd "$origdir"
cd "$workdir" || exit 1
absworkdir=`pwd`
cd "$origdir"
cd "$arcdir" || exit 1
absarcdir=`pwd`

# check if the cross compiler is installed where we need it
echo ""
echo "***** makeqpe.sh: checking for cross-compiler ..."
echo ""
if [ ! -e "$absworkdir/opt/Embedix/tools/bin/arm-linux-gcc" ] ; then
	echo "Please create the cross-compiler first (with the same workdir as Qtopia)"
	exit 1
fi
export PATH="$absworkdir/opt/Embedix/tools/bin":"$PATH"
export OLD_PATH="$PATH"

# check if the archive directory looks alright
echo ""
echo "***** makeqpe.sh: checking if the needed archives can be found ..."
echo ""
numarcs=`ls -l "$absarcdir" | grep "qtopia" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one qtopia archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "qt-embedded" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one qt-embedded archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "qt-x11" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one qt-x11 archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "qpe-patches" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one qtopia-patches archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "zaurus-libs" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one zaurus-libs archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "tmake" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one tmake archive in --arcdir"
	exit 1
fi

# extract the names of the archives we need
qtopia_arc=`ls "$absarcdir"/qtopia*gz 2>/dev/null`
if [ x"$qtopia_arc" = x ] ; then
	qtopia_arc=`ls "$absarcdir"/qtopia*.bz2`
	qtopia_arc_x=j
else
	qtopia_arc_x=z
fi
qt_embedded_arc=`ls "$absarcdir"/qt-embedded*gz 2>/dev/null`
if [ x"$qt_embedded_arc" = x ] ; then
	qt_embedded_arc=`ls "$absarcdir"/qt-embedded*.bz2`
	qt_embedded_arc_x=j
else
	qt_embedded_arc_x=z
fi
qt_x11_arc=`ls "$absarcdir"/qt-x11*gz 2>/dev/null`
if [ x"$qt_x11_arc" = x ] ; then
	qt_x11_arc=`ls "$absarcdir"/qt-x11*.bz2`
	qt_x11_arc_x=j
else
	qt_x11_arc_x=z
fi
qpe_patches_arc=`ls "$absarcdir"/qpe-patches*.tgz`
zaurus_libs_arc=`ls "$absarcdir"/zaurus-libs*.tgz`
tmake_arc=`ls "$absarcdir"/tmake*gz 2>/dev/null`
if [ x"$tmake_arc" = x ] ; then
	tmake_arc=`ls "$absarcdir"/tmake*.bz2`
	tmake_arc_x=j
else
	tmake_arc_x=z
fi

echo ""
echo "***** makeqpe.sh: extracting archives ..."
echo ""

# extract the archives
if [ $skipnative = 0 ] ; then
	cd "$abssrcdir"
	mkdir qtopia.mac
	cd qtopia.mac
	if [ $skipembedded = 0 ] ; then
		if [ $qt_embedded_arc_x = z ] ; then
			tar xzf "$qt_embedded_arc" || (echo "Cannot extract QT/Embedded source" && exit 1)
		else
			bzcat "$qt_embedded_arc" | tar xf - || (echo "Cannot extract QT/Embedded source" && exit 1)
		fi
		mv qt-2* qt-embedded
	fi
	if [ $qtopia_arc_x = z ] ; then
		tar xzf "$qtopia_arc" || (echo "Cannot extract Qtopia source" && exit 1)
	else
		bzcat "$qtopia_arc" | tar xf - || (echo "Cannot extract Qtopia source" && exit 1)
	fi
	if [ $qt_x11_arc_x = z ] ; then
		tar xzf "$qt_x11_arc" || (echo "Cannot extract Qt/X11 source" && exit 1)
	else
		bzcat "$qt_x11_arc" | tar xf - || (echo "Cannot extract Qt/X11 source" && exit 1)
	fi
	mv qt-2* qt-x11
fi
cd "$absworkdir"
if [ $tmake_arc_x = z ] ; then
	tar xzf "$tmake_arc" || (echo "Cannot extract tmake" && exit 1)
else
	bzcat "$tmake_arc" | tar xf - || (echo "Cannot extract tmake" && exit 1)
fi
cd "$abssrcdir"
tar xzf "$qpe_patches_arc" || exit 1
if [ $skipcross = 0 ] ; then
	mkdir qtopia.cross
	cd qtopia.cross
	if [ $skipembedded = 0 ] ; then
		if [ $qt_embedded_arc_x = z ] ; then
			tar xzf "$qt_embedded_arc" || (echo "Cannot extract Qt/Embedded source" && exit 1)
		else
			bzcat "$qt_embedded_arc" | tar xf - || (echo "Cannot extract Qt/Embedded source" && exit 1)
		fi
		mv qt-2* qt-embedded
	fi
	if [ $qtopia_arc_x = z ] ; then
		tar xzf "$qtopia_arc" || (echo "Cannot extract Qtopia source" && exit 1)
	else
		bzcat "$qtopia_arc" | tar xf - || (echo "Cannot extract Qtopia source" && exit 1)
	fi
	cd qtopia*
	tar xzf "$zaurus_libs_arc" || exit 1
fi

# patch native sources
if [ $skipnative = 0 ] ; then
	echo ""
	echo "***** makeqpe.sh: patching native sources ..."
	echo ""
	if [ $skipembedded = 0 ] ; then
		cd "$abssrcdir"/qtopia.mac/qt-embedded*
		patch -p1 <../../qtopia-patches/qt-embedded.patch || exit 1
	fi
	cd "$abssrcdir"/qtopia.mac/qt-x11*
	patch -p1 <../../qtopia-patches/qt-x11.patch || exit 1
	cd "$abssrcdir"/qtopia.mac/qtopia*
	patch -p1 <../../qtopia-patches/qtopia.patch || exit 1
else
	echo ""
	echo "***** makeqpe.sh: skipping native part ..."
	echo ""
fi

# unset QMAKESPEC which might have been set by
# installing Qt through fink
unset QMAKESPEC

# make native Qt/Embedded
if [ $skipnative = 0 ] ; then
	if [ $skipembedded = 0 ] ; then
		echo ""
		echo "***** makeqpe.sh: making native Qt/Embedded ..."
		echo ""
		cd "$abssrcdir"/qtopia.mac/qtopia*
		export QPEDIR="$PWD"
		cd ../qt-embedded*
		export QTDIR="$PWD"
		export QTEDIR="$QTDIR"
		export PATH="$QTDIR/bin":"$OLD_PATH"
		export DYLD_LIBRARY_PATH="$QTDIR/lib"
		cp "$QPEDIR/src/qt/qconfig-qpe.h" src/tools/qconfig-qpe.h || exit 1
		./configure -platform darwin-generic-g++ -qconfig qpe -qvfb -depths 4,8,16,32 -lresolv || exit 1
		make sub-src || exit 1
	fi
fi

# make native Qt/X11
if [ $skipnative = 0 ] ; then
	echo ""
	echo "***** makeqpe.sh: making native Qt/X11 ..."
	echo ""
	cd "$abssrcdir"/qtopia.mac/qt-embedded*
	export QTEDIR="$PWD"
	cd ../qt-x11*
	export QTDIR="$PWD"
	export PATH="$QTDIR/bin":"$OLD_PATH"
	export DYLD_LIBRARY_PATH="$QTDIR/lib"
	./configure -platform darwin-g++ -lresolv -lfontconfig -I/usr/X11R6/include/freetype2 || exit 1
	make || exit 1
	make -C tools/qvfb || exit 1
	mv tools/qvfb/qvfb bin || exit 1
	cp bin/uic "$QTEDIR/bin" || exit 1
fi

# make native Qtopia
if [ $skipnative = 0 ] ; then
	echo ""
	echo "***** makeqpe.sh: making native Qtopia ..."
	echo ""
	cd "$abssrcdir"/qtopia.mac/qt-embedded*
	export QTEDIR="$PWD"
	cd ../qt-x11*
	export QTDIR="$PWD"
	cd ../qtopia*
	export QPEDIR="$PWD"
	export PATH="$QPEDIR/bin":"$OLD_PATH"
	export DYLD_LIBRARY_PATH="$QTEDIR/lib":"$QTDIR/lib"
	export QTDIR="$QTEDIR"
	cd src
	./configure -platform darwin-generic-g++ || exit 1
	make || exit 1
fi

# patch the cross-compiler includes
if [ $skipcross = 0 ] ; then
	echo ""
	echo "***** makeqpe.sh: patching cross-compiler includes ..."
	echo "***** makeqpe.sh: (only successful the first time)"
	echo "***** makeqpe.sh:"
	echo "***** makeqpe.sh: If you get asked if a reversed patch should be applied,"
	echo "***** makeqpe.sh: say no (press <n> or <enter>). Same for \"Apply anyway?\"."
	echo ""
	cd "$abssrcdir"
	patch -d "$absworkdir" -p0 <qtopia-patches/include-cross.patch
fi

# patch cross-compiled sources
if [ $skipcross = 0 ] ; then
	echo ""
	echo "***** makeqpe.sh: patching cross-compiled sources ..."
	echo ""
	cd "$abssrcdir"/qtopia.cross/qtopia*
	patch -p1 <../../qtopia-patches/qtopia-cross.patch || exit 1
	if [ $skipembedded = 0 ] ; then
		cd "$abssrcdir"/qtopia.cross/qt-embedded*
		patch -p1 <../../qtopia-patches/qt-embedded-cross.patch || exit 1
	fi
fi

# make cross-compiled Qt/Embedded
if [ $skipcross = 0 ] ; then
	if [ $skipembedded = 0 ] ; then
		echo ""
		echo "***** makeqpe.sh: making cross-compiled Qt/Embedded ..."
		echo ""
		cd "$abssrcdir"/qtopia.mac/qt-embedded*
		export QTNATIVEDIR="$PWD"
		cd ../qt-x11*
		export DYLD_LIBRARY_PATH="$QTNATIVEDIR/lib:$PWD/lib"
		cd "$abssrcdir"/qtopia.cross/qtopia*
		export QPEDIR="$PWD"
		cd ../qt-embedded*
		export QTDIR="$PWD"
		export QTEDIR="$QTDIR"
		export PATH="$QTDIR/bin":"$OLD_PATH"
		cp "$QPEDIR/src/qt/qconfig-qpe.h" src/tools/qconfig-qpe.h || exit 1
		./configure -platform linux-sharp-g++ -qconfig qpe -qvfb -depths 4,8,16,32 || exit 1
		make sub-src || exit 1
	fi
fi

# make cross-compiled Qtopia
if [ $skipcross = 0 ] ; then
	echo ""
	echo "***** makeqpe.sh: making cross-compiled Qtopia ..."
	echo ""
	cd "$abssrcdir"/qtopia.mac/qt-embedded*
	export QTNATIVEDIR="$PWD"
	cd ../qt-x11*
	export DYLD_LIBRARY_PATH="$QTNATIVEDIR/lib:$PWD/lib"
	cd "$abssrcdir"/qtopia.cross/qt-embedded*
	export QTEDIR="$PWD"
	cd ../qtopia*
	export QPEDIR="$PWD"
	export PATH="$QPEDIR/bin":"$OLD_PATH"
	export QTDIR="$QTEDIR"
	cd src
	./configure -platform linux-sharp-g++ || exit 1
	make || exit 1
fi

# patch tmake
echo ""
echo "***** makeqpe.sh: patching tmake ..."
echo ""
cd "$absworkdir"/tmake*
patch -p1 <"$abssrcdir"/qtopia-patches/tmake.patch || exit 1

echo ""
echo "***** makeqpe.sh: Success!"
echo ""


