#!/bin/bash 

# build_inkscape.sh  -  version 0.1
# originally written by Armagon - armagon (underscore) ca (at) yahoo (dot) ca
# Feel free to use and modify it for your own purposes

# This script automates the cross-compilation of inkscape for pdaXrom.
# It does not do error checking, but does store the output of configure
# and make in a logs directory.  Scroll down, or run the script without arguments, 
# to see what the command-line parameters are.

# it was designed to be run in ~/Zaurus/Cross, and it expects the following
# subdirectories, but the names are easily modifiable (see the 
# "set up environment variables" section):
#  - logs     - output from ./configure, ./make, etc.
#  - install  - where the libraries, executables, docs, etc, are placed
# and directories for the source code:
#  - inkscape-0.43
#  - gc6.5
#  - libsigc++-2.0.17
#  - glibmm-2.8.4
#  - gtkmm-2.6.5

# If you invoke this script to patch inkscape's autoconf file, you will need
# koen's patch in ~/Zaurus/Cross; get it from 
# http://dominion.kabel.utwente.nl/koen/pda/files/no-boehm-version-check.patch

# For more information, you may want to see the thread in which I was able to get
# inkscape compiling: http://www.oesf.org/forums/index.php?showtopic=17787
# Post 19, with build notes, may be particularly useful.
# Also highly useful is the how to compile inkscape page on the inkscape wiki:
# http://wiki.inkscape.org/wiki/index.php/CompilingInkscape

# Other notes:
# You can easily change the compiler flags in the environment variable section.
# It is handy to run a shell afterwards if you are trying any custom steps.
# I have gotten bad results trying to compile with iwmmx support.  I haven't 
# nailed it down yet.  I have not yet tested make dynamic libraries, but 
# I expect it will work.  (You can change that in the BuildLibrary function.)


# Good luck!

function ShowUsage
{
	echo ""
	echo "To use $0, specify one or more of the following parameters on the command line:"
	echo "   shell -- makes the program run a shell with the environment variables set"
	echo "   clean -- removes previously built object files"
	echo "   libs  -- builds the libraries"
	echo "   patch -- patches inkscape autoconfigure file, and regenerates configure"
	echo "   ink   -- builds inkscape"
	echo "   mmx   -- builds with iwmmxt support (for C1000 and C3x00)"
	#echo "   all   -- builds the libraries and inkscape"
	echo "Example invocation: $0 shell libs ink"
	echo "Note: This is not a makefile; it does not resolve dependancies!"
	echo ""
	
	exit
}

# did they issue any commands?
if [ -z "$1" ]; then 
	ShowUsage
fi

# look at the command line parameters
until [ -z "$1" ]; do		# while there is a parameter
	
	if [ "$1" = "shell" ]; then CREATE_SHELL=1
	elif [ "$1" = "libs" ]; then MAKE_LIBRARIES=1
	elif [ "$1" = "ink" ]; then MAKE_INKSCAPE=1
	elif [ "$1" = "clean" ]; then DO_CLEAN=1
	elif [ "$1" = "patch" ]; then DO_PATCH_CONFIGURE=1
	elif [ "$1" = "mmx" ]; then USE_MMX=1
	elif [ "$1" = "--help" ]; then
		ShowUsage
	elif [ "$1" = "-h" ]; then
		ShowUsage
	fi
	
	shift		# go to the next parameter
	
done


# SET UP ENVIRONMENT VARIABLES!

# specify absolute paths for these directories
BASE_DIR=~/Zaurus/Cross
INSTALL_DIR=$BASE_DIR/install
LOG_DIR=$BASE_DIR/logs

# these directories use relative paths; they should all be in $BASE_DIR
INKSCAPE="inkscape-0.43"
BOEHM_GC="gc6.5"
LIBSIGCPP="libsigc++-2.0.17"
GLIBMM="glibmm-2.8.4"
GTKMM="gtkmm-2.6.5"

# where is the patch file, relative to the inkscape directory?
INKSCAPE_AUTOCONF_PATCH="../no-boehm-version-check.patch"

# set up compilation flags
# C compiler flags
export CFLAGS="-w"
export CFLAGS="$CFLAGS -O2 -fomit-frame-pointer"

if [ $USE_MMX ]; then
	export CFLAGS="$CFLAGS -march=iwmmxt -mcpu=iwmmxt"
	# I think I read that the -mtune=iwmmxt flag causes problems
fi

#c++ compiler flags
export CXXFLAGS=$CFLAGS
# linker flags
export LDFLAGS="-L$INSTALL_DIR/lib -s"
# c++ preprocessor flags
export CPPFLAGS="-I$INSTALL_DIR/include"



# misc
export M4=`which m4`
export PKG_CONFIG_PATH=$INSTALL_DIR/lib/pkgconfig:$PKG_CONFIG_PATH

# and configure flags
export CONFIGURE="./configure  --host=armv5tel-cacko-linux --build=i686-linux --prefix=$INSTALL_DIR"
export CONFIGURE_STATIC="$CONFIGURE  --enable-static --disable-shared"

echo "Environment variables have been set."


# CLEAN UP
function DoClean
{
	echo "   Cleaning $1"
	cd $1
	make clean  > $LOG_DIR/$1-clean.txt 2> $LOG_DIR/$1-clean-err.txt
	cd ..
}


if [ $DO_CLEAN ]; then
	DoClean $BOEHM_GC 
	DoClean $LIBSIGCPP
	DoClean $GLIBMM
	DoClean $GTKMM
	DoClean $INKSCAPE
fi

# BUILD THE LIBRARIES

# BuildLibrary takes two parameters
# $1 = the libraries directory name
# $2 = any additional flags for configure
# This function configures, makes and installs the libraries,
# and puts logs files in $LOG_DIR
function BuildLibrary
{
	echo "    Building $1"
	cd $1
	
	# this next line makes static libraries
	$CONFIGURE_STATIC $2 > $LOG_DIR/$1-conf.txt 2> $LOG_DIR/$1-conf-err.txt

	# or comment it out and use this line to compile dynamic libraries (untested)
	#$CONFIGURE $2 > $LOG_DIR/$1-conf.txt 2> $LOG_DIR/$1-conf-err.txt
	
	make  > $LOG_DIR/$1-make.txt 2> $LOG_DIR/$1-make-err.txt
	make install  > $LOG_DIR/$1-install.txt 2> $LOG_DIR/$1-install-err.txt
	cd ..
	echo "    Done"
	echo ""
}


if [ $MAKE_LIBRARIES ]; then

	echo "Building Libraries"
	
	BuildLibrary $BOEHM_GC
	BuildLibrary $LIBSIGCPP
	BuildLibrary $GLIBMM
	BuildLibrary $GTKMM
	
	echo ""
	echo "Done building all libraries."

fi



# BUILD INKSCAPE

if [ $DO_PATCH_CONFIGURE ]; then
	echo "Applying patch"
	cd $INKSCAPE
	patch -i $INKSCAPE_AUTOCONF_PATCH
	echo "Regenerating configure"
	autoconf
	cd ..
	echo "Done applying patch"
fi


if [ $MAKE_INKSCAPE ]; then
	
	# to be determined
	echo "Building inkscape!"

	INK=$INKSCAPE
	cd $INKSCAPE
	echo "     Configuring Inkscape"
	$CONFIGURE > $LOG_DIR/$INK-conf.txt 2> $LOG_DIR/$INK-conf-err.txt
		
	# change the config.h file, so that instead of paths in ~/Zaurus/Cross/install,
	# all the paths are in /usr .  This makes it so that inkscape can find its assets.
	sed -ibak s[$INSTALL_DIR[/usr[ config.h 

	# finally, build it
	echo "     Making Inkscape"
	make  > $LOG_DIR/$INK-make.txt 2> $LOG_DIR/$INK-make-err.txt

	echo "     Installing Inkscape"
	make install  > $LOG_DIR/$INK-install.txt 2> $LOG_DIR/$INK-install-err.txt
	cd ..
	echo "Done."
fi


# EXIT
# was a shell requested?
if [ $CREATE_SHELL ]; then
	echo "Type 'exit' to leave the inkscape build environment"
	/bin/bash
	exit
fi
