#!/bin/bash

logo_shazoe(){

clear

 cat << "EOF"

         ____  _   _    _     ________  _____ ____  
        / ___|| | | |  / \   |__  / _ \| ____/ ___|
        \___ \| |_| | / _ \    / / | | |  _| \___ \
         ___) |  _  |/ ___ \  / /| |_| | |___ ___) |
        |____/|_| |_/_/   \_\/____\___/|_____|____/
                               services.shazoes.xyz


EOF

}

logo_shazoe;



read -p "Enter your MONIKER :" MONIKER
echo 'export MONIKER='$MONIKER

# set vars
echo "export MONIKER=$MONIKER" >> $HOME/.bash_profile
echo "export ZETA_CHAIN_ID="zetachain_7000-1"" >> $HOME/.bash_profile
echo "export ZETA_PORT="311"" >> $HOME/.bash_profile
source $HOME/.bash_profile


echo -e "Moniker:        \e[1m\e[32m$MONIKER\e[0m"
echo -e "Chain id:       \e[1m\e[32m$ZETA_CHAIN_ID\e[0m"
echo -e "Node custom port:  \e[1m\e[32m$ZETA_PORT\e[0m"
sleep 1


# Update & install dependencies
sudo apt update && sudo apt upgrade -y
sudo apt install curl unzip tar wget clang pkg-config libssl-dev jq build-essential bsdmainutils git make ncdu gcc git jq htop tmux chrony liblz4-tool -y


# Install Go
cd $HOME
VER="1.25.5"
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"
[ ! -f ~/.bash_profile ] && touch ~/.bash_profile
echo "export PATH=$PATH:/usr/local/go/bin:~/go/bin" >> ~/.bash_profile
source $HOME/.bash_profile
[ ! -d ~/go/bin ] && mkdir -p ~/go/bin
go version


# Download Binary
cd $HOME
wget -O $HOME/zetacored https://github.com/zeta-chain/node/releases/download/v36.0.0/zetacored-linux-amd64
chmod +x $HOME/zetacored 
mv $HOME/zetacored $HOME/go/bin


# Config and Init App
zetacored init $MONIKER --chain-id $ZETA_CHAIN_ID
sed -i \
  -e 's|^chain-id *=.*|chain-id = "zetachain_7000-1"|' \
  -e 's|^keyring-backend *=.*|keyring-backend = "os"|' \
  -e 's|^node *=.*|node = "tcp://localhost:31157"|' \
  $HOME/.zetacored/config/client.toml


# Add Genesis File and Addrbook
wget -O $HOME/.zetacored/config/genesis.json https://files.shazoes.xyz/mainnets/zetachain/genesis.json
wget -O $HOME/.zetacored/config/addrbook.json https://files.shazoes.xyz/mainnets/zetachain/addrbook.json


#Configure Seeds and Peers
SEEDS=""
PEERS="24ec0bb556800b8a8c1ff5c97993fbf7765377a9@zetachain-mainnet-rpc.shazoes.xyz:31156,72da7873feafa8abcb7f53f5b870abefc35b5431@88.99.68.249:22556,af8c2a64f121a0fbffe731eaf036b0f921ed36c9@65.109.48.230:32003,d055168f4afe65bbecb951ed1158307ce5b98cc2@95.217.141.114:22556,f9e0ea944362101cf56b424bf62e2f2bb24d946c@145.239.146.45:22556,9dc398c169ce93ef20dceb4bec48d75419b7ffe6@46.4.99.152:21850,e5ebdc30959a5cbb2b7898995ac8489fd91e3d6e@64.185.227.202:31850,967991134f32eb558cae3e4c5f8bd9adbd599177@52.79.168.185:26656,519d5b9f3ac4a0830ecdb1655eabf63e2217e8ae@40.160.16.84:26656,a6c1f55d027e00205386b24e932eb11c0b77de1c@144.76.98.185:31850,d9fff938cf9a3bc11e0da8a1592e84008ef70977@50.115.46.18:31850"
sed -i -e "s/^seeds *=.*/seeds = \"$SEEDS\"/; s/^persistent_peers *=.*/persistent_peers = \"$PEERS\"/" $HOME/.zetacored/config/config.toml


# Config Pruning
pruning="custom"
pruning_keep_recent="100"
pruning_keep_every="0"
pruning_interval="10"
sed -i -e "s/^pruning *=.*/pruning = \"$pruning\"/" $HOME/.zetacored/config/app.toml
sed -i -e "s/^pruning-keep-recent *=.*/pruning-keep-recent = \"$pruning_keep_recent\"/" $HOME/.zetacored/config/app.toml
sed -i -e "s/^pruning-keep-every *=.*/pruning-keep-every = \"$pruning_keep_every\"/" $HOME/.zetacored/config/app.toml
sed -i -e "s/^pruning-interval *=.*/pruning-interval = \"$pruning_interval\"/" $HOME/.zetacored/config/app.toml


# Set Custom Port
sed -i.bak -e "s%:1317%:${ZETA_PORT}17%g;
s%:8080%:${ZETA_PORT}80%g;
s%:9090%:${ZETA_PORT}90%g;
s%:9091%:${ZETA_PORT}91%g;
s%:8545%:${ZETA_PORT}45%g;
s%:8546%:${ZETA_PORT}46%g;
s%:6065%:${ZETA_PORT}65%g" $HOME/.zetacored/config/app.toml
sed -i.bak -e "s%:26658%:${ZETA_PORT}58%g;
s%:26657%:${ZETA_PORT}57%g;
s%:6060%:${ZETA_PORT}60%g;
s%:26656%:${ZETA_PORT}56%g;
s%^external_address = \"\"%external_address = \"$(wget -qO- eth0.me):${ZETA_PORT}56\"%;
s%:26660%:${ZETA_PORT}60%g" $HOME/.zetacored/config/config.toml


# Set Minimum Gas Price, Enable Prometheus, and Disable Indexer
sed -i -e "s/^minimum-gas-prices *=.*/minimum-gas-prices = \"20000000000azeta\"/" $HOME/.zetacored/config/app.toml
sed -i -e 's|^indexer *=.*|indexer = "null"|' $HOME/.zetacored/config/config.toml
sed -i 's|^prometheus *=.*|prometheus = true|' $HOME/.zetacored/config/config.toml


# Download Snapshot
curl https://snapshot.shazoes.xyz/mainnets/snapshot-zetachain.tar.lz4 | lz4 -dc - | tar -xf - -C $HOME/.zetacored


# Set Service File
sudo tee /etc/systemd/system/zetacored.service > /dev/null <<EOF
[Unit]
Description=zetachain-mainnet
After=network-online.target

[Service]
User=$USER
ExecStart=$(which zetacored) start $HOME/.zetacored
Restart=on-failure
RestartSec=3
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOF


# Enable and Start Service
sudo systemctl daemon-reload && sudo systemctl enable zetacored && sudo systemctl start zetacored && sudo journalctl -fu zetacored -o cat