1#!/bin/bash -f
2
3#---------------------------------------------------------------------------
4#  Project:  TwlSDK - tools - loadrun
5#  File:     nitrorun.TWL
6#
7#  Copyright 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:: 2007-00-00#$
16#  $Rev: 0 $
17#  $Author: nobody $
18#---------------------------------------------------------------------------
19
20#---- SDK root directory
21SDKROOT=`cygpath -u "$TWLSDK_ROOT"`
22
23#---- Run command
24LOADRUN=$SDKROOT/tools/bin/loadrun.TWL.exe
25BURYARG=$SDKROOT/tools/bin/buryarg.TWL.exe
26COMMANDNAME=`basename $0`
27
28#---- Command options
29BOPTION=""
30LOPTION=""
31
32#---- Run flags
33gVerboseMode=0
34gHelpMode=0
35gIsError=0
36gNoExecMode=0
37
38#---- Temporary filename
39tmpFile=_nitrorun_TWL_tmp@@.$$
40
41#----------------------------------------------------------------
42# showVersion
43#		Display the version
44#
45function showVersion()
46{
47    echo "$COMMANDNAME  1.0  Copyright 2008 Nintendo. All right reserved."
48    #
49    # 1.0  First release (copied and revised nitrorun)
50    #
51}
52
53#----------------------------------------------------------------
54# showUsage
55#		Display the usage
56#
57function showUsage()
58{
59    echo "Usage: $COMMANDNAME [OPTION] [<TwlSrlFile> [ARGUMENT]...]"
60    echo "Execute buryarg and loadrun."
61    echo ""
62    echo "Options:"
63    echo "  -h, --help            show this help."
64    echo "      --version         show version."
65    echo "  -v, --verbose         verbose mode."
66    echo "      --bopt            specify options for buryarg.TWL."
67    echo "      --lopt            specify options for loadrun.TWL."
68    echo "  -n, --noexec          do not execute commands. (for test)"
69    echo ""
70    echo "Example:"
71    echo "  $COMMANDNAME --lopt \"-t30 -a\\\"ABORT\\\" -l\" main.srl 100 300"
72    echo "      is done as:  "
73    echo "  buryarg.TWL --stdout main.srl 100 300 > temporaryFile"
74    echo "  loadrun.TWL -t30 -a\"ABORT\" -l temporaryFile"
75}
76
77#----------------------------------------------------------------
78# checkError
79# 		Error check up to here
80#
81function checkError
82{
83	local isShowUsage=0
84
85	if [ $gIsError -eq 1 ];
86	then
87		exit 1
88	fi;
89
90	if [ $gHelpMode -eq 1 ];
91	then
92		isShowUsage=1
93	fi;
94
95	if [ $ARGC -eq 0 ];
96	then
97		isShowUsage=1
98	fi;
99
100	if [ $isShowUsage -eq 1 ];
101	then
102		showUsage
103		exit 1
104	fi;
105}
106
107#----------------------------------------------------------------
108# checkExistence
109#		Check existence of file
110#
111function checkExistence()
112{
113	#---- Does the buryarg command exist?
114	if [ ! -e $BURYARG ];
115	then
116		gIsError=1
117		echo "*** Error: $COMMANDNAME: buryarg.TWL command not exist"
118		return
119	fi;
120
121	#---- Does the loadrun command exist?
122	if [ ! -e $LOADRUN ];
123	then
124		gIsError=1
125		echo "*** Error: $COMMANDNAME: loadrun.TWL command not exist"
126		return
127	fi;
128
129	#---- Does the SRL file exist?
130	if [ ! -e $1 ];
131	then
132		gIsError=1
133		echo "*** Error: $COMMANDNAME: srl file not exist"
134		return
135	fi;
136
137	#---- Can create temporary file?
138	touch $tmpFile
139	if [ ! -e $tmpFile ];
140	then
141		gIsError=1
142		echo "*** Error: $COMMANDNAME: cannot create temporary file"
143		return
144	fi;
145}
146
147function deleteExit
148{
149    \rm -f $tmpFile
150    exit $1
151}
152
153#================================================================
154# Main
155#		Main
156#
157
158#---- Signal trap
159trap deleteExit 1 INT
160trap deleteExit 2 HUP
161trap deleteExit 3 QUIT
162
163#---------------- Check options
164while [ 1 ];
165do
166	case $1 in
167		# Help
168		--help|-help|-h)
169			gHelpMode=1
170			shift 1
171			continue
172			;;
173		# Version
174		--version|-version)
175			showVersion
176			exit 1
177			;;
178		# Options passed to buryarg
179		--bopt*|-bopt*)
180			BOPTION=$2
181			shift 2
182			continue
183			;;
184		# Options passed to loadrun
185		--lopt*|-lopt*)
186			LOPTION=$2
187			shift 2
188			continue
189			;;
190		# Verbose mode
191		--verbose|-v)
192			gVerboseMode=1
193			shift 1
194			continue
195			;;
196		# Do not execute
197		--noexec|-n)
198			gNoExecMode=1
199			shift 1
200			continue
201			;;
202		# Other options
203		-*)
204			gIsError=1
205			echo "*** Error: $COMMANDNAME: Illegal option"
206			break;
207			;;
208		# Non-options
209		*)
210			break;
211			;;
212	esac
213
214done
215
216#---------------- Error check for whether file exists or not
217ARGC=$#
218
219if [ $gIsError -eq 0 ];
220then
221	checkExistence $1
222fi;
223
224checkError
225
226#---------------- Execute command
227{
228	if [ $gVerboseMode -eq 1 ];
229	then
230		echo "buryarg.TWL option: $BOPTION"
231		echo "loadrun.TWL option: $LOPTION"
232	fi;
233
234	if [ $gVerboseMode -eq 1 ];
235	then
236		echo "$BURYARG $BOPTION --stdout $@  > $tmpFile"
237		echo "$LOADRUN $LOPTION $tmpFile"
238	fi;
239
240
241	if [ $gNoExecMode -eq 0 ];
242	then
243		#---- Sequentially execute two commands
244		$BURYARG $BOPTION --stdout "$@"  > $tmpFile
245		$LOADRUN $LOPTION $tmpFile
246	fi;
247}
248
249deleteExit 0
250