#!/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 NEUTRON_CHAIN_ID="neutron-1"" >> $HOME/.bash_profile
echo "export NEUTRON_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$NEUTRON_CHAIN_ID\e[0m"
echo -e "Node custom port:  \e[1m\e[32m$NEUTRON_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.23.1"
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 neutron
git clone https://github.com/neutron-org/neutron neutron
cd neutron
git checkout v9.1.0
make install

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


# Add Genesis File and Addrbook
wget -O $HOME/.neutrond/config/genesis.json https://snapshots.polkachu.com/genesis/neutron/genesis.json
wget -O $HOME/.neutrond/config/addrbook.json https://snapshots.polkachu.com/addrbook/neutron/addrbook.json


#Configure Seeds and Peers
SEEDS=""
PEERS="7d639ec89124f3b807e106248b108099e4f5d9f2@144.76.139.194:26656,6672b57b33780d843addae63e70fb0ace8e90d8f@144.76.18.142:26660,a4057a30248800f61f53dac5c2e221ba4e48a69c@94.130.22.166:26656,aabcad9448e2ff1d5896bdeb4dc12e2762eb4232@157.180.103.236:26656,fecdf6193f13bc139bf09e580242cb1c594cb1ed@65.109.64.40:26656,47936eba0fc0d0a99bef067b9c8ab72a0827a686@37.27.172.83:26656,797a78de392c2ebdf7c26cb5ae4922f534eb53e4@65.108.13.25:26656,b9acb6b774d6e7576200a5ca740bacbf82ffba06@65.21.69.188:26656,76ba8f92fba8769bbc2ce1abefa30dd4e8dff41f@13.229.158.250:26656,dfde3c92550d408dbf84faf0e3a2cc98e6ee7d0f@13.229.187.30:26656,3d8165888f261efc0100c1e33c33b8049c80b115@23.227.223.101:26656,bfc7564c00d9698cf1be1be3675d955cc02433df@49.12.148.77:26656,b17ef6237073e2cf8a57a0843cff716ef13a3612@94.130.22.168:26656,0320c43fb9c20521449955aa6454187d1723ce7c@213.32.24.197:26656,9282ed202e584f26aab5859951fb2b87183efae5@37.59.18.132:30002,2183e0fd2b423104b3816895104307f12a6c1c32@15.235.232.120:26656,6adc6ffbcc67ea9775646dd9080aab6f82d8302c@185.107.68.143:26656,89757803f40da51678451735445ad40d5b15e059@169.155.45.86:26656,dc5789764f84209a9e0d3c8e80ebe1fe16005782@15.235.232.124:26656,9ffcc9aec7f1d4d575dc73576375c3d15adf7bbc@65.108.42.153:26656,c89a03ff2703099c97cd92501ec1de916919bc7f@136.243.0.220:26656,d5b31c1fa103dc25422d4ab94deb6d179806f106@45.10.26.55:26656,e1b058e5cfa2b836ddaa496b10911da62dcf182e@169.155.44.97:26656,272386bc60732f9fb004e580559f65b7373a60bb@65.109.58.158:19156,e127f2ef48a977b33995d6e37c5b79d7f2e1006c@15.235.230.207:26656,d853af7fd8e8bf727b1f147ec49ff35b1f554c87@157.180.102.226:26656,463deb8556474ce36fb4b46fe34b29d63520fdb7@141.94.100.228:26756,14f7a3cbdfe9a49bd36f18656954a946f4f411ab@199.127.63.149:26676,d529ed65391cee145bd29430dd4bba0f993362ec@46.4.113.110:26656,71c1a97149e71d21913878bcbf5d3feac96d7517@178.63.104.243:26656"
sed -i -e "s/^seeds *=.*/seeds = \"$SEEDS\"/; s/^persistent_peers *=.*/persistent_peers = \"$PEERS\"/" $HOME/.neutrond/config/config.toml


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


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


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


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

[Service]
User=$USER
ExecStart=$(which neutrond) start $HOME/.neutrond --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 neutrond && sudo systemctl start neutrond && sudo journalctl -fu neutrond -o cat