#!/bin/bash
shopt -s extglob

DEB_FILE="wire-sysio_3.1.7.deb"
GITHUB_URL="https://github.com/Wire-Network/wire-sysio/releases/download/v3.1.7/$DEB_FILE"

# Step 1: Stop nodes
echo "Stopping nodes..."
cd /opt/wire-network/blockproducer && sudo ./stop.sh
cd /opt/wire-network/chain-api && sudo ./stop.sh

# Step 2: removing old package
echo "Removing old packages..."
sudo apt-get remove -y wire-sysio

# Step 4: Download and install new deb 
echo "Downloading and installing the new binaries..."
wget $GITHUB_URL -O /tmp/$DEB_FILE
sudo apt-get install -y /tmp/$DEB_FILE

# Step 5: Restart your block producing and chain_api nodes
echo "Restarting block producing and chain_api nodes..."
cd /opt/wire-network/blockproducer && sudo ./start.sh
cd /opt/wire-network/chain-api && sudo ./start.sh

# echo "Verifying nodeop is running..."
# pidof nodeop
# nodeop --full-version
rm -rf /tmp/$DEB_FILE
echo "Deb removed"

echo "Script completed successfully."


# First pre-release: https://github.com/Wire-Network/wire-sysio/releases/download/v3.1.6-rc2/core-renamed-roa-1.16.deb