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
15# We need to check if the script was called with or without source,
16# so we can either return or exit the proper codes.
17##############################################################
18sourced()
19{
20  if [ "${FUNCNAME[1]}" = source ]
21  then
22    return 0
23  else
24    return 1
25  fi
26}
27##############################################################
28
29##############################################################
30# main
31##############################################################
32
33# The script's return/exit variable
34rval=0
35
36# Find pcfsserver.exe
37if [ -f "$MION_BRIDGE_TOOLS/PCFS/PCFSServer.exe" ]
38    then
39    PCFSSERVER_EXE="$MION_BRIDGE_TOOLS/PCFS/PCFSServer.exe"
40else
41    PCFSSERVER_EXE="PCFSServer.exe"
42fi
43
44if [ "$USE_CAFEX" == "1" ]
45then
46  saveIFS="$IFS"
47  IFS=$'\n'
48  cafex.exe stop $@
49  rval=$?
50  IFS="$saveIFS"
51  export CAFESTOP_STATUS=$rval
52elif [ "$1" = "-hostonly" -o -z "$MION_BRIDGE_TOOLS" -o -z "$BRIDGE_CURRENT_IP_ADDRESS" ]
53then
54	# We want to kill any running processes, but don't want to talk to a (non-existent?) device
55	ToucanReset -stop
56	"$PCFSSERVER_EXE" -q
57	rval=0
58else
59	export PLATFORM=cafe
60
61    HARD_STOP=0
62    if [ "$1" = "-hard" ]
63    then
64        HARD_STOP=1
65        shift
66    fi
67
68	# Assume CAFERUN_OPTION_SOFT_LAUNCH is set in caferun or autotest script
69	if [ "$CAFERUN_OPTION_SOFT_LAUNCH" = "1" -a $HARD_STOP -eq 0 ]; then
70		# In autotest softlaunch, cafestop is a no-op
71		export CAFESTOP_STATUS=0
72		rval=$CAFESTOP_STATUS
73	else
74		source hoststop $@
75
76		"$PCFSSERVER_EXE" -q
77		export CAFESTOP_STATUS=$?
78
79        if [ $CAFESTOP_STATUS -ne 0 ]; then
80            echo `basename $BASH_SOURCE` error: CAFESTOP_STATUS=$CAFESTOP_STATUS
81        fi
82
83        if [ "$HOSTSTOP_RVAL" == "0" -a "$CAFESTOP_STATUS" == "0" ]; then
84            rval=0
85        else
86            rval=1
87        fi
88	fi
89fi
90
91# 'exit' the result only if "not sourced", since
92# "sourced" calls check environment variables.
93if ! sourced; then
94    exit $rval
95fi
96