#!/bin/bash
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$NAME
else
    OS="none"
fi

if [ "$OS" == "Ubuntu" ]; then
    if [ "$(dpkg -l | awk '/ubuntu-desktop/ {print }' | wc -l)" -ge 1 ]; then
        while [ ! -f /media/$USER/RPI-RP2/INFO_UF2.TXT ]; do
            sleep 1
        done
        set -e
        cargo run --release
    else
        sudo umount /mnt/usb
        while [ ! -f /mnt/usb/INFO_UF2.TXT ]; do
            sudo mount /dev/sda1 /mnt/usb -o umask=000
            sleep 1
        done
        set -e
        cargo run --release
        sudo umount /mnt/usb
    fi
elif [ "$OS" == "Arch Linux" ]; then
    while [ ! -d /media/RPI-RP2 ]; do
        sleep 1
    done
    set -e
    # cargo run --release --target thumbv6m-none-eabi
    cargo objcopy --release -- -O binary target/thumbv6m-none-eabi/release/rp2040.bin && python3 ~/linuxbox/uf2conv.py -b 0x10000000 -f 0xe48bff56 -c -o /media/RPI-RP2/firmware.uf2 target/thumbv6m-none-eabi/release/rp2040.bin
fi
