#!/usr/bin/env bash
# Copyright 2021 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# To build crosvm using cargo against libraries and crates provided by ChromeOS
# use this script to update path references in Cargo.toml.

set -e

CARGO_PATH=$(dirname "$0")/../../Cargo.toml

if ! git diff "${CARGO_PATH}"; then
    echo "There is pending Cargo.toml changes, please clean first."
    exit 1
fi

declare -A replacements=(
    ["libcras_stub"]="../../third_party/adhd/cras/client/libcras"
    ["system_api_stub"]="../../platform2/system_api"
    ["third_party/minijail"]="../../aosp/external/minijail"
)

for crate in "${!replacements[@]}"; do
    echo "Replacing '${crate}' with '${replacements[$crate]}'"
    sed -i "s|path = \"${crate}|path = \"${replacements[$crate]}|g" \
        "${CARGO_PATH}"
done

git commit "${CARGO_PATH}" -m 'crosvm: DO NOT SUBMIT: Cargo.toml changes.

This is for local cargo {build,test} in your CrOS checkout.
Please do not submit this change.

BUG=None
TEST=None

Commit: false
'

echo "Modified Cargo.toml with new paths. Please do not submit the change."
