#!/bin/bash

if [ "$1" = "-h" -o "$1" = "--help" -o ! -r Makefile ] ; then
	echo "Usage: $0 [-q] [-mipal [mi-pal_options]]";
	echo "       -q     will do a quick analysis using emonLogParser (from Sony)";
	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 "^BUILDDIR.*=" Makefile | cut -f 2- -d =`;
if [ ! "$BUILDDIR" ] ; then
	echo "WARNING: could not find BUILDDIR specification in Makefile.";
	echo "         Defaulting to 'build'.";
	BUILDDIR=build;
fi;

if [ ! -d "$BUILDDIR" ] ; then
	echo "ERROR: Build directory '$BUILDDIR' does not exist";
	exit 1;
fi;

runquick=;
if [ "$1" = "-q" ] ; then
	runquick=true;
	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;
	EMON=open-r/emon.log;
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;
	EMON=OPEN-R/EMON.LOG;
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;

${TEKKOTSU_ROOT}/tools/mntmem;
if [ $? -ne 0 ] ; then

	echo "ERROR: Memory stick not mounted.";
	exit 1;

else

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

	if [ "$runmipal" ] ; then
		if [ "$runquick" ] ; then
			echo "";
		fi;
		printf "Using StackedIt options: $options\n";
		printf "Generating disassembly...";
		${TEKKOTSU_ROOT}/tools/mipaltools/elp.pl -e ${MEMSTICK_ROOT}/${EMON} > 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"
		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 ${MEMSTICK_ROOT}/${EMON};
		
		${TEKKOTSU_ROOT}/tools/umntmem;
	fi;

fi;

