blob: 98345839960fd410e43365e4c755a98f86143de5 [file] [log] [blame]
#!/bin/bash
# Copyright 2015 The Vanadium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# jiri is a shim script that determines the jiri root directory and invokes
# $JIRI_ROOT/.jiri_root/bin/jiri with the given arguments.
#
# If the JIRI_ROOT environment variable is set, that is assumed to be the jiri
# root directory.
#
# Otherwise the script looks for the .jiri_root directory, starting in the
# current working directory and walking up the directory chain. The search is
# terminated successfully when the .jiri_root directory is found; it fails after
# it reaches the root of the file system.
#
# This script should be invoked from the jiri root directory or one of its
# subdirectories, unless the JIRI_ROOT environment variable is set.
set -euf -o pipefail
# fatal prints an error message and exits.
fatal() {
echo "ERROR: $@" 1>&2
exit 1
}
# Handle "jiri which" without any arguments. This is handy to determine whether
# the PATH is set up pointing at this shim script, or a regular binary. If
# there are any arguments, we pass the command through to the binary.
if [[ $# -eq 1 ]]; then
if [[ "$1" == "which" ]]; then
echo "# script"
type -p $0
exit 0
fi
fi
# If $JIRI_ROOT is set we always use it, otherwise look for a .jiri_root
# directory starting with the current working directory, and walking up.
if [[ -z ${JIRI_ROOT+x} ]]; then
CWD="$(pwd)"
while [[ ! -d "$(pwd)/.jiri_root" ]]; do
if [[ "$(pwd)" == "/" ]]; then
fatal "could not find .jiri_root directory"
fi
cd ..
done
export JIRI_ROOT="$(pwd)"
cd "${CWD}"
fi
# Make sure the jiri binary exists and is executable.
if [[ ! -e "${JIRI_ROOT}/.jiri_root/bin/jiri" ]]; then
fatal "${JIRI_ROOT}/.jiri_root/bin/jiri does not exist"
elif [[ ! -x "${JIRI_ROOT}/.jiri_root/bin/jiri" ]]; then
fatal "${JIRI_ROOT}/.jiri_root/bin/jiri is not executable"
fi
# Add $JIRI_ROOT/.jiri_root/bin to PATH, so that the jiri binary can find the
# subcommand binaries.
export PATH="${JIRI_ROOT}/.jiri_root/bin:${PATH}"
# Execute the jiri binary.
exec "${JIRI_ROOT}/.jiri_root/bin/jiri" "$@"