#!/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 LUMERA_CHAIN_ID="lumera-testnet-2"" >> $HOME/.bash_profile
echo "export LUMERA_PORT="40"" >> $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$LUMERA_CHAIN_ID\e[0m"
echo -e "Node custom port:  \e[1m\e[32m$LUMERA_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.8"
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 https://github.com/LumeraProtocol/lumera/releases/download/v1.6.0/lumera_v1.6.0_linux_amd64.tar.gz
tar -xvf lumera_v1.6.0_linux_amd64.tar.gz
rm lumera_v1.6.0_linux_amd64.tar.gz
rm install.sh
sudo mv libwasmvm.x86_64.so /usr/lib/
chmod +x lumerad
mv lumerad $HOME/go/bin/lumeradtest


# Config and Init App
lumeradtest init $MONIKER --chain-id $LUMERA_CHAIN_ID
sed -i \
  -e 's|^chain-id *=.*|chain-id = "lumera-testnet-2"|' \
  -e 's|^keyring-backend *=.*|keyring-backend = "test"|' \
  -e 's|^node *=.*|node = "tcp://localhost:40657"|' \
  $HOME/.lumera/config/client.toml


# Add Genesis File and Addrbook
wget -O $HOME/.lumera/config/genesis.json https://files.shazoes.xyz/testnets/lumera/genesis.json
wget -O $HOME/.lumera/config/addrbook.json https://files.shazoes.xyz/testnets/lumera/addrbook.json
wget -O $HOME/.lumera/config/claims.csv https://files.shazoes.xyz/testnets/lumera/claim.csv


#Configure Seeds and Peers
SEEDS=""
PEERS="478637fcf73477df67aa0f1a22bb915d820fb64e@lumera-testnet-rpc.shazoes.xyz:40656,5ca72982bcdccec1d14652a6e4a6319e9e3b684a@144.91.86.136:20656,f9d6abcabd2aeb417205d461ce6138473cf58619@62.169.16.57:16656,c099457bfb028f37c407f8aa77e862251ce65a22@193.34.212.38:31656,bb6f26151809349e706e6748f5fe9c6d5f8e6297@149.50.116.116:20656,a764a779e04231ed1c7a63421cfadc52832c4f1a@88.198.46.55:23556,d20502ee3ebd711cf21e6d70076357cb6d4a7c70@152.53.93.131:28656"
sed -i -e "s/^seeds *=.*/seeds = \"$SEEDS\"/; s/^persistent_peers *=.*/persistent_peers = \"$PEERS\"/" $HOME/.lumera/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/.lumera/config/app.toml
sed -i -e "s/^pruning-keep-recent *=.*/pruning-keep-recent = \"$pruning_keep_recent\"/" $HOME/.lumera/config/app.toml
sed -i -e "s/^pruning-keep-every *=.*/pruning-keep-every = \"$pruning_keep_every\"/" $HOME/.lumera/config/app.toml
sed -i -e "s/^pruning-interval *=.*/pruning-interval = \"$pruning_interval\"/" $HOME/.lumera/config/app.toml


# Set Custom Port
sed -i.bak -e "s%:1317%:${LUMERA_PORT}317%g;
s%:8080%:${LUMERA_PORT}080%g;
s%:9090%:${LUMERA_PORT}090%g;
s%:9091%:${LUMERA_PORT}091%g;
s%:8545%:${LUMERA_PORT}545%g;
s%:8546%:${LUMERA_PORT}546%g;
s%:6065%:${LUMERA_PORT}065%g" $HOME/.lumera/config/app.toml
sed -i.bak -e "s%:26658%:${LUMERA_PORT}658%g;
s%:26657%:${LUMERA_PORT}657%g;
s%:6060%:${LUMERA_PORT}060%g;
s%:26656%:${LUMERA_PORT}656%g;
s%^external_address = \"\"%external_address = \"$(wget -qO- eth0.me):${LUMERA_PORT}656\"%;
s%:26660%:${LUMERA_PORT}660%g" $HOME/.lumera/config/config.toml


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



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

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