#!/bin/sh -e
if [ $# -gt 2 ]; then
	echo "Usage: $0 [-f]"
	exit 1
fi
if [ "$1" = '-f' ]; then
	force=true
fi
if ! command -v curl > /dev/null; then
	echo "Error: curl not found"
	exit 1
fi
if ! command -v cc > /dev/null; then
	echo "Error: cc not found"
	exit 1
fi
if command -v cargo > /dev/null && [ "$force" != true ]; then
	echo "Error: rust already installed; use -f to force"
	exit 1
fi
curl --proto '=https' --tlsv1.3 -sSf https://sh.rustup.rs | sh -s -- -y
. "$HOME/.cargo/env"
rustup component add rust-analyzer
cat > ~/.cargo/config.toml << 'EOF'
[registries.crates-io]
protocol = "sparse"
EOF
mkdir -p ~/.config/rustfmt
cat > ~/.config/rustfmt/rustfmt.toml << 'EOF'
hard_tabs = true
EOF
mkdir -p ~/.local/bin
cat > ~/.local/bin/rust-analyzer << 'EOF'
#!/bin/sh
$(rustup which rust-analyzer) "$@"
EOF
chmod +x ~/.local/bin/rust-analyzer
cargo install cargo-auditable
cargo install --force cargo-auditable rust-script
./rust-configure
