#!/bin/bash -f

#---------------------------------------------------------------------------
#  Project:  NitroSDK - tools - loadrun
#  File:     nitrorun
#
#  Copyright 2005 Nintendo.  All rights reserved.
#
#  These coded instructions, statements, and computer programs contain
#  proprietary information of Nintendo of America Inc. and/or Nintendo
#  Company Ltd., and are protected by Federal copyright law.  They may
#  not be disclosed to third parties or copied or duplicated in any form,
#  in whole or in part, without the prior written consent of Nintendo.
#
#  $Log: nitrorun,v $
#  Revision 1.4  09/09/2005 05:38:23  yada
#  applied for ""-ed string containing space characters
#
#  Revision 1.3  09/02/2005 09:21:40  kitase_hirotake
#  tr to cygpath
#
#  Revision 1.2  08/31/2005 05:58:51  yada
#  supported '-version'
#
#  Revision 1.1  08/30/2005 08:37:19  yada
#  first release
#
#  $NoKeywords: $
#---------------------------------------------------------------------------

#---- SDK root directory
SDKROOT=`cygpath -u "$NITROSDK_ROOT"`

#---- Run command
LOADRUN=$SDKROOT/tools/bin/loadrun.exe
BURYARG=$SDKROOT/tools/bin/buryarg.exe
COMMANDNAME=`basename $0`

#---- Command options
BOPTION=""
LOPTION=""

#---- Run flags
gVerboseMode=0
gHelpMode=0
gIsError=0
gNoExecMode=0

#----------------------------------------------------------------
# showVersion
#		Display the version
#
function showVersion()
{
    echo "$COMMANDNAME  1.2  Copyright 2005 Nintendo. All right reserved."
    #
    # 1.2  Can now accept character strings that include white space as arguments if they are surrounded by ""
    # 1.1 Included version display
    # 1.0 first release
    #
}

#----------------------------------------------------------------
# showUsage
#		Display the usage
#
function showUsage()
{
    echo "Usage: $COMMANDNAME [OPTION] [<SrlFile> [ARGUMENT]...]"
    echo "Execute buryarg and loadrun."
    echo ""
    echo "Options:"
    echo "  -h, --help            show this help."
    echo "      --version         show version."
    echo "  -v, --verbose         verbose mode."
    echo "      --bopt            specify options for buryarg."
    echo "      --lopt            specify options for loadrun."
    echo "  -n, --noexec          do not execute commands. (for test)"
    echo ""
    echo "Example:"
    echo "  $COMMANDNAME --lopt \"-t30 -a\\\"ABORT\\\" -l\" main.srl 100 300"
    echo "      is done as:  "    
    echo "  buryarg --stdout main.srl 100 300 | loadrun --stdin -t30 -a\"ABORT\" -l"
}

#----------------------------------------------------------------
# checkError
# 		Error check up to here
#
function checkError
{
	local isShowUsage=0

	if [ $gIsError -eq 1 ];
	then
		exit 1
	fi;

	if [ $gHelpMode -eq 1 ];
	then
		isShowUsage=1
	fi;

	if [ $ARGC -eq 0 ];
	then
		isShowUsage=1
	fi;

	if [ $isShowUsage -eq 1 ];
	then
		showUsage
		exit 1
	fi;
}

#----------------------------------------------------------------
# checkExistence
#		Check existence of file
#
function checkExistence()
{
	#---- Does the buryarg command exist?
	if [ ! -e $BURYARG ];
	then
		gIsError=1
		echo "*** Error: $COMMANDNAME: buryarg command not exist"
		return
	fi;

	#---- Does the loadrun command exist?
	if [ ! -e $LOADRUN ];
	then
		gIsError=1
		echo "*** Error: $COMMANDNAME: loadrun command not exist"
		return
	fi;

	#---- Does the srl file exist?
	if [ ! -e $1 ];
	then
		gIsError=1
		echo "*** Error: $COMMANDNAME: srl file not exist"
		return
	fi;
}

#================================================================
# main
#		Main
#

#---------------- Check options
while [ 1 ];
do
	case $1 in
		# Help
		--help|-help|-h)
			gHelpMode=1
			shift 1
			continue
			;;
		# Version
		--version|-version)
			showVersion
			exit 1
			;;
		# Options passed to buryarg
		--bopt*|-bopt*)
			BOPTION=$2
			shift 2
			continue
			;;
		# Options passed to loadrun
		--lopt*|-lopt*)
			LOPTION=$2
			shift 2
			continue
			;;
		# verbose mode
		--verbose|-v)
			gVerboseMode=1
			shift 1
			continue
			;;
		# Do not execute
		--noexec|-n)
			gNoExecMode=1
			shift 1
			continue
			;;
		# Other options
		-*)
			gIsError=1
			echo "*** Error: $COMMANDNAME: Illegal option"
			break;
			;;
		# Not an option
		*)
			break;
			;;
	esac

done

#---------------- Error check for whether file exists or not
ARGC=$#

if [ $gIsError -eq 0 ];
then
	checkExistence $1
fi;

checkError

#---------------- Execute command
{
	if [ $gVerboseMode -eq 1 ];
	then
		echo "buryarg option: $BOPTION"
		echo "loadrun option: $LOPTION"
	fi;

	if [ $gVerboseMode -eq 1 ];
	then
		echo "$BURYARG $BOPTION --stdout $@ | $LOADRUN --stdin $LOPTION"
	fi;


	if [ $gNoExecMode -eq 0 ];
	then
		#---- Link and execute two commands using a pipe
		$BURYARG $BOPTION --stdout "$@"  | $LOADRUN --stdin $LOPTION
	fi;
}

exit 0
