#!/bin/bash

set -euo pipefail

# Run the upgrade simulation (./simulate_upgrade) against each contract twice
# against the ethereum mainnet state. Running the upgrade twice ensures that the
# contract is still upgradeable after the first upgrade.

ANVIL_PID=""
function clean_up () {
    ARG=$?
    [ -n "$ANVIL_PID" ] && kill "$ANVIL_PID"
    exit $ARG
}
trap clean_up EXIT

echo "🍴 Forking mainnet..."
./anvil_fork ethereum > /dev/null &
ANVIL_PID=$!

# Sleep for 10 seconds here to give some time for the fork to complete.
sleep 10

echo "========================= Updating core contract #1 ============================"
./simulate_upgrade -m bridge -c ethereum -d
echo "========================= Updating core contract #2 ============================"
./simulate_upgrade -m bridge -c ethereum -d

echo "===================== Updating token bridge contract #1 ========================"
./simulate_upgrade -m token_bridge -c ethereum -d
echo "===================== Updating token bridge contract #2 ========================"
./simulate_upgrade -m token_bridge -c ethereum -d

echo "====================== Updating NFT bridge contract #1 ========================="
./simulate_upgrade -m nft_bridge -c ethereum -d
echo "====================== Updating NFT bridge contract #2 ========================="
./simulate_upgrade -m nft_bridge -c ethereum -d
