Update smoke test

Remove the requirement to set ffx repository server mode to ffx, as this
is the default now.

Change-Id: I642253b5bbd734ea0d889e0f1413d3900ea0b95b
Reviewed-on: https://fuchsia-review.googlesource.com/c/sdk-samples/getting-started/+/673563
Reviewed-by: Chris Holguin <cholguin@google.com>
diff --git a/scripts/smoke_test.sh b/scripts/smoke_test.sh
index 8e3d36f..bfe14b8 100755
--- a/scripts/smoke_test.sh
+++ b/scripts/smoke_test.sh
@@ -1,11 +1,9 @@
 #!/bin/bash
-
 # Copyright 2022 The Fuchsia Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-
-# Run this from the root of the getting-started repo with `scripts/smoke_test.sh`
+# Run from the root of the repo with `scripts/smoke_test.sh`
 set -e
 
 emu_name=_smoke_test
@@ -15,46 +13,75 @@
   return 1
 }
 
+failure() {
+    echo >&2 "ERROR: $0 line $1"
+}
+
+print_and_run() {
+  echo
+  echo "*** $@"
+  "$@"
+}
+
 main() {
+  trap 'failure $LINENO' ERR
+  local keep_emu=0
+  local skip_clean=0
+  while [[ $# -gt 0 ]]; do
+    if [[ "$1" == "--keep" ]]; then
+      keep_emu=1
+    elif [[ "$1" == "--no-clean" ]]; then
+      skip_clean=1
+    else
+      echo >&2 "Invalid argument: $1"
+      echo >&2 "Syntax: $0 [--keep] [--no-clean]"
+      return 1
+    fi
+    shift
+  done
+
   echo "Build starting!"
-  set -x
-
-  # clear previously fetched dependencies to ensure a clean environment
-  bazel clean --expunge
-
-  # fetch dependencies and build the samples source code 
-  bazel build --config=fuchsia_x64 src:samples_repository
-
-  # fetch an emulator image of workstation and start an emulator
-  tools/ffx product-bundle get workstation.qemu-x64
-  stop_emu
 
   if is_mac; then
-    tools/ffx emu start --net user workstation.qemu-x64 -H  --name $emu_name
+    net_mode="user"
   else
-    tools/ffx emu start --net tap workstation.qemu-x64 -H  --name $emu_name
+    net_mode="tap"
   fi
 
-  trap "stop_emu" EXIT
+  if [[ $skip_clean -eq 0 ]]; then
+    # clear previously fetched dependencies to ensure a clean environment
+    print_and_run bazel clean --expunge
+  fi
 
-  # enable ffx package repository mode
-  tools/ffx config set repository.server.mode ffx
-  tools/ffx daemon stop
-  tools/ffx repository server start
+  # fetch dependencies and build the samples source code
+  print_and_run bazel build --config=fuchsia_x64 src:samples_repository
+
+  # fetch an emulator image of workstation and start an emulator
+  print_and_run tools/ffx product-bundle get workstation.qemu-x64
+  stop_emu
+
+  print_and_run tools/ffx emu start --net $net_mode workstation.qemu-x64 \
+    --headless --name $emu_name
+
+  if [[ $keep_emu -eq 0 ]]; then
+    trap "stop_emu" EXIT
+  fi
+
+  # start the package server
+  print_and_run tools/ffx repository server start
 
   # register the package repository with on-demand prebuilt packages
-  tools/ffx -t $emu_name target repository register -r workstation.qemu-x64 --alias fuchsia.com
+  print_and_run tools/ffx -t $emu_name target repository register -r workstation.qemu-x64 --alias fuchsia.com
 
   # register the package repository with these samples packages
-  tools/ffx repository add-from-pm -r fuchsiasamples.com bazel-bin/src/fuchsiasamples.com.repo
-  tools/ffx -t $emu_name target repository register -r fuchsiasamples.com
+  print_and_run tools/ffx repository add-from-pm -r fuchsiasamples.com bazel-bin/src/fuchsiasamples.com.repo
+  print_and_run tools/ffx -t $emu_name target repository register -r fuchsiasamples.com
 
   # run tests on the emulator
-  tools/ffx -t $emu_name test run fuchsia-pkg://fuchsiasamples.com/hello_test#meta/hello_gtest.cm
-  tools/ffx -t $emu_name test run fuchsia-pkg://fuchsiasamples.com/echo_unittests#meta/echo_unittests.cm
-  tools/ffx -t $emu_name test run fuchsia-pkg://fuchsiasamples.com/echo_integration_test#meta/echo_integration_test.cm
+  print_and_run tools/ffx -t $emu_name test run "fuchsia-pkg://fuchsiasamples.com/hello_test#meta/hello_gtest.cm"
+  print_and_run tools/ffx -t $emu_name test run "fuchsia-pkg://fuchsiasamples.com/echo_unittests#meta/echo_unittests.cm"
+  print_and_run tools/ffx -t $emu_name test run "fuchsia-pkg://fuchsiasamples.com/echo_integration_test#meta/echo_integration_test.cm"
 
-  set +x
   echo "Success!"
 }