1#! /bin/bash -f
2
3#---------------------------------------------------------------------------
4#  Project:  TwlSDK - tools - loadrun
5#  File:     nitrorun
6#
7#  Copyright 2005-2008 Nintendo. All rights reserved.
8#
9#  These coded instructions, statements, and computer programs contain
10#  proprietary information of Nintendo of America Inc. and/or Nintendo
11#  Company Ltd., and are protected by Federal copyright law. They may
12#  not be disclosed to third parties or copied or duplicated in any form,
13#  in whole or in part, without the prior written consent of Nintendo.
14#
15#  $Date:: 2008-09-18#$
16#  $Rev: 8573 $
17#  $Author: okubata_ryoma $
18#---------------------------------------------------------------------------
19
20#---- SDK root directory
21SDKROOT=`cygpath -u "$TWLSDK_ROOT"`
22
23#---- Run command
24LOADRUN=$SDKROOT/tools/bin/loadrun.exe
25BURYARG=$SDKROOT/tools/bin/buryarg.exe
26COMMANDNAME=`basename $0`
27
28#---- Command options
29BOPTION=""
30LOPTION=""
31
32#---- Run flags
33gVerboseMode=0
34gHelpMode=0
35gIsError=0
36gNoExecMode=0
37
38#----------------------------------------------------------------
39# showVersion
40#		Display the version
41#
42function showVersion()
43{
44    echo "$COMMANDNAME  1.2  Copyright 2005 Nintendo. All right reserved."
45    #
46    # 1.2 Can now accept character strings that include white space as arguments if they are surrounded by quotation marks.
47    # 1.1 Included version display
48    # 1.0 First release
49    #
50}
51
52#----------------------------------------------------------------
53# showUsage
54#		Display the usage
55#
56function showUsage()
57{
58    echo "Usage: $COMMANDNAME [OPTION] [<NitroSrlFile> [ARGUMENT]...]"
59    echo "Execute buryarg and loadrun."
60    echo ""
61    echo "Options:"
62    echo "  -h, --help            show this help."
63    echo "      --version         show version."
64    echo "  -v, --verbose         verbose mode."
65    echo "      --bopt            specify options for buryarg."
66    echo "      --lopt            specify options for loadrun."
67    echo "  -n, --noexec          do not execute commands. (for test)"
68    echo ""
69    echo "Example:"
70    echo "  $COMMANDNAME --lopt \"-t30 -a\\\"ABORT\\\" -l\" main.srl 100 300"
71    echo "      is done as:  "
72    echo "  buryarg --stdout main.srl 100 300 | loadrun --stdin -t30 -a\"ABORT\" -l"
73}
74
75#----------------------------------------------------------------
76# checkError
77# 		Error check up to here
78#
79function checkError
80{
81	local isShowUsage=0
82
83	if [ $gIsError -eq 1 ];
84	then
85		exit 1
86	fi;
87
88	if [ $gHelpMode -eq 1 ];
89	then
90		isShowUsage=1
91	fi;
92
93	if [ $ARGC -eq 0 ];
94	then
95		isShowUsage=1
96	fi;
97
98	if [ $isShowUsage -eq 1 ];
99	then
100		showUsage
101		exit 1
102	fi;
103}
104
105#----------------------------------------------------------------
106# checkExistence
107#		Check existence of file
108#
109function checkExistence()
110{
111	#---- Does the buryarg command exist?
112	if [ ! -e $BURYARG ];
113	then
114		gIsError=1
115		echo "*** Error: $COMMANDNAME: buryarg command not exist"
116		return
117	fi;
118
119	#---- Does the loadrun command exist?
120	if [ ! -e $LOADRUN ];
121	then
122		gIsError=1
123		echo "*** Error: $COMMANDNAME: loadrun command not exist"
124		return
125	fi;
126
127	#---- Does the SRL file exist?
128	if [ ! -e $1 ];
129	then
130		gIsError=1
131		echo "*** Error: $COMMANDNAME: srl file not exist"
132		return
133	fi;
134}
135
136#================================================================
137# Main
138#		Main
139#
140
141#---------------- Check options
142while [ 1 ];
143do
144	case $1 in
145		# Help
146		--help|-help|-h)
147			gHelpMode=1
148			shift 1
149			continue
150			;;
151		# Version
152		--version|-version)
153			showVersion
154			exit 1
155			;;
156		# Options passed to buryarg
157		--bopt*|-bopt*)
158			BOPTION=$2
159			shift 2
160			continue
161			;;
162		# Options passed to loadrun
163		--lopt*|-lopt*)
164			LOPTION=$2
165			shift 2
166			continue
167			;;
168		# Verbose mode
169		--verbose|-v)
170			gVerboseMode=1
171			shift 1
172			continue
173			;;
174		# Do not execute
175		--noexec|-n)
176			gNoExecMode=1
177			shift 1
178			continue
179			;;
180		# Other options
181		-*)
182			gIsError=1
183			echo "*** Error: $COMMANDNAME: Illegal option"
184			break;
185			;;
186		# Non-options
187		*)
188			break;
189			;;
190	esac
191
192done
193
194#---------------- Error check for whether file exists or not
195ARGC=$#
196
197if [ $gIsError -eq 0 ];
198then
199	checkExistence $1
200fi;
201
202checkError
203
204#---------------- Execute command
205{
206	if [ $gVerboseMode -eq 1 ];
207	then
208		echo "buryarg option: $BOPTION"
209		echo "loadrun option: $LOPTION"
210	fi;
211
212	if [ $gVerboseMode -eq 1 ];
213	then
214		echo "$BURYARG $BOPTION --stdout $@ | $LOADRUN --stdin $LOPTION"
215	fi;
216
217
218	if [ $gNoExecMode -eq 0 ];
219	then
220		#---- Link and execute two commands using a pipe
221		$BURYARG $BOPTION --stdout "$@"  | $LOADRUN --stdin $LOPTION
222	fi;
223}
224
225exit 0
226