new_variant: scripts detect and warn when standalone

Have the various scripts detect that they are running standalone and
offer the user information about using new_variant.py instead.

BUG=b:162258393
TEST=`./create_coreboot_config.sh volteer volteer gnastygnorc`
and you get a nice warning message about how you could be using
new_variant.py instead.

Change-Id: Ib0908c883338be89f86357a4ea984de1893d0eaa
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2320893
Tested-by: Paul Fagerburg <pfagerburg@chromium.org>
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Commit-Queue: Paul Fagerburg <pfagerburg@chromium.org>
diff --git a/contrib/variant/add_variant_to_yaml.sh b/contrib/variant/add_variant_to_yaml.sh
index 53f9169..285332a 100755
--- a/contrib/variant/add_variant_to_yaml.sh
+++ b/contrib/variant/add_variant_to_yaml.sh
@@ -3,7 +3,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-VERSION="1.2.3"
+VERSION="1.2.4"
 SCRIPT=$(basename -- "${0}")
 
 export LC_ALL=C
@@ -25,6 +25,10 @@
 # shellcheck disable=SC1091
 source "${BASH_SOURCE%/*}/revbump_ebuild.sh"
 
+# shellcheck source=check_standalone.sh
+# shellcheck disable=SC1091
+source "${BASH_SOURCE%/*}/check_standalone.sh"
+
 # This is the name of the base board that we're using to make the variant.
 # ${var,,} converts to all lowercase.
 BASE="${1,,}"
@@ -56,7 +60,8 @@
 
 # Start a branch. Use YMD timestamp to avoid collisions.
 DATE=$(date +%Y%m%d)
-repo start "create_${VARIANT}_${DATE}" . || exit 1
+BRANCH="create_${VARIANT}_${DATE}"
+repo start "${BRANCH}" . || exit 1
 
 # ebuild is located 1 directory up.
 pushd .. || exit 1
@@ -95,3 +100,5 @@
 chromeos-config-bsp-${BASE}-private chromeos-config-bsp chromeos-config
  Check /build/${BASE}/usr/share/chromeos-config for '${VARIANT}' in
  config.json, yaml/config.c, and yaml/*.yaml"
+
+check_standalone "$(pwd)" "${BRANCH}"
diff --git a/contrib/variant/check_standalone.sh b/contrib/variant/check_standalone.sh
new file mode 100644
index 0000000..7532874
--- /dev/null
+++ b/contrib/variant/check_standalone.sh
@@ -0,0 +1,45 @@
+#!/bin/bash
+# Copyright 2020 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+check_standalone() {
+  # Check if this script is running from new_variant.py, and if not, print
+  # a message informing the user that they have the option to back out and
+  # run the process from new_variant.py.
+  #
+  # Usage: check_standalone ${DIR} ${BRANCH}
+
+  local DIR="$1"
+  local BRANCH="$2"
+
+  local PARENT
+  PARENT="$(ps --no-headers -o command "${PPID}")"
+
+  if ! [[ "${PARENT}" =~ "new_variant" ]] ; then
+    cat <<EOF >&2
+******************************************************************************
+
+This script appears to be running stand-alone, instead of under the control
+of new_variant.py. If you want to continue using the scripts by themselves,
+you can do that.
+
+However, there are significant advantages to using new_variant.py: it will
+call the right scripts in the right order, upload CLs, and even add
+Cq-Depend information to them so they submit in the correct order. If you
+haven't uploaded any of your CLs, you can start over with new_variant.py,
+but you'll need to delete your CLs for the new variant.
+
+To delete the CL that this script just created:
+  pushd ${DIR}
+  repo abandon ${BRANCH} .
+  popd
+
+Please refer to the README.md file in platform/dev/contrib/variant for
+details about using new_variant.py to create all of the CLs required for
+a new variant.
+
+******************************************************************************
+EOF
+  fi
+}
diff --git a/contrib/variant/copy_cras_config.sh b/contrib/variant/copy_cras_config.sh
index fadb18e..f381d8d 100755
--- a/contrib/variant/copy_cras_config.sh
+++ b/contrib/variant/copy_cras_config.sh
@@ -3,7 +3,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-VERSION="1.0.1"
+VERSION="1.0.2"
 SCRIPT=$(basename -- "${0}")
 
 export LC_ALL=C
@@ -25,6 +25,10 @@
 # shellcheck disable=SC1091
 source "${BASH_SOURCE%/*}/revbump_ebuild.sh"
 
+# shellcheck source=check_standalone.sh
+# shellcheck disable=SC1091
+source "${BASH_SOURCE%/*}/check_standalone.sh"
+
 # This is the name of the base board.
 # ${var,,} converts to all lowercase.
 BASE="${1,,}"
@@ -44,7 +48,8 @@
 
 # Start a branch. Use YMD timestamp to avoid collisions.
 DATE=$(date +%Y%m%d)
-repo start "create_${VARIANT}_${DATE}" . || exit 1
+BRANCH="create_${VARIANT}_${DATE}"
+repo start "${BRANCH}" . || exit 1
 
 # ebuild will be located 2 directories up.
 pushd ../.. || exit 1
@@ -64,3 +69,4 @@
 BUG=${BUG}
 TEST=N/A"
 
+check_standalone "$(pwd)" "${BRANCH}"
diff --git a/contrib/variant/create_coreboot_config.sh b/contrib/variant/create_coreboot_config.sh
index 6cfdd4c..ea15ab4 100755
--- a/contrib/variant/create_coreboot_config.sh
+++ b/contrib/variant/create_coreboot_config.sh
@@ -3,7 +3,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-VERSION="2.1.1"
+VERSION="2.1.2"
 SCRIPT=$(basename -- "${0}")
 
 export LC_ALL=C
@@ -22,6 +22,10 @@
   exit 1
 fi
 
+# shellcheck source=check_standalone.sh
+# shellcheck disable=SC1091
+source "${BASH_SOURCE%/*}/check_standalone.sh"
+
 # This is the name of the base board.
 # ${var,,} converts to all lowercase.
 BASE="${1,,}"
@@ -51,7 +55,8 @@
 
 # Start a branch. Use YMD timestamp to avoid collisions.
 DATE=$(date +%Y%m%d)
-repo start "create_${VARIANT}_${DATE}" . || exit 1
+BRANCH="create_${VARIANT}_${DATE}"
+repo start "${BRANCH}" . || exit 1
 
 # There are multiple usages of the reference board name that we want to change,
 # using the Hatch reference board and the Kohaku variant in this example.
@@ -97,3 +102,5 @@
 BUG=${BUG}
 TEST=FW_NAME=${VARIANT} emerge-${BASE} coreboot chromeos-bootimage
 Ensure that image-${VARIANT}.*.bin are created"
+
+check_standalone "$(pwd)" "${BRANCH}"
diff --git a/contrib/variant/create_coreboot_variant.sh b/contrib/variant/create_coreboot_variant.sh
index 7cf5436..b0dbb2d 100755
--- a/contrib/variant/create_coreboot_variant.sh
+++ b/contrib/variant/create_coreboot_variant.sh
@@ -5,7 +5,7 @@
 
 set -e
 
-VERSION="4.1.1"
+VERSION="4.1.2"
 SCRIPT="$(basename -- "$0")"
 
 if [[ -z "${CB_SRC_DIR}" ]]; then
@@ -34,6 +34,10 @@
   echo "${UPPER}"
 }
 
+# shellcheck source=check_standalone.sh
+# shellcheck disable=SC1091
+source "${BASH_SOURCE%/*}/check_standalone.sh"
+
 # This is the name of the base board
 BASE="$(to_lower "$1")"
 # This is the name of the reference board that we're using to make the variant.
@@ -132,3 +136,5 @@
 TEST=util/abuild/abuild -p none -t google/${BASE} -x -a
 make sure the build includes GOOGLE_${VARIANT_UPPER}"
 # TODO(b/149702214): verify that it builds correctly
+
+check_standalone "$(pwd)" "${BRANCH}"
diff --git a/contrib/variant/create_initial_ec_image.sh b/contrib/variant/create_initial_ec_image.sh
index f55b020..1ea99db 100755
--- a/contrib/variant/create_initial_ec_image.sh
+++ b/contrib/variant/create_initial_ec_image.sh
@@ -3,7 +3,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-VERSION="1.0.3"
+VERSION="1.0.4"
 SCRIPT=$(basename -- "${0}")
 
 export LC_ALL=C
@@ -20,6 +20,10 @@
   exit 1
 fi
 
+# shellcheck source=check_standalone.sh
+# shellcheck disable=SC1091
+source "${BASH_SOURCE%/*}/check_standalone.sh"
+
 # This is the name of the reference board that we copying to make the variant.
 # ${var,,} converts to all lowercase.
 REF="${1,,}"
@@ -30,7 +34,7 @@
 BUG=${3:-None}
 
 # All of the necessary files are in platform/ec/board
-cd ~/trunk/src/platform/ec/board || exit 1
+cd "${HOME}/trunk/src/platform/ec/board" || exit 1
 
 # Make sure that the reference board exists.
 if [[ ! -e "${REF}" ]]; then
@@ -46,7 +50,8 @@
 
 # Start a branch. Use YMD timestamp to avoid collisions.
 DATE=$(date +%Y%m%d)
-repo start "create_${VARIANT}_${DATE}" . || exit 1
+BRANCH="create_${VARIANT}_${DATE}"
+repo start "${BRANCH}" . || exit 1
 
 mkdir "${VARIANT}"
 cp "${REF}"/* "${VARIANT}"
@@ -77,3 +82,5 @@
 BUG=${BUG}
 BRANCH=none
 TEST=make BOARD=${VARIANT}"
+
+check_standalone "$(pwd)" "${BRANCH}"
diff --git a/contrib/variant/fw_build_config.sh b/contrib/variant/fw_build_config.sh
index 3afc3c9..8925528 100755
--- a/contrib/variant/fw_build_config.sh
+++ b/contrib/variant/fw_build_config.sh
@@ -3,7 +3,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-VERSION="1.0.1"
+VERSION="1.0.2"
 SCRIPT=$(basename -- "${0}")
 set -e
 
@@ -21,6 +21,10 @@
   exit 1
 fi
 
+# shellcheck source=check_standalone.sh
+# shellcheck disable=SC1091
+source "${BASH_SOURCE%/*}/check_standalone.sh"
+
 # This is the name of the base board.
 # ${var,,} converts to all lowercase.
 BASE="${1,,}"
@@ -75,3 +79,5 @@
 
 BUG=${BUG}
 TEST=Verify the ${VARIANT} firmware builds"
+
+check_standalone "$(pwd)" "${BRANCH}"