Check-in initial rsyncing script for bp2build.

This script recursively copies BUILD.bazel files from out/soong/bp2build
to the top level directory. It also supports removing the same files.

Test: ./build/bazel/scripts/bp2build-sync.sh write
Test: ./build/bazel/scripts/bp2build-sync.sh remove

Change-Id: I7cdf408b770b0b3ac10a8cd89fedddc35d457b58
diff --git a/scripts/bp2build-sync.sh b/scripts/bp2build-sync.sh
new file mode 100755
index 0000000..554146a
--- /dev/null
+++ b/scripts/bp2build-sync.sh
@@ -0,0 +1,76 @@
+#!/bin/bash
+#
+# This script rsyncs generated bp2build BUILD files from out/soong/bp2build to
+# the top level AOSP source tree.
+#
+# Write BUILD files in declared packages:  ./build/bazel/scripts/bp2build-sync.sh write
+# Remove BUILD files in declared packages: ./build/bazel/scripts/bp2build-sync.sh remove
+
+set -euo pipefail
+
+# Declare packages to be synced here.
+#
+# TODO: Should be parameterized, but let's hardcode the prioritized packages for
+# now.
+declare -a packages=(
+  "bionic"
+)
+
+# We're in <root>/build/bazel/scripts
+AOSP_ROOT="$(dirname $0)/../../.."
+
+function write_build_files() {
+  for package in ${packages[@]}; do
+    echo "Syncing $(dirname $package)"
+    rsync -av \
+      --include="*/" \
+      --include="BUILD.bazel" \
+      --exclude="*" \
+      "$AOSP_ROOT/out/soong/bp2build/$package" \
+      "$AOSP_ROOT/$(dirname $package)"
+  done
+}
+
+function remove_build_files() {
+  pushd $AOSP_ROOT > /dev/null
+  echo "Removing BUILD files.."
+  for package in ${packages[@]}; do
+    find "$package" -type f -name "BUILD.bazel" -exec rm -vf {} \; \
+      || echo "No BUILD files found under $package."
+  done
+  popd > /dev/null
+}
+
+function help() {
+  cat <<EOF
+bp2build-sync.sh: synchronize and delete generated BUILD files.
+Usage:
+  ./bp2build-sync.sh write -- copies generated BUILD files from out/soong/bp2build
+                              to the source tree, for declared packages.
+  ./bp2build-sync.sh remove -- remove BUILD files for declared packages and all subpackages.
+EOF
+}
+
+function run() {
+  action=$1; shift
+
+  case $action in
+    "write")
+      write_build_files
+      ;;
+    "remove")
+      remove_build_files
+      ;;
+    "help")
+      help
+      ;;
+    *)
+      echo "Unknown bp2build-sync action: $action"
+      help
+      exit 1
+  esac
+
+  echo "Done."
+}
+
+run $@