#!/bin/bash --norc
set -euo pipefail

#
# set-default-visibility.sh //path/to/package:target
#
# Set the `visibility` property of `target` to whatever is currently set as the
# `default_visibility` clause from the `package()` statement in the BUILD file
# found at `path/to/package/BUILD`.
#

function target_to_package() {
  sed -e 's#\(.*\):.*#\1#'
}

target="$1"
package="$(echo "$target" | target_to_package)"
visibility="$(
  (buildozer "print default_visibility" "$package:__pkg__" || true) |
    sed -e 's#\[\(.*\)\]#\1#'
)"

buildozer -stdout "set visibility $visibility" "$target"
