#!/bin/bash
# How to run this script
# sudo curl https://www.bi7jta.org/files/MMDVM_Firmware/repeater_HAT/upgrade_repeater_FW_Network_12MHz_b20230726 | 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 "Starting Flashing..."

sudo ~/stm32flashV5 -v -w ~/${hex_file}  -R  -i 20,-21,21:-20,-21,21 /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..."




