#!/usr/bin/env bash
# Cargo runs rustdoc through this script, which `.cargo/config.toml` names.
#
# The examples in the internal crates' docs are written as an application writes them, with
# `io::…` paths that reach every part of the framework. Cargo compiles a crate's doc tests
# against that crate alone, so for the internal crates this script points `io` at the facade,
# which each of them names `io_facade` in its dev-dependencies. Every other rustdoc run passes
# through unchanged.
set -euo pipefail

facade=""
for argument in "$@"; do
    if [[ $argument == io_facade=* ]]; then
        facade=${argument#io_facade=}
    fi
done

if [[ -z $facade ]]; then
    exec rustdoc "$@"
fi

arguments=()
while (($#)); do
    if [[ $1 == --extern && ${2-} == io=* ]]; then
        shift 2
        continue
    fi
    arguments+=("$1")
    shift
done

exec rustdoc "${arguments[@]}" --extern "io=$facade"
