#!/bin/bash

set -e
set -u

tty=""

for device in /sys/bus/usb/devices/?-*; do
    if [ -e $device/manufacturer ]; then
	cat $device/manufacturer
	m=$(cat $device/manufacturer)
	if [ "$m" == "Minutnik" ]; then
	    for d in ${device}/?-*; do
		echo "D: $d"
		if [ -e $d/tty ]; then
		    a=$d/tty/*
		    a=$(basename $a)
		    echo Bingo: $a
		    tty=$a
		fi
	    done
	fi
    fi
done

if [ -z "$tty" ]; then
    echo "Could not find STLink tty."
    exit
fi


. libcampo/scripts/campo.sh dummy

if [ "$#" -gt "0" ]; then
    buildDirectory=$1
else
    getBuildDirectory
fi

echo "Build Dir: $buildDirectory"
cd $buildDirectory
make media.bin

${HOME}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin/STM32_Programmer_CLI -c port=/dev/${tty} -e all -d theme_noto/media.bin 0x0

#---fin------------------------------------------------------------------------
