#!/bin/bash

function rust_create_embedded_project() {
    if [ -z "$1" ]; then
        echo "you need to specify a project name. for example"
        echo "create_rust_embedded_project proj-name"
    else
        cargo generate \
            --git https://github.com/knurling-rs/app-template \
            --branch main \
            --name $1
		cd $1
		cargo add panic-halt
    fi
}

function rust_embedded_available_chips() {
    if [ -z "$1" ]; then
        probe-rs chip list
    else
        probe-rs chip list >> $1
	fi
}

function rust_print_elf_headers() {
	if [ -z "$1" ]; then
        echo "you need to specify a project name. for example"
        echo "rust_print_elf_headers proj-name"
    else
		cargo readobj --bin $1 -- --file-headers
	fi
}

function rust_print_linker_sections() {
	if [ -z "$1" ]; then
		echo "you need to specify a project name. for example"
		echo "rust_print_linker_sections proj-name"
		echo ""
		echo "above example will print the release version to print dev version use"
		echo "rust_print_linker_sections proj-name dev"
	elif [ -z "$2" ]; then
		cargo size --bin $1 --release -- -A
	else
		cargo size --bin $1 -- -A
	fi
}

function rust_disassemble() {
	if [ -z "$1" ]; then
		echo "you need to specify a project name. for example"
		echo "rust_disassemble proj-name"
		echo ""
		echo "above example will print the release version to print dev version use"
		echo "rust_disassemble proj-name dev"
	elif [ -z "$2" ]; then
		cargo objdump --bin $1 --release -- --disassemble --no-show-raw-insn --print-imm-hex
	else
		cargo objdump --bin $1 -- --disassemble --no-show-raw-insn --print-imm-hex
	fi
}

function rust_print_size() {
	elif [ -z "$1" ]; then
		cargo nm --release -- --print-size --size-sort
		cargo size --release
	else
		cargo nm -- --print-size --size-sort lists symbol sorted by size
		cargo size
	fi
}
