#!/bin/bash
shopt -s extglob

export DEBIAN_FRONTEND=noninteractive

echo '* libraries/restart-without-asking boolean true' | debconf-set-selections
PUBLIC_KEY=$(grep -E 'SIGNING_PUB_KEY' /opt/wire-network/blockproducer/start.sh | grep -v '\-\-signature-provider' | sed -E 's/.*="([^"]+)".*/\1/')
PRIVATE_KEY=$(grep -E 'SIGNING_PRIV_KEY' /opt/wire-network/blockproducer/start.sh | grep -v '\-\-signature-provider' | sed -E 's/.*="([^"]+)".*/\1/')

# Output keys 
echo "PUBLIC_KEY: $PUBLIC_KEY"
echo "PRIVATE_KEY: $PRIVATE_KEY"

# get the latest snapshot
SNAPSHOT_FILE=$(ls -t /opt/wire-network/blockproducer/data/snapshots | head -n 1)
SNAPSHOT_FILE="/opt/wire-network/blockproducer/data/snapshots/$SNAPSHOT_FILE"

# Output the snapshot file for confirmation
echo "SNAPSHOT_FILE: $SNAPSHOT_FILE"

DEB_FILE="wire-sysio_5.1.0-ubuntu22.04_amd64.deb"
GITHUB_URL="https://github.com/Wire-Network/wire-sysio/releases/download/v5.1.0-rc1/$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 --no-install-recommends --no-upgrade wire-sysio
sudo apt-get remove -y --no-install-recommends --no-upgrade cdt
# Step 3: cleaning up data dir/
#echo "Cleaning up state /data directory and preparing to restore snapshot WITH FULL HISTORY"
rm -rf /opt/wire-network/blockproducer/data/state/shared_memory.bin
rm -rf /opt/wire-network/chain-api/data/state/shared_memory.bin

# 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 --no-install-recommends --no-upgrade /tmp/$DEB_FILE

# Installing cdt 
echo 'Installing new cdt'
wget https://github.com/Wire-Network/wire-cdt/releases/download/v4.0.1/cdt_4.0.1_amd64.deb -O /tmp/cdt_4.0.1_amd64.deb
sudo apt-get install -y --no-install-recommends --no-upgrade /tmp/cdt_4.0.1_amd64.deb


# Step 5: Restart your block producing and chain_api nodes
echo "Restarting block producing and chain_api nodes..."
sudo bash -c "nodeop --config-dir /opt/wire-network/blockproducer/config --contracts-console --data-dir /opt/wire-network/blockproducer/data --s-chain-contract settle.wns --s-chain-actions batchw --plugin sysio::sub_chain_plugin --signature-provider $PUBLIC_KEY=KEY:$PRIVATE_KEY --snapshot $SNAPSHOT_FILE 2> /opt/wire-network/blockproducer/data/nodeop.log & echo \$! > /opt/wire-network/blockproducer/config/nodeop.pid"
sudo bash -c "nodeop --config-dir /opt/wire-network/chain-api/config --contracts-console --data-dir /opt/wire-network/chain-api/data --snapshot $SNAPSHOT_FILE --disable-replay-opts 2> /opt/wire-network/chain-api/data/nodeop.log & echo \$! > /opt/wire-network/chain-api/config/nodeop.pid"


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


echo 'Removing cdt deb '
rm -rf /tmp/cdt_4.0.1_amd64.deb


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


