#!/bin/bash

if [ "$1" = "-h" -o "$1" = "--help" -o ! -r Makefile ] ; then
	echo "Usage: $0 [-q] [-f file] [-mipal [mi-pal_options]]";
	echo "       -q     will do a quick analysis using emonLogParser (from Sony)";
	echo "       -f     will read file as emon.log instead of looking";
	echo "              at \$MEMSTICK_ROOT/open-r/emon.log";
	echo "       -mipal will use the Mi-Pal tools from Griffith University";
	echo "              mi-pal_options (if any) are passed to StackedIt";
	echo "       If no options are given, '-q -mipal -2' is assumed.";
	echo "       This command will read the value of the MEMSTICK_ROOT, TEKKOTSU_ROOT,";
	echo "       and FILENAME_CASE environment variables to find the emon.log file";
	echo "       and other tools.";
	echo "       This should be run from within the project directory";
	echo "       which caused the crash";
fi;

if [ "$1" = "-h" -o "$1" = "--help" ] ; then
	exit 2;
fi;

BUILDDIR=`grep "^PROJECT_BUILDDIR.*=" Makefile | cut -f 2- -d =`;
if [ ! "$BUILDDIR" ] ; then
	BUILDDIR=build;
	echo "WARNING: could not find PROJECT_BUILDDIR specification in Makefile.";
	echo "         Defaulting to '$BUILDDIR'.";
fi;
if [ ! "$TEKKOTSU_TARGET_MODEL" ] ; then
	TEKKOTSU_TARGET_MODEL=TGT_ERS2xx;
fi;
BUILDDIR=`echo $BUILDDIR/PLATFORM_APERIOS_$TEKKOTSU_TARGET_MODEL`; #echo removes leading spaces

if [ ! -d "$BUILDDIR" ] ; then
	echo "ERROR: Build directory '$BUILDDIR' does not exist";
	echo "Are you in the project directory for the code that was running";
	echo "at the time of the crash?  You're supposed to be... otherwise";
	echo "how am I supposed to know which project you're debugging? ;)";
	exit 1;
fi;

runquick=;
if [ "$1" = "-q" ] ; then
	runquick=true;
	shift;
fi;

emonfile=;
usefile=;
if [ "$1" = "-f" ] ; then
	usefile=true;
	emonfile="$2";
	shift; shift;
fi;

runmipal=;
if [ "$1" = "-mipal" ] ; then
	runmipal=true;
	shift;

	options=$*;
	if [ ! "$options" ] ; then
		options=-2
	fi;
fi;

if [ -z "$runquick" -a -z "$runmipal" -a $# -eq 0 ] ; then
	runquick=true;
	runmipal=true;
	options=-2;
fi;

if [ -z "$TEKKOTSU_ROOT" ] ; then
	TEKKOTSU_ROOT=/usr/local/Tekkotsu;
fi;
if [ ! -d "$TEKKOTSU_ROOT" ] ; then
	echo "ERROR: Could not find TEKKOTSU_ROOT: $TEKKOTSU_ROOT";
	exit 1;
fi;

if [ -z "$MEMSTICK_ROOT" ] ; then
	MEMSTICK_ROOT=/mnt/memstick;
fi;

if [ -z "FILENAME_CASE" ] ; then
	FILENAME_CASE=lower;
fi;

cd "$BUILDDIR"

if [ "$FILENAME_CASE"="lower" ] ; then
	ln -sf mmcombo.nosnap.elf MainObj.nosnap.elf;
	ln -sf mmcombo.nosnap.elf MotoObj.nosnap.elf;
	ln -sf sndplay.nosnap.elf SoundPlay.nosnap.elf;
	if [ -z "$emonfile" ] ; then
		emonfile=${MEMSTICK_ROOT}/open-r/emon.log;
	fi;
else
	ln -sf MMCOMBO.nosnap.elf MainObj.nosnap.elf;
	ln -sf MMCOMBO.nosnap.elf MotoObj.nosnap.elf;
	ln -sf SNDPLAY.nosnap.elf SoundPlay.nosnap.elf;
	if [ -z "$emonfile" ] ; then
		emonfile=${MEMSTICK_ROOT}/OPEN-R/EMON.LOG;
	fi;
fi;

# remove any previous disassembly files
rm -f aiboDis*.ass;
# just so the elp.pl doesn't complain about not finding any previous
touch aiboDis0.ass;

if [ -z "$usefile" ] ; then
	${TEKKOTSU_ROOT}/tools/mntmem;
	if [ $? -ne 0 ] ; then
		echo "ERROR: Memory stick not mounted.";
		exit 1;
	fi;
fi;

if [ "$runquick" ] ; then
	grep "exception code:" ${emonfile}
	${TEKKOTSU_ROOT}/tools/emonLogParser ${emonfile} > quick-out.txt;
	file=`sed -n '/object:/s/object:	*//p' quick-out.txt`;
	${TEKKOTSU_ROOT}/tools/emonLogParser ${emonfile} ${file}.nosnap.elf;
fi;

if [ "$runmipal" ] ; then
	if [ "$runquick" ] ; then
		echo "";
	fi;
	printf "Using StackedIt options: $options\n";
	printf "Generating disassembly (this can take a *long* time)...";
	${TEKKOTSU_ROOT}/tools/mipaltools/elp.pl -e ${emonfile} > elp-out.txt;
	echo "done.";
	
	LINES=`cat elp-out.txt | wc -l`
	
	echo "*** Using elp.pl by Stuart Seymon (v2.0, 5/9/2003) ***"
	echo "***  Mi-Pal,  Griffith University, Australia  ***"
	echo " see ${TEKKOTSU_ROOT}/tools/mipaltools/ for more info"
	if [ $LINES -lt 7 ] ; then
		cat elp-out.txt
		exit 1;
	fi;
	head -`expr $LINES - 7` elp-out.txt;
	
	EPC=`grep "^EPC:" elp-out.txt | cut -f 2 -d x`;
	RT=`grep "^linkTimeAddr:" elp-out.txt | cut -f 2 -d x`;
	
	echo
	echo "*** Using StackedIt by Joel Fenwick (v2.0, 1/9/2003) ***"
	echo "***   Mi-Pal,  Griffith University, Australia    ***"
	echo " see ${TEKKOTSU_ROOT}/tools/mipaltools/ for more info"
	${TEKKOTSU_ROOT}/tools/mipaltools/StackedIt $options $EPC $RT aiboDis1.ass ${emonfile};
	
	if [ "$usefile" ] ; then
		${TEKKOTSU_ROOT}/tools/umntmem;
	fi;
fi;

