#!/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 LAYEREDGE_CHAIN_ID="4207"" >> $HOME/.bash_profile
echo "export LAYEREDGE_PORT="304"" >> $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$LAYEREDGE_CHAIN_ID\e[0m"
echo -e "Node custom port:  \e[1m\e[32m$LAYEREDGE_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.24.3"
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
rm -rf cosmos-evm
git clone https://github.com/Layer-Edge/cosmos-evm.git
cd cosmos-evm
git checkout edgen-mainnet
make install


# Config and Init App
evmd init $MONIKER --chain-id $LAYEREDGE_CHAIN_ID
sed -i \
  -e 's|^chain-id *=.*|chain-id = "4207"|' \
  -e 's|^keyring-backend *=.*|keyring-backend = "os"|' \
  -e 's|^node *=.*|node = "tcp://localhost:30457"|' \
  $HOME/.evmd/config/client.toml


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


#Configure Seeds and Peers
SEEDS=""
PEERS="d638950f8f8abe07261a10be23fef3c95b3b0626@63.178.182.38:26656,b4f6f7ac3d453c56d5cb7e644830c65803f87755@3.124.18.213:26656"
sed -i -e "s/^seeds *=.*/seeds = \"$SEEDS\"/; s/^persistent_peers *=.*/persistent_peers = \"$PEERS\"/" $HOME/.evmd/config/config.toml


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


# Set Minimum Gas Price, Enable Prometheus, and Disable Indexer
sed -i -e "s/^minimum-gas-prices *=.*/minimum-gas-prices = \"0.025aedgen\"/" $HOME/.evmd/config/app.toml
sed -i -e 's|^enabled-unsafe-cors *=.*|enabled-unsafe-cors = true|' $HOME/.evmd/config/app.toml
sed -i -e 's|^indexer *=.*|indexer = "kv"|' $HOME/.evmd/config/config.toml
sed -i 's|^prometheus *=.*|prometheus = true|' $HOME/.evmd/config/config.toml
sed -i 's/^cors_allowed_origins = \[\]/cors_allowed_origins = \["\*"\]/g' $HOME/.evmd/config/config.toml
awk '
  /^\[api\]/ {in_api=1; print; next}
  /^\[/ && !/^\[api\]/ {in_api=0}
  in_api && /^enable = false/ {print "enable = true"; next}
  {print}
' $HOME/.evmd/config/app.toml > tmp && mv tmp $HOME/.evmd/config/app.toml

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

[Service]
User=$USER
ExecStart=$(which evmd) start $HOME/.evmd --state-sync.snapshot-interval 1000 --state-sync.snapshot-keep-recent 2
Restart=on-failure
RestartSec=3
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOF


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