#!/bin/sh

# Script for creating a 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

# Loop over all args
echo ""
echo "***** makecross.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
		;;
	*)
		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: makecross.sh --srcdir <source directory> --workdir <work directory> --arcdir <archive directory>"
	exit 1
fi

# Construct absolute paths for all dirs
echo ""
echo "***** makecross.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 archive directory looks alright
echo ""
echo "***** makecross.sh: checking if the needed archives can be found ..."
echo ""
numarcs=`ls -l "$absarcdir" | grep "glibc" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one glibc archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "linux-headers" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one linux-headers archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "binutils" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one binutils archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "gcc-core" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one gcc-core archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "gcc-g++" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one gcc-g++ archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "gcc-objc" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one gcc-objc archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "gcc-patches" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one gcc-patches archive in --arcdir"
	exit 1
fi
numarcs=`ls -l "$absarcdir" | grep "rpm2cpio" | wc -l`
if [ $numarcs != 1 ]; then
	echo "Please make sure there is exactly one rpm2cpio archive in --arcdir"
	exit 1
fi

# extract the names of the archives we need
glibc_arc=`ls "$absarcdir"/glibc*`
linux_headers_arc=`ls "$absarcdir"/linux-headers*`
binutils_arc=`ls "$absarcdir"/binutils*`
gcc_core_arc=`ls "$absarcdir"/gcc-core*`
gcc_gxx_arc=`ls "$absarcdir"/gcc-g++*`
gcc_objc_arc=`ls "$absarcdir"/gcc-objc*`
gcc_patches_arc=`ls "$absarcdir"/gcc-patches*`
rpm2cpio_arc=`ls "$absarcdir"/rpm2cpio*`

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

# extract the rpm2cpio tool
cd "$absworkdir"
mkdir rpm2cpio
cd rpm2cpio
(gnutar xzf "$rpm2cpio_arc" || bzcat "$rpm2cpio_arc" | gnutar xf -) || (echo "Cannot extract rpm2cpio tool" && exit 1)
rpm2cpio_script="$absworkdir/rpm2cpio/bin/rpm2cpio"

# extract the archives in the work directory
cd "$absworkdir"
perl "$rpm2cpio_script" "$glibc_arc" | cpio -di || exit 1
perl "$rpm2cpio_script" "$linux_headers_arc" | cpio -di || exit 1

# extract the archives in the source directory
cd "$abssrcdir"
(gnutar xzf "$binutils_arc" || bzcat "$binutils_arc" | gnutar xf -) || (echo "Cannot extract binutils source" && exit 1)
(gnutar xzf "$gcc_core_arc" || bzcat "$gcc_core_arc" | gnutar xf -) || (echo "Cannot extract gcc core source" && exit 1)
(gnutar xzf "$gcc_gxx_arc" || bzcat "$gcc_gxx_arc" | gnutar xf -) || (echo "Cannot extract gcc g++ source" && exit 1)
(gnutar xzf "$gcc_objc_arc" || bzcat "$gcc_objc_arc" | gnutar xf -) || (echo "Cannot extract gcc objc source" && exit 1)
(gnutar xzf "$gcc_patches_arc" || bzcat "$gcc_patches_arc" | gnutar xf -) || (echo "Cannot extract gcc patches" && exit 1)

# make binutils
echo ""
echo "***** makecross.sh: making binutils ..."
echo ""
cd binutils*
./configure --target=arm-linux --prefix="$absworkdir/opt/Embedix/tools" || exit 1
make || exit 1
make install || exit 1
PATH="$PATH":"$absworkdir/opt/Embedix/tools/bin"

# make gcc
echo ""
echo "***** makecross.sh: making gcc ..."
echo ""
cd "$abssrcdir"
patch -p0 <gcc-patches/gcc-2.95.3.patch || exit 1
cd "gcc-2.95.3"
./configure --target=arm-linux --prefix="$absworkdir/opt/Embedix/tools" --with-headers="$absworkdir/opt/Embedix/tools/arm-linux/include" --with-libs="$absworkdir/opt/Embedix/tools/arm-linux/lib" || exit 1
make || exit 1
make install || exit 1
PATH="$PATH"

# make a test program
echo ""
echo "***** makecross.sh: compiling test program ..."
echo ""
cd "$abssrcdir"
printf '#include <iostream>\nusing namespace std; int main() { cout << "Hello World!\\n"; return 0; }' >test.cpp
arm-linux-gcc test.cpp -lstdc++ || exit 1

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

