#!/bin/sh -e
#
# setup-simple-rust-binary - make rust
#
# ---
# source: https://github.com/BurntSushi/dotfiles

if [ "$#" -lt 1 ]; then
  echo "Usage: $(basename "$0") <dir> [<dependency> ...]" >&2
  exit 1
fi
dir="$1"
shift

mkdir -p "${dir}"
cd "${dir}"

# shellcheck disable=SC2312
name="$(basename "$(realpath .)")"
cat >main.rs <<EOF
fn main() -> anyhow::Result<()> {
    println!("Hello, world!");
    Ok(())
}
EOF
cat >.gitignore <<EOF
/target
/Cargo.lock
EOF
cat >rustfmt.toml <<EOF
max_width = 79
use_small_heuristics = "Max"
EOF
# shellcheck disable=SC2250
cat >Cargo.toml <<EOF
[package]
publish = false
name = "$name"
version = "0.1.0"
edition = "2024"

[dependencies]

[[bin]]
name = "$name"
path = "main.rs"

[profile.release]
debug = true
EOF
cargo add anyhow
for dependency; do
  cargo add "${dependency}"
done
