#!/bin/bash
# How to run this script
# sudo curl https://www.bi7jta.org/files/MMDVM_Firmware/repeater_HAT/reboot_flash/2.booto_upgrade_repeater_FM-Network.sh | sudo sh

sudo mount -o remount,rw / ;
	
cd ~
hex_file="mmdvm_f4_fm-newtork_build20230726.hex"
echo "hex_file:${hex_file}"
#rm -f ./${hex_file}
curl -OL https://www.bi7jta.org/files/MMDVM_Firmware/repeater_HAT/${hex_file};
curl -OL https://www.bi7jta.org/files/MMDVM_Firmware/repeater_HAT/stm32flashV5; 

sudo chmod +x stm32flashV5;

echo "Stopping Pi-Star Services..." 

sudo systemctl stop pistar-watchdog.timer
sudo systemctl stop pistar-watchdog.service
sudo systemctl stop mmdvmhost.timer
sudo systemctl stop mmdvmhost.service
sleep 3

echo "Killing any remaining MMDVMHost processes..."
sudo killall MMDVMHost
sudo killall MMDVMHost_NoOLED
sudo killall MMDVMHost_Adafruit
echo " "
echo " "
echo "================================WARNING: YOU MUST SHORT BOOTO GND before flash !!!!!================================"
echo " "
echo "Example:  https://www.bi7jta.org/wiki/index.php?title=Main_Page#What_is_Recovery_Mode.3F"
echo " "
echo "================================WARNING: YOU MUST SHORT BOOTO GND before flash !!!!!================================"
echo " "
echo " "
echo "Starting Flashing..."

sudo ~/stm32flashV5 -v -w ~/${hex_file}  -R   /dev/ttyAMA0;

sleep 3

echo "Starting Pi-Star Services..."

sudo mount -o remount,ro /

sudo systemctl start pistar-watchdog.timer
sudo systemctl start pistar-watchdog
sudo systemctl start mmdvmhost.timer
sudo systemctl start mmdvmhost

echo "Finnished..."




