#!/bin/bash

# Function to display the animated text
display_text() {
    # Install Ruby and RubyGems if not already installed
    if ! command -v gem &> /dev/null; then
        echo "RubyGems (gem) is not installed. Installing Ruby..."
        sudo apt-get update
        sudo apt-get install -y ruby-full || { echo "Failed to install Ruby"; exit 1; }
    fi

    # Install figlet if not already installed
    if ! command -v figlet &> /dev/null; then
        echo "Installing figlet..."
        sudo apt-get install -y figlet || { echo "Failed to install figlet"; exit 1; }
    fi

    # Install lolcat using gem if not already installed
    if ! command -v lolcat &> /dev/null; then
        echo "Installing lolcat..."
        sudo gem install lolcat || { echo "Failed to install lolcat"; exit 1; }
    fi

    # Display the animated text
    figlet "Initia Installation By BlackNodes" | lolcat
}

# Clear the screen
clear

# Display the animated text banner
display_text

# Pause to display the text
sleep 3

# Prompt for the MONIKER
echo "Please enter your node's MONIKER (name):"
read MONIKER
export MONIKER

# Echo the MONIKER to check it
echo "Setting up your node, $MONIKER..."

# Update and install dependencies
sudo apt update && sudo apt install curl git jq build-essential gcc unzip wget lz4 aria2 liblz4-tool -y

# Install Go
ver="1.22.0"
wget "https://golang.org/dl/go$ver.linux-amd64.tar.gz"
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf "go$ver.linux-amd64.tar.gz"
rm "go$ver.linux-amd64.tar.gz"
echo "export PATH=\$PATH:/usr/local/go/bin:\$HOME/go/bin" >> $HOME/.bash_profile
source $HOME/.bash_profile

# Install Binary if not already installed
if [ ! -f /root/go/bin/initiad ]; then
    git clone https://github.com/initia-labs/initia.git
    cd initia
    git checkout v0.2.15
    make install
    initiad version
    cd $HOME
fi

# Set Variables
echo "export CHAIN_ID='initiation-1'" >> $HOME/.bash_profile
source $HOME/.bash_profile

# Initialize the node if not already initialized
if [ ! -d /root/.initia ]; then
    initiad init $MONIKER --chain-id $CHAIN_ID
fi

# Download Genesis and Addrbook File
wget -O genesis.json https://files2.blacknodes.net/initiatestnet/genesis.json --inet4-only
mv genesis.json $HOME/.initia/config/
wget -O addrbook.json https://files2.blacknodes.net/initiatestnet/addrbook.json --inet4-only
mv addrbook.json $HOME/.initia/config/

# Use Live Peers
peers=$(curl -s https://services.blacknodes.net/Initia-Testnet/data/peers.txt)
sed -i.bak -e "s/^persistent_peers *=.*/persistent_peers = '$peers'/" $HOME/.initia/config/config.toml

# Fetch the latest block height 
BLOCK_HEIGHT=$(curl -s https://files2.blacknodes.net/initiatestnet/latest_block_height.txt)

# Stop initiad.service if it exists and is active
if systemctl is-active --quiet initiad.service; then
    echo "Stopping initiad.service..."
    sudo systemctl stop initiad.service
fi

# Use Fast Node Snapshot
initiad tendermint unsafe-reset-all --home $HOME/.initia --keep-addr-book
sudo apt-get install wget liblz4-tool aria2 -y

# Delete existing snapshot file if it exists
SNAPSHOT_FILE="initiation-1_${BLOCK_HEIGHT}.tar.lz4"
if [ -f "$SNAPSHOT_FILE" ]; then
    echo "Deleting existing snapshot file..."
    rm "$SNAPSHOT_FILE"
fi

aria2c -x5 https://files3.blacknodes.net/initiatestnet/initiation-1_${BLOCK_HEIGHT}.tar.lz4
echo "Snapshot decompression started, please wait."
lz4 -c -d initiation-1_${BLOCK_HEIGHT}.tar.lz4 | tar -x -C $HOME/.initia/data


# Create a Service File
sudo tee /etc/systemd/system/initiad.service > /dev/null <<EOF
[Unit]
Description=Initia Node
After=network.target

[Service]
User=$USER
Type=simple
ExecStart=$(which initiad) start --home $HOME/.initia
Restart=on-failure
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOF

# Enable and Start The Node
sudo systemctl daemon-reload
sudo systemctl enable initiad.service
sudo systemctl restart initiad.service

# Display the exit message after user exits log view
echo "The Initia node is running in the background, press Ctrl+C to exit if you are stuck"
echo "To view the logs use the command: sudo journalctl -u initiad.service -f -o cat"
