red-level 1.0.7

Cross-platform software display dimming and RGB channel control
Documentation
#!/bin/sh

set -eu

if ! command -v cargo >/dev/null 2>&1; then
    printf '%s\n' "error: cargo was not found; install Rust from https://rustup.rs" >&2
    exit 1
fi

script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
platform=$(uname -s)

case "$platform" in
    Darwin | Linux)
        binary_name=red-level
        default_install_dir=${XDG_BIN_HOME:-"$HOME/.local/bin"}
        ;;
    MINGW* | MSYS* | CYGWIN*)
        binary_name=red-level.exe
        default_install_dir=${LOCALAPPDATA:-"$HOME/.local"}/red-level/bin
        ;;
    *)
        printf 'error: unsupported platform: %s\n' "$platform" >&2
        exit 1
        ;;
esac

install_dir=${RED_LEVEL_INSTALL_DIR:-"$default_install_dir"}
temporary_binary=$install_dir/.$binary_name.tmp.$$

cleanup() {
    rm -f -- "$temporary_binary"
}
trap cleanup EXIT HUP INT TERM

printf '%s\n' "Building red-level..."
(
    cd -- "$script_dir"
    cargo build --release
)

# Resolve the actual Cargo target directory instead of assuming
# "$script_dir/target". This honors CARGO_TARGET_DIR, CARGO_BUILD_TARGET_DIR,
# and any build.target-dir set in a discovered .cargo/config.toml (which may
# live outside of $script_dir).
target_dir=$(
    cd -- "$script_dir" \
        && cargo metadata --no-deps --format-version 1 \
        | sed -n 's/.*"target_directory":"\([^"]*\)".*/\1/p'
)
target_dir=${target_dir:-"$script_dir/target"}
source_binary=$target_dir/release/$binary_name

if [ ! -f "$source_binary" ]; then
    printf 'error: built binary not found at %s\n' "$source_binary" >&2
    exit 1
fi

mkdir -p -- "$install_dir"
cp -- "$source_binary" "$temporary_binary"
chmod 755 "$temporary_binary"
mv -f -- "$temporary_binary" "$install_dir/$binary_name"

printf 'Installed red-level to %s\n' "$install_dir/$binary_name"

case ":$PATH:" in
    *":$install_dir:"*) ;;
    *)
        printf 'Prepend the install directory to PATH with:\n'
        printf '  export PATH="%s:$PATH"\n' "$install_dir"
        ;;
esac