#! /bin/bash -f

#---------------------------------------------------------------------------
#  Project:  TwlSDK - tools - loadrun
#  File:     nitrorun
#
#  Copyright 2005-2008 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.
#
#  $Date:: 2008-09-18#$
#  $Rev: 8573 $
#  $Author: okubata_ryoma $
#---------------------------------------------------------------------------

#---- SDK root directory
SDKROOT=`cygpath -u "$TWLSDK_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 quotation marks.
    # 1.1 Included version display
    # 1.0 First release
    #
}

#----------------------------------------------------------------
# showUsage
#		Display the usage
#
function showUsage()
{
    echo "Usage: $COMMANDNAME [OPTION] [<NitroSrlFile> [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;
			;;
		# Non-options
		*)
			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
