#!/bin/bash

# Invokes DfuSeCommand.exe to flash a .dfu file to 32blit
# requires DfuSeCommand.exe - https://www.st.com/en/development-tools/stsw-stm32080.html

FILENAME=$1

if [ "$FILENAME" == "" ]; then
    echo "Invokes DfuSeCommand.exe to flash a .dfu file to 32blit"
    echo "Usage: $0 <filename>.dfu"
    exit 1
fi

# Do some rudimentary path fiddling for Windows-compatible path names
FILEPATH_WIN=$(realpath $FILENAME | sed 's/\//\\/g' | sed 's/\\mnt\\c/C:/')

echo "$FILEPATH_WIN"

# Call DfuSeCommand.exe
# Ideally you should use the modified version at https://github.com/pimoroni/DfuSE/ since it will reset the hardware after flashing
/mnt/c/Program\ Files\ \(x86\)/STMicroelectronics/Software/DfuSe\ v3.0.6/Sources/Binary/Release/DfuSeCommand.exe -c -d --fn "$FILEPATH_WIN"

# Add the --r flag if you have a modified DfuSe Commander with reset support
# /mnt/c/Program\ Files\ \(x86\)/STMicroelectronics/Software/DfuSe\ v3.0.6/Sources/Binary/Release/DfuSeCommand.exe -c -d --r --fn "$FILEPATH_WIN"
