1#!/usr/bin/env bash
2
3###############################################################################
4#
5# Copyright (C) 2009-2013 Nintendo.  All rights reserved.
6#
7# These coded instructions, statements, and computer programs contain
8# proprietary information of Nintendo of America Inc. and/or Nintendo
9# Company Ltd., and are protected by Federal copyright law.  They may
10# not be disclosed to third parties or copied or duplicated in any form,
11# in whole or in part, without the prior written consent of Nintendo.
12#
13###############################################################################
14
15if [ "$USE_CAFEX" == "1" ]
16then
17    # Set IFS to something that will not be in the args, so that BASH will pass the entire un-parsed string to CafeX
18    saveIFS="$IFS"
19    IFS=$'\n'
20    echo "Using CafeControl"
21    cafex.exe on $@
22    rval=$?
23    IFS="$saveIFS"
24    exit $rval
25fi
26
27
28###############################################################################
29# cafeon simulates launching main startup application from power on
30# assumes arguments are not options but command-line arguments for SysConfig
31###############################################################################
32
33
34###############################################################################
35# create_directory_if_noexist
36###############################################################################
37create_directory_if_noexist()
38{
39    if [ ! -d "$1" ]
40    then
41        echo "$(basename "$0"): Creating dir: $1"
42        mkdir -m 0777 -p "$1"
43    fi
44}
45
46################################################################################
47# sed
48#
49# Wrapper function for /usr/bin/sed. Needed because of a sed bug: when using
50# some non-UTF-8 encoding formats (ex: Shift-JIS), executing on a file with
51# multibyte characters, sed can hang. This function forces sed to use the UTF-8
52# encoding format, then restores to previous format.
53################################################################################
54sed()
55{
56    local saved_lang=$LANG
57    LANG=C.UTF-8
58    /usr/bin/sed "$@"
59    LANG=$saved_lang
60}
61
62###############################################################################
63# main
64###############################################################################
65
66CAFEON_DISABLE_BGD=0
67CAFEON_MORE_OPTIONS=0
68CAFEON_NOBGD_META=
69BRIDGE_PARAMETERS_WITH_E=
70CAFE_RUN_DEBUG=
71
72echo `basename "$BASH_SOURCE"`: using sed workaround
73CAFEON_OPTION_NO_DATA_SYNC=${CAFERUN_OPTION_NO_DATA_SYNC:-0}
74
75export CAFE_RUN_RUNNING=${CAFE_RUN_RUNNING:-0}
76if [ $CAFE_RUN_RUNNING -gt 0 ]
77then
78    echo "****cafeon: RECURSIVE CALL TO cafeon EXITING****"
79    exit 1
80fi
81export CAFE_RUN_RUNNING=1
82
83if [ $# -gt 0 ]
84then
85	CAFEON_MORE_OPTIONS=1
86	while [ $CAFEON_MORE_OPTIONS -eq 1 ]
87	do
88		if [ "$1" = "-noprompt" ]
89		then
90			export CATTOUCAN_TERM="source -p -q /vol/content"
91			shift
92		elif [ "$1" = "-d" ]
93		then
94			CAFE_RUN_DEBUG="${CAFE_RUN_DEBUG} $1 $2"
95			shift
96			shift
97		elif [ "$1" = "-p" ]
98		then
99			CAFE_RUN_DEBUG="${CAFE_RUN_DEBUG} $1 $2"
100			shift
101			shift
102		elif [ "$1" = "-e" ]
103		then
104			BRIDGE_PARAMETERS_WITH_E="$BRIDGE_PARAMETERS_WITH_E $1 $2"
105			shift
106			shift
107		elif [ "$1" = "-nobgd" ]
108		then
109			CAFEON_DISABLE_BGD=1
110			shift
111		elif [ "$1" = "-nosync" ]
112        then
113            if [ ! -e "$CAFE_DATA_DIR" ]
114            then
115                echo "Session data directory is missing and needs to sync'd!"
116                echo "  Please re-run without the '-nosync' option."
117                exit
118            fi
119            CAFEON_OPTION_NO_DATA_SYNC=1
120			shift
121		elif [ "$1" = "-recover" ]
122		then
123            export _CCRSYSCFG1=0x40000000
124			BOOTRUN_USE_RECOVERY_IMAGE=1
125			shift
126		else
127			CAFEON_MORE_OPTIONS=0
128		fi
129	done
130fi
131
132#Create a new version of $CAFE_META_DIR, this one with an edited meta.xml,
133#which will force the disabling of background daemons
134if [ $CAFEON_DISABLE_BGD -eq 1 ]
135then
136    CAFEON_NOBGD_META=$CAFE_TEMP/${SESSION_PATH_PREFIX}tmp_meta
137	echo "Disabling background daemons in tmp dir $CAFEON_NOBGD_META"
138
139	create_directory_if_noexist $CAFEON_NOBGD_META
140	cp $CAFE_META_DIR/* $CAFEON_NOBGD_META
141
142	sed -i -e "s|\(<bg_daemon_enable .*>\)\(.*\)\(</bg_daemon_enable>\)|\10\3|" "$CAFEON_NOBGD_META/meta.xml"
143
144	export CAFE_META_DIR=$CAFEON_NOBGD_META
145fi
146
147
148# Pull out the low portion of the OS version number to determine if the
149# SysConfig was built in debug or ndebug
150OS_VERSION_LO=$(grep -s "os_version" \
151				"$CAFE_ROOT/data/mlc/sys/title/00050010/1f700500/code/app.xml" \
152				| cut -d ">" -f2 | cut -c 9-14 )
153
154TOTAL_ARGLIST_WITH_QS=`echo $@ | sed -e "s/ /?/g" | sed -e "s/-c?/-c /g" | sed -e "s/?-c/ -c/g"`
155
156if [ -z $OS_VERSION_LO ]
157then
158	echo "error: unable to locate System Config Tool app.xml or file is corrupt"
159else
160    # Synchronize the session data directory if multisession and not suppressed
161    if [ $CAFEON_OPTION_NO_DATA_SYNC -eq 0 ]
162    then
163        syncsession
164    fi
165
166    sed -i "s|\(<argstr.*>\)\(.*\)\(</argstr.*>\)|\1system_config_tool.rpx $TOTAL_ARGLIST_WITH_QS\3|" "$CAFE_DATA_DIR/mlc/sys/title/00050010/1f700500/code/cos.xml"
167
168	if [ $OS_VERSION_LO -eq "100040" ] # NDEBUG build
169	then
170		source caferun $CAFE_RUN_DEBUG $BRIDGE_PARAMETERS_WITH_E -e mcp:launch_hint:hfiomlc -z -t 0x000500101f700500 "$CAFE_ROOT/system/bin/ghs/cafe/app/system_config_tool/base/NDEBUG/system_config_tool.rpx" "$@"
171	elif [ $OS_VERSION_LO -eq "100080" ] # DEBUG build
172	then
173	    source caferun $CAFE_RUN_DEBUG $BRIDGE_PARAMETERS_WITH_E -e mcp:launch_hint:hfiomlc -z -b -t 0x000500101f700500 "$CAFE_ROOT/system/bin/ghs/cafe/app/system_config_tool/base/DEBUG/system_config_tool.rpx" "$@"
174	fi
175	sed -i -e "s|\(<argstr.*>\)\(.*\)\(</argstr.*>\)|\1system_config_tool.rpx\3|" "$CAFE_DATA_DIR/mlc/sys/title/00050010/1f700500/code/cos.xml"
176fi
177